refactor: replace per-call httpx clients with module-level singletons#67
Open
bolinocroustibat wants to merge 1 commit intomainfrom
Open
refactor: replace per-call httpx clients with module-level singletons#67bolinocroustibat wants to merge 1 commit intomainfrom
bolinocroustibat wants to merge 1 commit intomainfrom
Conversation
Each helper module previously created a new httpx.AsyncClient on every function call and closed it immediately after, causing a full TLS handshake on every outbound request. A memray profiling session showed this pattern accounted for 295 MB of cumulative SSL context allocations across a single short LLM session (~40 outbound calls). Each helper now holds one shared AsyncClient created at import time. The optional `session` parameter is kept on all public functions as a test override hook. pytest-asyncio is configured with a session-scoped event loop so module-level clients survive across tests without triggering "event loop is closed" errors on teardown. Made-with: Cursor
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Memray profiling of a live LLM session revealed that every outbound HTTP call was creating a fresh
httpx.AsyncClient, performing a full TLS handshake, and immediately closing it. With ~40 outbound calls per session (API lookups + Matomo pings), this accumulated 295 MB of SSL context allocations, all transient but entirely unnecessary.This PR replaces the per-call pattern with a module-level singleton client in each helper (
datagouv_api_client,tabular_api_client,crawler_api_client,metrics_api_client,matomo). Connections are now pooled and reused across requests, eliminating repeated TLS handshakes.sessionparameter is preserved on all public functions as a dependency-injection hook for testspytest-asynciois configured with a session-scoped event loop (asyncio_default_test_loop_scope = session) so the shared clients work correctly across the test suite without "event loop is closed" teardown errors