Add retry logic with exponential backoff for transient failures - #28
Open
szemyd wants to merge 1 commit into
Open
Add retry logic with exponential backoff for transient failures#28szemyd wants to merge 1 commit into
szemyd wants to merge 1 commit into
Conversation
`fetch_json` surfaced any non-2xx as an APIError on the first attempt, so a momentary 503 from the API (e.g. its Atlas symbol source being rate-limited upstream) failed the whole call — the flaky `get_symbols` CI failure. Both transports now retry 429/502/503/504 and connection errors with jittered exponential backoff, honouring a server-sent `Retry-After` capped at 10s so a synchronous call can never stall for minutes. Non-transient responses (401, 400, 404) are still raised on the first attempt.
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.
Summary
Implements automatic retry logic with exponential backoff for both the httpx and pyfetch HTTP transports. Transient failures (connection errors, rate limits, and 5xx errors) are now automatically retried, while client errors and successful responses are returned immediately.
Key Changes
New retry module (
_retry.py): Shared backoff policy implementing exponential backoff with jitter, respecting server-sentRetry-Afterheaders (capped atMAX_RETRY_DELAY)httpx transport (
_httpx_transport.py):_get_with_retries()helper that retries onhttpx.TransportErrorand retryable status codes (429, 502, 503, 504)fetch_json()to acceptmax_retriesandbackoff_baseparameterspyfetch transport (
_pyfetch_transport.py):_pyfetch_get()and_parse_json_response()helpers to separate concerns_retry_after()helper to extractRetry-Afterheaders from pyfetch responsesfetch_json()with same retry parameters and behavior as httpx transportConfiguration (
config.py):RETRYABLE_STATUS_CODESfrozenset (429, 502, 503, 504)MAX_RETRY_DELAYconstant (10.0 seconds) to prevent excessive waits from server hintsTest coverage:
test_retry.pywith comprehensive httpx transport retry teststest_pyfetch_transport.pywith pyfetch-specific retry testsFakeResponsemock to support headers for testingRetry-AfterbehaviorImplementation Details
backoff_base * (2^attempt) + random(0, 1), capped atMAX_RETRY_DELAYRetry-After(delta-seconds form only) takes precedence over exponential scheduleasyncio.sleep()to keep suite fast while verifying backoff behaviorhttps://claude.ai/code/session_01F2CWz3EUMbCukT4anW6edg