fix(api-client): share a long-lived httpx client, configure timeouts, and single-flight JWKS / OIDC refetch#86
Open
jackton1 wants to merge 2 commits intoauth0:mainfrom
Open
Conversation
… and single-flight JWKS / OIDC refetch
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.
Changes
ApiClient._fetch_jwksand_fetch_oidc_metadatabuild a freshhttpx.AsyncClient()on every cache miss with no explicit timeout and no single-flight protection. Under concurrent load, TTL expiry turns into a thundering herd ofConnectTimeouterrors that surface asUnknownAuth0Exception/ "Unknown auth error". Still reproducible on1.0.0b8.This PR:
utils.py— adds a lazily-initialized module-levelhttpx.AsyncClientwith explicit timeouts (connect=5s, read=10s, write=5s, pool=5s), keep-alive pool limits, andAsyncHTTPTransport(retries=2).fetch_jwksandfetch_oidc_metadatareuse it when nocustom_fetchis supplied. Adds idempotentaclose_default_httpx_client().api_client.py—ApiClient.__init__allocates per-keyasyncio.Lockmaps (_discovery_locks,_jwks_locks)._discoverand_fetch_jwksnow do a fast cache check, take the per-key lock, re-check, then fetch — so N concurrent misses for the same key produce exactly one upstream call. Adds idempotentApiClient.aclose().tests/test_concurrent_fetch.py— new tests for the above.Not changed: public API surface,
custom_fetchbehaviour, cache implementation / TTL semantics. No new dependencies.References
httpxclients and timeoutsTesting
tests/test_concurrent_fetch.py(6 tests,pytest-asyncio+pytest-httpx) covers:aclose()is idempotent and the client can be re-created afterward.Reproduce:
Integration tests against a live tenant were not added — the existing project mocks the network layer, and the
ConnectTimeoutstorm under TTL expiry is exercised deterministically bypytest-httpx.Checklist