@@ -9,40 +9,100 @@ from lightbug_http.utils import logger
99from lightbug_http.owning_list import OwningList
1010
1111
12+ @value
13+ struct Scheme (Hashable , EqualityComparable , Representable , Stringable , Writable ):
14+ var value : String
15+ alias HTTP = Self(" http" )
16+ alias HTTPS = Self(" https" )
17+
18+ fn __hash__ (self ) -> UInt:
19+ return hash (self .value)
20+
21+ fn __eq__ (self , other : Self) -> Bool:
22+ return self .value == other.value
23+
24+ fn __ne__ (self , other : Self) -> Bool:
25+ return self .value != other.value
26+
27+ fn write_to [W : Writer, //](self , mut writer : W) -> None :
28+ writer.write(" Scheme(value=" , repr (self .value), " )" )
29+
30+ fn __repr__ (self ) -> String:
31+ return String.write(self )
32+
33+ fn __str__ (self ) -> String:
34+ return self .value.upper()
35+
36+
37+ @value
38+ struct PoolKey (Hashable , KeyElement ):
39+ var host : String
40+ var port : UInt16
41+ var scheme : Scheme
42+
43+ fn __init__ (out self , host : String, port : UInt16, scheme : Scheme):
44+ self .host = host
45+ self .port = port
46+ self .scheme = scheme
47+
48+ fn __hash__ (self ) -> UInt:
49+ # TODO : Very rudimentary hash. We probably need to actually have an actual hash function here.
50+ # Since Tuple doesn't have one.
51+ return hash (self .host) + hash (self .port) + hash (self .scheme)
52+
53+ fn __eq__ (self , other : Self) -> Bool:
54+ return self .host == other.host and self .port == other.port and self .scheme == other.scheme
55+
56+ fn __ne__ (self , other : Self) -> Bool:
57+ return self .host != other.host or self .port != other.port or self .scheme != other.scheme
58+
59+ fn __str__ (self ) -> String:
60+ var result = String()
61+ result.write(self .scheme.value, " ://" , self .host, " :" , str (self .port))
62+ return result
63+
64+ fn __repr__ (self ) -> String:
65+ return String.write(self )
66+
67+ fn write_to [W : Writer, //](self , mut writer : W) -> None :
68+ writer.write(
69+ " PoolKey(" , " scheme=" , repr (self .scheme.value), " , host=" , repr (self .host), " , port=" , str (self .port), " )"
70+ )
71+
72+
1273struct PoolManager[ConnectionType: Connection]():
1374 var _connections : OwningList[ConnectionType]
1475 var _capacity : Int
15- var mapping : Dict[String , Int]
76+ var mapping : Dict[PoolKey , Int]
1677
1778 fn __init__ (out self , capacity : Int = 10 ):
1879 self ._connections = OwningList[ConnectionType](capacity = capacity)
1980 self ._capacity = capacity
20- self .mapping = Dict[String , Int]()
81+ self .mapping = Dict[PoolKey , Int]()
2182
2283 fn __del__ (owned self ):
2384 logger.debug(
2485 " PoolManager shutting down and closing remaining connections before destruction:" , self ._connections.size
2586 )
2687 self .clear()
2788
28- fn give (mut self , host : String , owned value : ConnectionType) raises :
29- if host in self .mapping:
30- self ._connections[self .mapping[host ]] = value^
89+ fn give (mut self , key : PoolKey , owned value : ConnectionType) raises :
90+ if key in self .mapping:
91+ self ._connections[self .mapping[key ]] = value^
3192 return
3293
3394 if self ._connections.size == self ._capacity:
3495 raise Error(" PoolManager.give: Cache is full." )
3596
36- self ._connections[self ._connections.size] = value^
37- self .mapping[host] = self ._connections.size
38- self ._connections.size += 1
39- logger.debug(" Checked in connection for peer:" , host + " , at index:" , self ._connections.size)
97+ self ._connections.append(value^ )
98+ self .mapping[key] = self ._connections.size - 1
99+ logger.debug(" Checked in connection for peer:" , str (key) + " , at index:" , self ._connections.size)
40100
41- fn take (mut self , host : String ) raises -> ConnectionType:
101+ fn take (mut self , key : PoolKey ) raises -> ConnectionType:
42102 var index : Int
43103 try :
44- index = self .mapping[host ]
45- _ = self .mapping.pop(host )
104+ index = self .mapping[key ]
105+ _ = self .mapping.pop(key )
46106 except :
47107 raise Error(" PoolManager.take: Key not found." )
48108
@@ -52,7 +112,7 @@ struct PoolManager[ConnectionType: Connection]():
52112 if kv[].value > index:
53113 self .mapping[kv[].key] -= 1
54114
55- logger.debug(" Checked out connection for peer:" , host + " , from index:" , self ._connections.size + 1 )
115+ logger.debug(" Checked out connection for peer:" , str (key) + " , from index:" , self ._connections.size + 1 )
56116 return connection^
57117
58118 fn clear (mut self ):
@@ -65,14 +125,14 @@ struct PoolManager[ConnectionType: Connection]():
65125 logger.error(" Failed to tear down connection. Error:" , e)
66126 self .mapping.clear()
67127
68- fn __contains__ (self , host : String ) -> Bool:
69- return host in self .mapping
128+ fn __contains__ (self , key : PoolKey ) -> Bool:
129+ return key in self .mapping
70130
71- fn __setitem__ (mut self , host : String , owned value : ConnectionType) raises -> None :
72- if host in self .mapping:
73- self ._connections[self .mapping[host ]] = value^
131+ fn __setitem__ (mut self , key : PoolKey , owned value : ConnectionType) raises -> None :
132+ if key in self .mapping:
133+ self ._connections[self .mapping[key ]] = value^
74134 else :
75- self .give(host , value^ )
135+ self .give(key , value^ )
76136
77- fn __getitem__ (self , host : String ) raises -> ref [self ._connections] ConnectionType:
78- return self ._connections[self .mapping[host ]]
137+ fn __getitem__ (self , key : PoolKey ) raises -> ref [self ._connections] ConnectionType:
138+ return self ._connections[self .mapping[key ]]
0 commit comments