-
Notifications
You must be signed in to change notification settings - Fork 31
Add async client transport cleanup #765
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
jonathan343
wants to merge
1
commit into
develop
Choose a base branch
from
aiohttp-unclosed-issue
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...mithy-http/.changes/next-release/smithy-http-bugfix-9b22c3201ef34610abb155ba32d0b097.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "bugfix", | ||
| "description": "Preserved HTTP clients across operation configuration copies to avoid duplicating sessions and discarding connection pools." | ||
| } |
4 changes: 4 additions & 0 deletions
4
...ithy-http/.changes/next-release/smithy-http-feature-4fe36219987843b19f163600ae3a25a2.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "type": "feature", | ||
| "description": "Added deterministic connection-pool cleanup and async context manager support to HTTP clients." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| from copy import copy, deepcopy | ||
| from itertools import chain | ||
| from typing import TYPE_CHECKING, Any | ||
| from typing import TYPE_CHECKING, Any, Self | ||
| from urllib.parse import parse_qs | ||
|
|
||
| import yarl | ||
|
|
@@ -111,6 +110,16 @@ async def send( | |
| ) as resp: | ||
| return await self._marshal_response(resp) | ||
|
|
||
| async def close(self) -> None: | ||
| """Close the underlying aiohttp session and its connection pool.""" | ||
| await self._session.close() | ||
|
|
||
| async def __aenter__(self) -> Self: | ||
| return self | ||
|
|
||
| async def __aexit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None: | ||
| await self.close() | ||
|
|
||
| async def _prepare_body(self, body: StreamingBlob) -> AsyncBytesReader | None: | ||
| """Convert a body for aiohttp, omitting seekable bodies with no data.""" | ||
| if not isinstance(body, AsyncBytesReader): | ||
|
|
@@ -159,7 +168,4 @@ async def _marshal_response( | |
| ) | ||
|
|
||
| def __deepcopy__(self, memo: Any) -> "AIOHTTPClient": | ||
| return AIOHTTPClient( | ||
| client_config=deepcopy(self._config), | ||
| _session=copy(self._session), | ||
| ) | ||
| return self | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we already codegen this: Do we need to change the override to return itself? |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # pyright: reportMissingTypeStubs=false,reportUnknownMemberType=false | ||
| from asyncio import gather | ||
| from collections.abc import AsyncGenerator, AsyncIterable | ||
| from copy import deepcopy | ||
| from dataclasses import dataclass | ||
| from inspect import iscoroutinefunction | ||
| from io import BytesIO | ||
| from typing import TYPE_CHECKING, Any | ||
| from typing import TYPE_CHECKING, Any, Self | ||
|
|
||
| from awscrt.exceptions import AwsCrtError | ||
|
|
||
|
|
@@ -195,6 +195,18 @@ async def send( | |
| raise _CRTTimeoutError(f"CRT {e.name}: {e.message}") from e | ||
| raise | ||
|
|
||
| async def close(self) -> None: | ||
| """Close all pooled HTTP connections.""" | ||
| connections = tuple(self._connections.values()) | ||
| self._connections.clear() | ||
| await gather(*(connection.close() for connection in connections)) | ||
|
|
||
| async def __aenter__(self) -> Self: | ||
| return self | ||
|
|
||
| async def __aexit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None: | ||
| await self.close() | ||
|
|
||
| async def _await_response( | ||
| self, stream: "AIOHttpClientStreamUnified" | ||
| ) -> AWSCRTHTTPResponse: | ||
|
|
@@ -364,7 +376,4 @@ async def _create_body_generator( | |
| yield chunk | ||
|
|
||
| def __deepcopy__(self, memo: Any) -> "AWSCRTHTTPClient": | ||
| return AWSCRTHTTPClient( | ||
| eventloop=self._eventloop, | ||
| client_config=deepcopy(self._config), | ||
| ) | ||
| return self | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment at the |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Non-blocking, general comment]
I'm noticing some inconsistency in how we are preserving certain config fields. Our retries strategies each define their own
__deepcopy__override to return the same instance. This works fine for the retry strategies we maintain but if customers provide their own custom implementations, they will need to always implement their own override.This issue also arises for the
config.aws_credentials_identity_resolverfield. We need to preserve the same instance to actually reuse cached credentials. I like your approach of modifying the memo dict in thedeepcopy()call. I will probably add a memo entry for the identity resolver field in my upcoming PR that integrates the chain into the client.As a follow up, we should see if its possible to do all of this work inside the config object's own
__deepcopy__. This should centralize the logic and ensure any retry strategy, transport, or identity resolver is preserved, plus any future fields.We can wait to do this until we deprecate the existing
Configobject in favor of the newAsyncAwsConfig.