Skip to content

Commit

Permalink
[PR #9365/d684195b backport][3.11] Speed up the ConnectionKey (#9384)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Oct 1, 2024
1 parent ac48753 commit fbbe4ed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGES/9365.breaking.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Changed ``ClientRequest.connection_key`` to be a `NamedTuple` to improve client performance -- by :user:`bdraco`.
24 changes: 15 additions & 9 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
Iterable,
List,
Mapping,
NamedTuple,
Optional,
Tuple,
Type,
Expand Down Expand Up @@ -208,8 +209,13 @@ def _merge_ssl_params(
return ssl


@attr.s(auto_attribs=True, slots=True, frozen=True, cache_hash=True)
class ConnectionKey:
_SSL_SCHEMES = frozenset(("https", "wss"))


# ConnectionKey is a NamedTuple because it is used as a key in a dict
# and a set in the connector. Since a NamedTuple is a tuple it uses
# the fast native tuple __hash__ and __eq__ implementation in CPython.
class ConnectionKey(NamedTuple):
# the key should contain an information about used proxy / TLS
# to prevent reusing wrong connections from a pool
host: str
Expand Down Expand Up @@ -358,24 +364,24 @@ def _writer(self, writer: Optional["asyncio.Task[None]"]) -> None:
writer.add_done_callback(self.__reset_writer)

def is_ssl(self) -> bool:
return self.url.scheme in ("https", "wss")
return self.url.scheme in _SSL_SCHEMES

@property
def ssl(self) -> Union["SSLContext", bool, Fingerprint]:
return self._ssl

@property
def connection_key(self) -> ConnectionKey:
proxy_headers = self.proxy_headers
if proxy_headers:
if proxy_headers := self.proxy_headers:
h: Optional[int] = hash(tuple(proxy_headers.items()))
else:
h = None
url = self.url
return ConnectionKey(
self.host,
self.port,
self.is_ssl(),
self.ssl,
url.raw_host or "",
url.port,
url.scheme in _SSL_SCHEMES,
self._ssl,
self.proxy,
self.proxy_auth,
h,
Expand Down
5 changes: 2 additions & 3 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
)

import aiohappyeyeballs
import attr

from . import hdrs, helpers
from .abc import AbstractResolver, ResolveResult
Expand Down Expand Up @@ -1401,8 +1400,8 @@ async def _create_proxy_connection(
# asyncio handles this perfectly
proxy_req.method = hdrs.METH_CONNECT
proxy_req.url = req.url
key = attr.evolve(
req.connection_key, proxy=None, proxy_auth=None, proxy_headers_hash=None
key = req.connection_key._replace(
proxy=None, proxy_auth=None, proxy_headers_hash=None
)
conn = Connection(self, key, proto, self._loop)
proxy_resp = await proxy_req.send(conn)
Expand Down

0 comments on commit fbbe4ed

Please sign in to comment.