Skip to content

Commit 696ba80

Browse files
Marco Paoliniasvetlov
Marco Paolini
authored andcommitted
Fix cleanup of empty pool keys in connector
Issues #253 and #254 implemented a `_conns` key evince logic in the function that actually **adds** items to `_conns` Issue #406 tweaked this logic even more, making early and eviction of reusable items in the pool possible. Here we put the key eviction logic where it belongs: in the method that **removes** items from the `_conns` pool.
1 parent f9a4f50 commit 696ba80

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

aiohttp/connector.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,10 @@ def connect(self, req):
303303
return conn
304304

305305
def _get(self, key):
306-
conns = self._conns.get(key)
306+
try:
307+
conns = self._conns[key]
308+
except KeyError:
309+
return None, None
307310
t1 = self._loop.time()
308311
while conns:
309312
transport, proto, t0 = conns.pop()
@@ -313,7 +316,9 @@ def _get(self, key):
313316
transport = None
314317
else:
315318
return transport, proto
316-
319+
# No more connections for this key. Drop refs to transport and protocol
320+
# that make the key
321+
del self._conns[key]
317322
return None, None
318323

319324
def _release(self, key, req, transport, protocol, *, should_close=False):
@@ -351,15 +356,6 @@ def _release(self, key, req, transport, protocol, *, should_close=False):
351356

352357
reader = protocol.reader
353358
if should_close or (reader.output and not reader.output.at_eof()):
354-
conns = self._conns.get(key)
355-
if conns is not None and len(conns) >= 0:
356-
# Issue #253: An empty array will eventually be
357-
# removed by cleanup, but it's better to pop straight
358-
# away, because cleanup might not get called (e.g. if
359-
# keepalive is False).
360-
if not acquired:
361-
self._conns.pop(key, None)
362-
363359
transport.close()
364360
else:
365361
conns = self._conns.get(key)

0 commit comments

Comments
 (0)