@@ -56,7 +56,7 @@ class Config:
56
56
aws_secret_access_key : str = "fake-alternator-lb-secret-access-key"
57
57
update_interval : int = 10
58
58
connect_timeout : int = 3600
59
- max_pool_connections : int = 200
59
+ max_pool_connections : int = 10
60
60
61
61
def _get_nodes (self ) -> List [str ]:
62
62
nodes = []
@@ -120,16 +120,39 @@ def _get_connection_pool(self, parsed):
120
120
if pool :
121
121
return pool
122
122
if self ._config .schema == "http" :
123
- pool = urllib3 .HTTPConnectionPool (
124
- host = parsed .hostname ,
125
- port = parsed .port ,
126
- )
123
+ if self ._config .max_pool_connections == 1 :
124
+ # There is some bug in urllib3, if `size` = 1, pool still grows more than 1
125
+ # only way to make it not to grow is to not provide `size` at all
126
+ pool = urllib3 .HTTPConnectionPool (
127
+ host = parsed .hostname ,
128
+ port = parsed .port ,
129
+ timeout = self ._config .connect_timeout ,
130
+ )
131
+ else :
132
+ pool = urllib3 .HTTPConnectionPool (
133
+ host = parsed .hostname ,
134
+ port = parsed .port ,
135
+ timeout = self ._config .connect_timeout ,
136
+ size = self ._config .max_pool_connections ,
137
+ )
127
138
else :
128
- pool = urllib3 .HTTPSConnectionPool (
129
- host = parsed .hostname ,
130
- port = parsed .port ,
131
- cert_reqs = 'CERT_NONE' ,
132
- )
139
+ if self ._config .max_pool_connections == 1 :
140
+ # There is some bug in urllib3, if `size` = 1, pool still grows more than 1
141
+ # only way to make it not to grow is to not provide `size` at all
142
+ pool = urllib3 .HTTPSConnectionPool (
143
+ host = parsed .hostname ,
144
+ port = parsed .port ,
145
+ cert_reqs = 'CERT_NONE' ,
146
+ timeout = self ._config .connect_timeout ,
147
+ )
148
+ else :
149
+ pool = urllib3 .HTTPSConnectionPool (
150
+ host = parsed .hostname ,
151
+ port = parsed .port ,
152
+ cert_reqs = 'CERT_NONE' ,
153
+ timeout = self ._config .connect_timeout ,
154
+ size = self ._config .max_pool_connections ,
155
+ )
133
156
self ._conn_pools [parsed .netloc ] = pool
134
157
return pool
135
158
@@ -192,9 +215,11 @@ def _get_nodes(self, uri: str) -> List[str]:
192
215
url = parsed .path
193
216
if parsed .query :
194
217
url += "?" + parsed .query
195
- response = self ._get_connection_pool (parsed ).request ("GET" , url )
196
- if response .status != 200 :
197
- return []
218
+ pool = self ._get_connection_pool (parsed )
219
+ with pool :
220
+ response = pool .request ("GET" , url )
221
+ if response .status != 200 :
222
+ return []
198
223
199
224
nodes = json .loads (response .data )
200
225
return [self ._host_to_uri (host ) for host in nodes if host and self ._validate_node (host )]
@@ -241,13 +266,14 @@ def get_known_nodes(self):
241
266
242
267
def _init_botocore_config (self ) -> config .Config :
243
268
config_params = {
244
- "tcp_keepalive" : True ,
269
+ "tcp_keepalive" : bool ( self . _config . max_pool_connections ) ,
245
270
"connect_timeout" : self ._config .connect_timeout ,
246
271
"max_pool_connections" : self ._config .max_pool_connections ,
247
272
}
248
273
if self ._config .client_cert_file :
249
274
if self ._config .client_key_file :
250
- config_params ["client_cert" ] = (self ._config .client_cert_file , self ._config .client_key_file )
275
+ config_params ["client_cert" ] = (
276
+ self ._config .client_cert_file , self ._config .client_key_file )
251
277
else :
252
278
config_params ["client_cert" ] = self ._config .client_cert_file
253
279
return config .Config (** config_params )
0 commit comments