-
Notifications
You must be signed in to change notification settings - Fork 26
Fix/aiohttp call #137
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
Fix/aiohttp call #137
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
677c3a7
Introduce shared aiohttp session pool for connection reuse
ac32bc5
Fix async streaming timeout: use sock_read instead of total
3a8e5fa
Fix session pool: remove broken atexit handler and clean stale entries
1574ccb
Bump version to 1.25.22 and translate all Chinese comments and docstr…
5fdd2e3
Remove redundant HTML header block from README
93a0d28
Fix pre-commit lint issues
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,54 @@ | ||
| # -*- 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 weakref | ||
| from typing import Optional | ||
|
|
||
| import aiohttp | ||
| import certifi | ||
|
|
||
| _shared_ssl_context: Optional[ssl.SSLContext] = None | ||
| _aio_sessions: "weakref.WeakKeyDictionary" = weakref.WeakKeyDictionary() | ||
|
|
||
|
|
||
| def get_ssl_context() -> ssl.SSLContext: | ||
| global _shared_ssl_context | ||
| 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() | ||
|
|
||
| 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() | ||
| 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.