Examples & integrations
Verified examples:
docs/snippets/async_worker_retry.pydocs/snippets/async_worker_abort.pydocs/snippets/bench_retry.py
These snippets are exercised by smoke tests. Examples that depend on optional third-party services, credentials, or external runtimes remain illustrative unless noted otherwise.
HTTPX contrib wrappers (sync + async)
- Snippet:
docs/snippets/httpx_contrib.py - Canonical helper API via
RetryingHttpxClient/AsyncRetryingHttpxClient. - Includes
default_result_classifierand idempotency gating. - Run:
uv pip install "redress[httpx]"thenuv run python docs/snippets/httpx_contrib.py.
HTTPX (sync)
- Snippet:
docs/snippets/httpx_sync_retry.py - Shows per-class strategies (tighter backoff for 429s) and metric hook logging.
- Run:
uv pip install httpxthenuv run python docs/snippets/httpx_sync_retry.py.
HTTPX Retry-After (sync)
- Snippet:
docs/snippets/httpx_retry_after.py - Uses result-based retries with Retry-After metadata and a fallback jitter strategy.
- Run:
uv pip install httpxthenuv run python docs/snippets/httpx_retry_after.py.
requests (sync)
- Snippet:
docs/snippets/requests_retry.py - Uses
http_classifierwith requests and emits metrics. - Run:
uv pip install requeststhenuv run python docs/snippets/requests_retry.py.
Prometheus contrib hook
- Snippet:
docs/snippets/prometheus_contrib.py - Uses
redress.contrib.prometheus.prometheus_hookswith event and retry-sleep metrics. - Run:
uv pip install "redress[prometheus]"thenuv run python docs/snippets/prometheus_contrib.py.
Datadog contrib hook
- Snippet:
docs/snippets/datadog_contrib.py - Uses
redress.contrib.datadog.datadog_hooksto emit DogStatsD counters and retry-delay histograms. - Run:
uv pip install "redress[datadog]"thenuv run python docs/snippets/datadog_contrib.py.
Sentry contrib hook
- Snippet:
docs/snippets/sentry_contrib.py - Uses
redress.contrib.sentry.sentry_hooksto record breadcrumbs and terminal failure messages. - Run:
uv pip install "redress[sentry]"thenuv run python docs/snippets/sentry_contrib.py.
HTTPX (async)
- Snippet:
docs/snippets/httpx_async_retry.py - Same shape as sync, with
AsyncRetryPolicyandhttpx.AsyncClient. - Run:
uv pip install httpxthenuv run python docs/snippets/httpx_async_retry.py.
HTTPX async + Policy + circuit breaker
- Snippet:
docs/snippets/httpx_async_policy_breaker.py - Unified
AsyncPolicydemo with Retry-After result retries and circuit breaking. - Run:
uv pip install httpxthenuv run python docs/snippets/httpx_async_policy_breaker.py.
Async worker loop
- Snippet:
docs/snippets/async_worker_retry.py - Verified: smoke-tested in the test suite.
- Simulates a message worker with retries and metrics per message.
- Run:
uv run python docs/snippets/async_worker_retry.py.
Async worker with cooperative abort
- Snippet:
docs/snippets/async_worker_abort.py - Verified: smoke-tested in the test suite.
- Shows
abort_iffor shutdown andon_logfor durable attempt logging. - Run:
uv run python docs/snippets/async_worker_abort.py.
Async Postgres (asyncpg)
- Snippet:
docs/snippets/asyncpg_retry.py - Uses
sqlstate_classifierto map SQLSTATE codes and retries with asyncpg. - Run:
uv pip install asyncpgand setASYNC_PG_DSN, thenuv run python docs/snippets/asyncpg_retry.py.
PyODBC + SQLSTATE classifier
- Classifier:
pyodbc_classifier(inredress.extras) maps SQLSTATE codes to ErrorClass. - Snippet:
docs/snippets/pyodbc_retry.pyshows batched row fetch under retry. - Run:
uv pip install pyodbcand setPYODBC_CONN_STR, thenuv run python docs/snippets/pyodbc_retry.py.
ASGI middleware (Starlette-style)
- Snippet:
docs/snippets/asgi_middleware.py - Uses
redress.contrib.asgi.retry_middlewarewithdefault_operation(METHOD + scope["path"]). - Scope: Starlette-style
(request, call_next)middleware. This is not raw ASGI(scope, receive, send). - Run:
uv pip install starlette "uvicorn[standard]"thenuv run uvicorn docs.snippets.asgi_middleware:app --reload.
gRPC unary-unary client interceptor
- Snippet:
docs/snippets/grpc_interceptor.py - Uses
redress.contrib.grpc.unary_unary_client_interceptorandaio_unary_unary_client_interceptor. - Recommended classifier:
redress.extras.grpc_classifier. - Run:
uv pip install "redress[grpc]".
FastAPI proxy with retries
- Snippet:
docs/snippets/fastapi_downstream.py - Wraps a downstream call with retries and exposes
/metricscounters. - Run:
uv pip install "fastapi[standard]" httpxthenuv run uvicorn docs.snippets.fastapi_downstream:app --reload. - Shape:
from fastapi import FastAPI, HTTPException
from redress import RetryPolicy, default_classifier
from redress.strategies import decorrelated_jitter
app = FastAPI()
policy = RetryPolicy(classifier=default_classifier, strategy=decorrelated_jitter(max_s=2.0))
@app.get("/proxy")
def proxy():
def _call():
resp = httpx.get("https://httpbin.org/status/500", timeout=3.0)
resp.raise_for_status()
return resp.text
try:
return {"body": policy.call(_call, operation="proxy_downstream")}
except Exception as exc:
raise HTTPException(status_code=502, detail=str(exc))
FastAPI middleware with per-endpoint policies
- Snippet:
docs/snippets/fastapi_middleware.py - Middleware applies a retry policy to requests; includes a proxy endpoint.
- Note: This retries the full handler; use only for idempotent endpoints.
- Run:
uv pip install "redress[fastapi]" "fastapi[standard]" httpxthenuv run uvicorn docs.snippets.fastapi_middleware:app --reload. - Shape:
from fastapi import FastAPI, Request
from redress import AsyncRetryPolicy
from redress.contrib.fastapi import default_operation, retry_middleware
from redress.extras import http_classifier
from redress.strategies import decorrelated_jitter
app = FastAPI()
def policy_for(_request: Request) -> AsyncRetryPolicy:
return AsyncRetryPolicy(
classifier=http_classifier,
strategy=decorrelated_jitter(max_s=2.0),
max_attempts=4,
deadline_s=8.0,
)
app.middleware("http")(
retry_middleware(
policy_provider=policy_for,
operation=default_operation,
)
)
Benchmarks (pyperf)
- Snippet:
docs/snippets/bench_retry.py - Verified: benchmark functions are smoke-tested in the test suite.
- Measures bare sync retry overhead with pyperf runners.
- Run:
uv pip install .[dev]thenuv run python docs/snippets/bench_retry.py.