-
Notifications
You must be signed in to change notification settings - Fork 26
Reapply/aiohttp fix #139
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
Merged
Merged
Reapply/aiohttp fix #139
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a48ca3c
Introduce shared aiohttp session pool for connection reuse
0e99336
Fix async streaming timeout: use sock_read instead of total
56f53e8
Fix session pool: remove broken atexit handler and clean stale entries
a33b55a
Bump version to 1.25.22 and translate all Chinese comments and docstr…
fdb4284
Remove redundant HTML header block from README
4c77007
Fix pre-commit lint issues
274a896
Fix leftover try block in async request handler from cherry-pick
e031f88
Add threading.RLock to session pool for thread safety
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| # -*- coding: utf-8 -*- | ||
| # Copyright (c) Alibaba, Inc. and its affiliates. | ||
| """Shared aiohttp session pool with cached SSL context. | ||
|
|
||
| Provides connection reuse across async API calls. Each event loop gets | ||
| its own ClientSession (aiohttp sessions are loop-bound). The SSL context | ||
| is created once and shared across all sessions. | ||
| """ | ||
| import asyncio | ||
| import ssl | ||
| import threading | ||
| import weakref | ||
| from typing import Optional | ||
|
|
||
| import aiohttp | ||
| import certifi | ||
|
|
||
| _shared_ssl_context: Optional[ssl.SSLContext] = None | ||
| _aio_sessions: "weakref.WeakKeyDictionary" = weakref.WeakKeyDictionary() | ||
| _lock = threading.RLock() | ||
|
|
||
|
|
||
| def get_ssl_context() -> ssl.SSLContext: | ||
| global _shared_ssl_context | ||
| with _lock: | ||
| if _shared_ssl_context is None: | ||
| _shared_ssl_context = ssl.create_default_context( | ||
| cafile=certifi.where(), | ||
| ) | ||
| return _shared_ssl_context | ||
|
|
||
|
|
||
| async def get_shared_aio_session() -> aiohttp.ClientSession: | ||
| """Return a shared aiohttp.ClientSession bound to the running event loop. | ||
|
|
||
| The session is lazily created on first use and reused for all | ||
| subsequent calls on the same event loop. Connection pooling (keep-alive) | ||
| is handled by the underlying TCPConnector. | ||
| """ | ||
| loop = asyncio.get_running_loop() | ||
|
|
||
| with _lock: | ||
| session = _aio_sessions.get(loop) | ||
| if session is not None and not session.closed: | ||
| return session | ||
|
|
||
| connector = aiohttp.TCPConnector(ssl=get_ssl_context()) | ||
| session = aiohttp.ClientSession(connector=connector) | ||
| _aio_sessions[loop] = session | ||
| return session | ||
|
|
||
|
|
||
| async def close_shared_aio_session() -> None: | ||
| """Close the shared session for the current event loop.""" | ||
| loop = asyncio.get_running_loop() | ||
| with _lock: | ||
| session = _aio_sessions.pop(loop, None) | ||
| if session is not None and not session.closed: | ||
| await session.close() | ||
|
lzsweb marked this conversation as resolved.
|
||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.