Zero-config HTTP retries with exponential backoff, jitter, and OAuth2 token refresh for httpx and requests
Project description
retryhttp
Zero-config HTTP retries with exponential backoff, jitter, and OAuth2 token refresh — for both httpx and requests.
from retryhttp import RetryClient
with RetryClient(token_refresher=lambda: fetch_token()) as client:
resp = client.get("https://api.example.com/data")
Why
Every team rewrites retry logic from scratch. retryhttp gives you:
- Exponential backoff with full jitter (AWS-recommended strategy)
- Token refresh hooks — pass a callable, 401s are handled automatically
- Per-status-code retry policies — retry 503 but not 404
- Network error retries — connection errors and timeouts included
- Async support via
AsyncRetryClient - Drop-in replacements — subclasses
httpx.Clientandrequests.Session, so existing code works unchanged
Install
# httpx support
pip install "retryhttp[httpx]"
# requests support
pip install "retryhttp[requests]"
# both
pip install "retryhttp[all]"
Usage
httpx (sync)
from retryhttp import RetryClient, RetryConfig
config = RetryConfig(
max_attempts=5,
backoff_base=0.5,
backoff_max=30.0,
retry_statuses=frozenset({429, 500, 502, 503, 504}),
)
def get_token() -> str:
# call your auth server
return "new-bearer-token"
with RetryClient(config=config, token_refresher=get_token) as client:
resp = client.get("https://api.example.com/resource")
print(resp.json())
httpx (async)
import asyncio
from retryhttp import AsyncRetryClient
async def get_token() -> str:
return "new-bearer-token"
async def main():
async with AsyncRetryClient(token_refresher=get_token) as client:
resp = await client.get("https://api.example.com/resource")
print(resp.json())
asyncio.run(main())
requests
from retryhttp import RetrySession
with RetrySession(token_refresher=lambda: "new-token") as session:
resp = session.get("https://api.example.com/resource")
Configuration
RetryConfig controls all retry behaviour:
| Parameter | Default | Description |
|---|---|---|
max_attempts |
3 |
Total attempts (1 = no retries) |
retry_statuses |
{429,500,502,503,504} |
Status codes that trigger a retry |
backoff_base |
1.0 |
Base backoff in seconds |
backoff_max |
60.0 |
Maximum backoff ceiling |
jitter |
True |
Full jitter on each delay |
retry_on_network_error |
True |
Retry on connection/timeout errors |
token_refresh_status |
401 |
Status that triggers token refresh |
raise_on_exhaust |
True |
Raise RetryExhausted when all attempts fail; False returns the last response |
How backoff works
Uses AWS's Full Jitter strategy:
cap = min(backoff_max, backoff_base × 2^attempt)
sleep = random(0, cap)
This avoids thundering-herd problems when many clients retry simultaneously.
Exceptions
from retryhttp import RetryExhausted
try:
client.get("https://api.example.com")
except RetryExhausted as e:
print(f"Failed after {e.attempts} attempts, last status: {e.last_status}")
Development
git clone https://github.com/praneethvedantham/retryhttp
cd retryhttp
pip install -e ".[dev]"
pytest
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file retryhttp2-0.1.0.tar.gz.
File metadata
- Download URL: retryhttp2-0.1.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c62a78e1fbe889a9a2be647b62735c076ecab244a9bc0463664285971f9709c1
|
|
| MD5 |
82a621420ddbf815b785c7eba48a797e
|
|
| BLAKE2b-256 |
64308565a10fd52eff29f733bf2d8a1decf2987aae66459c1fccf3666fb7593b
|
Provenance
The following attestation bundles were made for retryhttp2-0.1.0.tar.gz:
Publisher:
ci.yml on praneethv2003/retryhttp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
retryhttp2-0.1.0.tar.gz -
Subject digest:
c62a78e1fbe889a9a2be647b62735c076ecab244a9bc0463664285971f9709c1 - Sigstore transparency entry: 2169646686
- Sigstore integration time:
-
Permalink:
praneethv2003/retryhttp@3ccab052468b558d96db525e0c4215ea2cb4055b -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/praneethv2003
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@3ccab052468b558d96db525e0c4215ea2cb4055b -
Trigger Event:
release
-
Statement type:
File details
Details for the file retryhttp2-0.1.0-py3-none-any.whl.
File metadata
- Download URL: retryhttp2-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6a4be6cf5e2e0860b5dac792ad3746afaf42ef8fe202d93405ac64e4ae7e439
|
|
| MD5 |
d8a2459dfef05dc3dc6d16dff4684825
|
|
| BLAKE2b-256 |
5d1a38fe02f6307ed8ae469a63cdfea4da428ed291d4dd52ace682e073509a5c
|
Provenance
The following attestation bundles were made for retryhttp2-0.1.0-py3-none-any.whl:
Publisher:
ci.yml on praneethv2003/retryhttp
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
retryhttp2-0.1.0-py3-none-any.whl -
Subject digest:
f6a4be6cf5e2e0860b5dac792ad3746afaf42ef8fe202d93405ac64e4ae7e439 - Sigstore transparency entry: 2169646700
- Sigstore integration time:
-
Permalink:
praneethv2003/retryhttp@3ccab052468b558d96db525e0c4215ea2cb4055b -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/praneethv2003
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@3ccab052468b558d96db525e0c4215ea2cb4055b -
Trigger Event:
release
-
Statement type: