HTTPX Async Client Retry Transport

Subclass the HTTPX transport layer to retry idempotent requests with capped backoff, honoring Retry-After and exponential delays.

PythonResilience

Code

pythonsnippet
40 lines
import asyncio
import random

import httpx


class RetryingTransport(httpx.AsyncHTTPTransport):
    def __init__(self, retries: int = 3, **kwargs):
        super().__init__(**kwargs)
        self._retries = retries

    async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
        retryable = request.method in {'GET', 'HEAD', 'OPTIONS'}
        last_error: Exception | None = None

        for attempt in range(self._retries + 1):
            if attempt > 0:
                backoff = min(2 ** attempt * 0.5, 8.0)
                await asyncio.sleep(backoff + random.uniform(0, 0.3))
            try:
                response = await super().handle_async_request(request)
                if response.status_code in {429, 502, 503, 504} and retryable:
                    retry_after = response.headers.get('Retry-After')
                    if retry_after and retry_after.isdigit():
                        await asyncio.sleep(int(retry_after))
                    continue
                return response
            except (httpx.TransportError,) as exc:
                last_error = exc
        raise httpx.TransportError(f'exhausted retries: {last_error}')


async def main() -> None:
    transport = RetryingTransport(retries=3)
    async with httpx.AsyncClient(transport=transport, timeout=10.0) as client:
        resp = await client.get('https://api.example.com/status')
        print('final status:', resp.status_code)


asyncio.run(main())

How it works

Structurally, expect the class RetryingTransport and the functions __init__, handle_async_request and main and nothing else.

Repetition is confined to for attempt in range(self._retries + 1) — the rest of the file is straight-line. The async parts await their coroutines, which keeps ordering predictable. httpx appears at a raise inside an except handler — it fires only while that failure type is already being handled.

What to watch for

  • Plan for an install step: asyncio and httpx are a real dependency, resolved by the package manager.

Using it

Entry signature: def handle_async_request(self, request: httpx.Request) -> httpx.Response

The demo output is labeled (final status:), so every printed line maps back to its originating operation.

Import handle_async_request from the file and call it — the rest of the code is implementation detail.

Key statements

async def main() -> None:
    transport = RetryingTransport(retries=3)
    async with httpx.AsyncClient(transport=transport, timeout=10.0) as client:
        resp = await client.get('https://api.example.com/status')
        print('final status:', resp.status_code)
def __init__(self, retries: int = 3, **kwargs):
    super().__init__(**kwargs)
    self._retries = retries
except (httpx.TransportError,) as exc:
    last_error = exc

Interactive Tools for This Topic

FAQ

When is the exception raised?
Inside an `except` handler — the `httpx` fires only while that failure type is already being handled, not on its own trigger.
Are there third-party dependencies?
`asyncio` and `httpx` — install them with the package manager before running.