Skip to content

Commit 7242615

Browse files
committed
python: fix connection persistance
1 parent b03f3f2 commit 7242615

File tree

1 file changed

+41
-15
lines changed

1 file changed

+41
-15
lines changed

python/alternator_lb.py

+41-15
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Config:
5656
aws_secret_access_key: str = "fake-alternator-lb-secret-access-key"
5757
update_interval: int = 10
5858
connect_timeout: int = 3600
59-
max_pool_connections: int = 200
59+
max_pool_connections: int = 10
6060

6161
def _get_nodes(self) -> List[str]:
6262
nodes = []
@@ -120,16 +120,39 @@ def _get_connection_pool(self, parsed):
120120
if pool:
121121
return pool
122122
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+
)
127138
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+
)
133156
self._conn_pools[parsed.netloc] = pool
134157
return pool
135158

@@ -192,9 +215,11 @@ def _get_nodes(self, uri: str) -> List[str]:
192215
url = parsed.path
193216
if parsed.query:
194217
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 []
198223

199224
nodes = json.loads(response.data)
200225
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):
241266

242267
def _init_botocore_config(self) -> config.Config:
243268
config_params = {
244-
"tcp_keepalive": True,
269+
"tcp_keepalive": bool(self._config.max_pool_connections),
245270
"connect_timeout": self._config.connect_timeout,
246271
"max_pool_connections": self._config.max_pool_connections,
247272
}
248273
if self._config.client_cert_file:
249274
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)
251277
else:
252278
config_params["client_cert"] = self._config.client_cert_file
253279
return config.Config(**config_params)

0 commit comments

Comments
 (0)