Skip to content

Allow caching of middleware construction #10890

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
28 changes: 16 additions & 12 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,9 +622,23 @@ async def _request(
get_env_proxy_for_url, url
)

response_params = {
"timer": timer,
"skip_payload": method in EMPTY_BODY_METHODS,
"read_until_eof": read_until_eof,
"auto_decompress": auto_decompress,
"read_timeout": real_timeout.sock_read,
"read_bufsize": read_bufsize,
"timeout_ceil_threshold": self._connector._timeout_ceil_threshold,
"max_line_size": max_line_size,
"max_field_size": max_field_size,
}

req = self._request_class(
method,
url,
response_params=response_params,
timeout=real_timeout,
params=params,
headers=headers,
skip_auto_headers=skip_headers,
Expand Down Expand Up @@ -656,25 +670,15 @@ async def _connect_and_send_request(
assert self._connector is not None
try:
conn = await self._connector.connect(
req, traces=traces, timeout=real_timeout
req, traces=traces, timeout=req._timeout
)
except asyncio.TimeoutError as exc:
raise ConnectionTimeoutError(
f"Connection timeout to host {req.url}"
) from exc

assert conn.protocol is not None
conn.protocol.set_response_params(
timer=timer,
skip_payload=req.method in EMPTY_BODY_METHODS,
read_until_eof=read_until_eof,
auto_decompress=auto_decompress,
read_timeout=real_timeout.sock_read,
read_bufsize=read_bufsize,
timeout_ceil_threshold=self._connector._timeout_ceil_threshold,
max_line_size=max_line_size,
max_field_size=max_field_size,
)
conn.protocol.set_response_params(**req._response_params)
try:
resp = await req.send(conn)
try:
Expand Down
17 changes: 17 additions & 0 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Optional,
Tuple,
Type,
TypedDict,
Union,
)

Expand Down Expand Up @@ -190,6 +191,18 @@ class ConnectionKey(NamedTuple):
proxy_headers_hash: Optional[int] # hash(CIMultiDict)


class _ResponseParams(TypedDict):
timer: Optional[BaseTimerContext]
skip_payload: bool
read_until_eof: bool
auto_decompress: bool
read_timeout: Optional[float]
read_bufsize: int
timeout_ceil_threshold: float
max_line_size: int
max_field_size: int


class ClientRequest:
GET_METHODS = {
hdrs.METH_GET,
Expand Down Expand Up @@ -230,6 +243,8 @@ def __init__(
method: str,
url: URL,
*,
response_params: _ResponseParams,
timeout: ClientTimeout,
params: Query = None,
headers: Optional[LooseHeaders] = None,
skip_auto_headers: Optional[Iterable[str]] = None,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just thinking through future maintainability of this class. Should we put in a **kwargs: Any and not use it, so that any subclass is required to include that and pass it through the super() call? That way we should be pretty safe to add parameters in future without worrying about breaking any subclasses.

Expand Down Expand Up @@ -281,6 +296,8 @@ def __init__(
self.response_class: Type[ClientResponse] = real_response_class
self._timer = timer if timer is not None else TimerNoop()
self._ssl = ssl
self._response_params = response_params
self._timeout = timeout
self.server_hostname = server_hostname

if loop.get_debug():
Expand Down
Loading