Repository navigation
perf(leadgen): reuse pooled httpx clients in plain fetch - #96
Merged
Merged
Conversation
StealthClient._fetch_plain built and tore down a new httpx.AsyncClient per fetch, paying a fresh TCP/TLS handshake every time. Pool clients keyed by (running event loop, proxy) with a bounded LRU (16 per loop), close evicted clients once idle, and add aclose_plain_clients() wired into app shutdown. Security properties are unchanged: follow_redirects=False, check_url on every hop, _MAX_REDIRECTS enforced, timeout passed per request. The shared client uses a reject-all cookie jar so no cookie can persist across fetches or tenants; cookies within a single redirect chain are replayed from a per-fetch jar, matching the old per-call behaviour. Part of #33 Signed-off-by: debpalash <debash67@outlook.in> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Part of #33. This is the cheap fix: Tier-1 plain HTTP fetches now reuse their transports.
What changed
StealthClient._fetch_plainused to build and close a newhttpx.AsyncClienton every fetch, so every fetch paid a fresh TCP/TLS handshake. It now borrows a pooled client:WeakKeyDictionary, so a finished loop (tests,asyncio.runin workers) drops its clients and they are never handed to another loop._MAX_PLAIN_CLIENTS). An evicted client is closed once its in-flight requests finish (tracked with an in-use refcount).aclose_plain_clients()closes the current loop's clients. It is called from the FastAPI lifespan shutdown inapps/api/main.py.Safety properties (per the RFC: "may reuse transports, but must not reuse tenant-specific credentials or cookies")
follow_redirects=False.check_url(current, allow_http=True, resolve=True)still runs on every hop._MAX_REDIRECTSis still enforced andBlockedUrlErrorbehaves the same.build_request(..., timeout=timeout)), so callers' timeouts are still honored on the shared client.Set-Cookiefrom one fetch can never reach a later fetch, tenant or workspace. Cookies set during a single redirect chain are replayed from a per-fetch jar, which matches the old per-call-client behaviour. One thing to know: httpx copies anhttpx.Cookiesargument into a fresh jar with the default policy, so the rawCookieJarhas to be passed. The new tests caught exactly this leak during development.verify=Trueand the proxy are unchanged. No credentials are stored on the client.Tests
New
tests/test_leadgen_http_client_reuse.py(11 tests, offline viahttpx.MockTransport):Set-Cookieis not sent on a later fetch, and the shared jar stays emptycheck_urlis called on every redirect hop withallow_http=True, resolve=Trueaclose_plain_clientscloses and clears the poolCommands run:
uv run pytest -q tests/test_leadgen_http_client_reuse.py tests/test_security_floor.py tests/test_browser_tier_escalation.py: 51 passeduv run pytest -q -x: 2226 passed, 139 skippeduvx ruff checkon the changed files: no new findings on the changed lines. The remaining findings were already there.🤖 Generated with Claude Code