Skip to content

Add opt-in status-code retries to Client and AsyncClient - #3857

Open
masterramankhatri-glitch wants to merge 4 commits into
encode:masterfrom
masterramankhatri-glitch:feature/status-code-retries
Open

masterramankhatri-glitch wants to merge 4 commits into
encode:masterfrom
masterramankhatri-glitch:feature/status-code-retries

Conversation

@masterramankhatri-glitch

Copy link
Copy Markdown

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

  • Client and AsyncClient can now be configured to automatically retry
    a request when the response has one of the configured retryable status
    codes.
  • The retry behaviour is off by default. Existing code that does not
    opt in behaves exactly as before, so this is fully backward compatible.
  • Retries apply per request and stop as soon as a non-retryable response
    is received or the configured attempts are exhausted.
  • Updated the client implementation in httpx/_client.py.
  • Added documentation for the new option in docs/advanced/clients.md.
  • Added a changelog entry under the 1.0.0 (Unreleased) section.

Tests

  • Added a new test module tests/client/test_retries.py covering the
    retry behaviour for both the sync and async clients — 21 tests, all
    passing.
  • Full test suite run locally: 1435 passed (the only failures are
    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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant