Replies: 2 comments
|
A request hook is the right public extension point for signing, but it runs a little earlier than “socket write.” HTTPX invokes it immediately before A practical pattern is: async def sign(request: httpx.Request) -> None:
# Recompute both values from the final request bytes on every attempt.
request.headers["X-Timestamp"] = current_timestamp()
request.headers["Authorization"] = make_signature(request)
timeout = httpx.Timeout(30.0, pool=5.0)
limits = httpx.Limits(max_connections=50)
client = httpx.AsyncClient(
timeout=timeout,
limits=limits,
event_hooks={"request": [sign]},
)The short pool timeout is important: it bounds how stale a hook-generated signature can become while queued. On Allow margin for connection/TLS setup and request-body writes too. If the protocol truly requires signing only after a connection has been acquired, HTTPX currently has no public event hook at that exact point; that would require a lower-level transport/httpcore integration. For a five-minute validity window, bounded pool waiting plus application backpressure should normally avoid that complexity. |
|
A request hook runs too early to guarantee a fresh signing timestamp at high concurrency. In both the synchronous and asynchronous client paths, HTTPX invokes the request hooks in A custom The robust options are generally to:
Be careful with automatic retries: retrying the same already-signed |
Uh oh!
There was an error while loading. Please reload this page.
I'm building an async client based on
httpxfor a service that uses request signing. Signatures must fall within a specific timestamp or risk 401 response. This is not a problem for low volume requests, but high volume requests run the risk of failing authentication due to sufficient clock skew (i.e., the request was signed >5min before it hit the network, as it was sitting in the connection pool waiting to be processed).I thought a way around this might be using event hooks, and at first they seemed to work well. Though in at least one high volume test, I ran into a bunch of 401s. The docs aren't clear exactly when the request hook is processed, so I'm not sure what to make of it.
Are request hooks a good way to handle this? Any other thoughts appreciated!
All reactions