Repository navigation
Add opt-in status-code retries to Client and AsyncClient - #3857
masterramankhatri-glitch wants to merge 10 commits into
Conversation
|
The opt-in retry on status codes is a clean addition. Good call having |
ege-arhan
left a comment
There was a problem hiding this comment.
Thanks, the opt-in shape and replayability guard look right.
One issue with the Retry-After handling in _get_retry_delay (httpx/_client.py:253-274): the header value is returned unbounded, so a server can park the client for an arbitrary amount of time. Retry-After: 86400 makes time.sleep()/anyio.sleep() block for 24h, with no way to bound it short of disabling retries entirely.
Repro (with time.sleep patched to record the delay, max_retries=1, transport returns 503 with Retry-After: 86400):
status 200 sleep_delays [86400.0]
The HTTP-date branch (line 261-274) is unbounded the same way.
Suggestion: clamp both branches to a configurable ceiling, e.g. a retry_after_max (default something like 60s) or reuse a backoff cap:
if delay >= 0:
return min(float(delay), self.retry_after_max)and a test asserting Retry-After: 86400 produces the capped delay would keep this from regressing. Worth noting too that the docs currently just say the header is honored, without mentioning that it can be huge.
|
Thanks for the careful review! You're right - the Retry-After value was unbounded. I've added a retry_after_max client option (default 60s) and clamped both the delta-seconds and HTTP-date branches to it, with regression tests for the Retry-After: 86400 case (sync, async, and HTTP-date forms) and a docs note. Pushed in the latest commits - please take another look when you get a chance. |
Summary
This PR adds opt-in status-code retries to httpx. Until now, httpx did not
retry a request when the server responded with a transient error status
(for example 429 or 503), so every user had to write their own retry loop
around the client. This change adds that capability inside the client
itself, in a fully opt-in way.
What changed
ClientandAsyncClientcan now be configured to automatically retrya request when the response has one of the configured retryable status
codes.
opt in behaves exactly as before, so this is fully backward compatible.
is received or the configured attempts are exhausted.
httpx/_client.py.docs/advanced/clients.md.Tests
tests/client/test_retries.pycovering theretry behaviour for both the sync and async clients — 21 tests, all
passing.
pre-existing and unrelated to this change).
Notes
This is intentionally a minimal, focused implementation: only status-code
based retries, no changes to transport-level or connection-error handling,
so it is easy to review and safe to extend later.