Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small cleanups to connector ConnectionKey usage #9333

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,9 @@ def __init__(
self._force_close = force_close

# {host_key: FIFO list of waiters}
self._waiters = defaultdict(deque) # type: ignore[var-annotated]
self._waiters: DefaultDict[ConnectionKey, deque[asyncio.Future[None]]] = (
defaultdict(deque)
)

self._loop = loop
self._factory = functools.partial(ResponseHandler, loop=loop)
Expand Down Expand Up @@ -356,13 +358,10 @@ def _cleanup(self) -> None:
def _drop_acquired_per_host(
self, key: "ConnectionKey", val: ResponseHandler
) -> None:
acquired_per_host = self._acquired_per_host
if key not in acquired_per_host:
return
conns = acquired_per_host[key]
conns.remove(val)
if not conns:
del self._acquired_per_host[key]
if conns := self._acquired_per_host.get(key):
conns.remove(val)
if not conns:
del self._acquired_per_host[key]

def _cleanup_closed(self) -> None:
"""Double confirmation for transport close.
Expand Down Expand Up @@ -492,7 +491,7 @@ async def connect(
# Wait if there are no available connections or if there are/were
# waiters (i.e. don't steal connection from a waiter about to wake up)
if available <= 0 or key in self._waiters:
fut = self._loop.create_future()
fut: asyncio.Future[None] = self._loop.create_future()

# This connection will now count towards the limit.
self._waiters[key].append(fut)
Expand Down Expand Up @@ -662,20 +661,21 @@ def _release(

if key.is_ssl and not self._cleanup_closed_disabled:
self._cleanup_closed_transports.append(transport)
else:
conns = self._conns.get(key)
if conns is None:
conns = self._conns[key] = []
conns.append((protocol, self._loop.time()))

if self._cleanup_handle is None:
self._cleanup_handle = helpers.weakref_handle(
self,
"_cleanup",
self._keepalive_timeout,
self._loop,
timeout_ceil_threshold=self._timeout_ceil_threshold,
)
return

conns = self._conns.get(key)
if conns is None:
conns = self._conns[key] = []
conns.append((protocol, self._loop.time()))

if self._cleanup_handle is None:
self._cleanup_handle = helpers.weakref_handle(
self,
"_cleanup",
self._keepalive_timeout,
self._loop,
timeout_ceil_threshold=self._timeout_ceil_threshold,
)

async def _create_connection(
self, req: ClientRequest, traces: List["Trace"], timeout: "ClientTimeout"
Expand Down
Loading