From 696ba80333887e2ae21e206861b45385bcf34d80 Mon Sep 17 00:00:00 2001 From: Marco Paolini Date: Sat, 22 Aug 2015 23:20:40 +0200 Subject: [PATCH] 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. --- aiohttp/connector.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/aiohttp/connector.py b/aiohttp/connector.py index 5bb1d3c715a..f39ae89d9b0 100644 --- a/aiohttp/connector.py +++ b/aiohttp/connector.py @@ -303,7 +303,10 @@ def connect(self, req): return conn def _get(self, key): - conns = self._conns.get(key) + try: + conns = self._conns[key] + except KeyError: + return None, None t1 = self._loop.time() while conns: transport, proto, t0 = conns.pop() @@ -313,7 +316,9 @@ def _get(self, key): transport = None else: return transport, proto - + # No more connections for this key. Drop refs to transport and protocol + # that make the key + del self._conns[key] return None, None def _release(self, key, req, transport, protocol, *, should_close=False): @@ -351,15 +356,6 @@ def _release(self, key, req, transport, protocol, *, should_close=False): reader = protocol.reader if should_close or (reader.output and not reader.output.at_eof()): - conns = self._conns.get(key) - if conns is not None and len(conns) >= 0: - # Issue #253: An empty array will eventually be - # removed by cleanup, but it's better to pop straight - # away, because cleanup might not get called (e.g. if - # keepalive is False). - if not acquired: - self._conns.pop(key, None) - transport.close() else: conns = self._conns.get(key)