Skip to main content

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.Client and requests.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

retryhttp2-0.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

retryhttp2-0.1.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

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

Hashes for retryhttp2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c62a78e1fbe889a9a2be647b62735c076ecab244a9bc0463664285971f9709c1
MD5 82a621420ddbf815b785c7eba48a797e
BLAKE2b-256 64308565a10fd52eff29f733bf2d8a1decf2987aae66459c1fccf3666fb7593b

See more details on using hashes here.

Provenance

The following attestation bundles were made for retryhttp2-0.1.0.tar.gz:

Publisher: ci.yml on praneethv2003/retryhttp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for retryhttp2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6a4be6cf5e2e0860b5dac792ad3746afaf42ef8fe202d93405ac64e4ae7e439
MD5 d8a2459dfef05dc3dc6d16dff4684825
BLAKE2b-256 5d1a38fe02f6307ed8ae469a63cdfea4da428ed291d4dd52ace682e073509a5c

See more details on using hashes here.

Provenance

The following attestation bundles were made for retryhttp2-0.1.0-py3-none-any.whl:

Publisher: ci.yml on praneethv2003/retryhttp

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page