Skip to main content

Module retry

Module retry 

Available on crate features http and std only.
Expand description

Middleware for retrying “failed” requests.

Every attempt runs on its own fork of the caller’s request Extensions: an attempt reads everything the caller inserted, while what it (or any inner layer) inserts stays isolated from the caller and from every other attempt. A failed attempt therefore cannot steer the next one by inserting.

Isolation is structural, not deep: an inherited value is shared by handle, so interior mutation through it stays visible to the caller and to every other attempt — which is exactly how a caller-owned counter or budget is meant to work. Only the entries an attempt inserts itself are private to it.

This holds whether or not the request turns out to be retryable: what the Policy decides about retrying has no bearing on who owns the request’s extensions.

The Policy itself is the one exception to that isolation: it is handed a request backed by the caller’s own store, so metadata it records there (a retry count, an exhaustion marker) reaches the next attempt and stays visible to the caller. Only the inner service’s inserts are private to an attempt.

§Layer placement

Place RetryLayer as early — as far outward — in the stack as your use case allows, in front of everything whose work depends on the request: route or proxy selection, DNS overwrites, per-target limits. Such a layer placed outside runs exactly once, and every attempt then reuses its verdict — retrying through the very proxy or address that just failed. Placed inside, it is consulted per attempt.

Retrying requires a replayable body. Policy::prepare_body decides before reading whether to capture data and trailers as the first attempt consumes them. By default, ManagedPolicy keeps up to and including 64 KiB of payload in memory per request. Larger bodies stream on the first attempt without a retry or disk spill. Replay is also skipped if capture takes longer than 30 seconds or storage fails. Source errors reach the first reader.

The policy classifies every attempt it holds a cloned request for, even when the replay became unavailable; a retry then returns its fallback result. When it requests a retry while the first reader is still alive, this layer waits up to the store’s handoff timeout (within the capture deadline) for that reader to finish or be dropped, then drains the remaining frames before the next attempt. A read error, capture failure, or expired timeout during this handoff returns the first attempt’s policy-supplied result, and a live reader keeps streaming.

follow_redirect follows the same rules for redirect hops. Keep that middleware outside this one, so a retry replays one hop instead of the entire chain.

Modules§

early_data
Compose one confirmed early-data recovery with an application retry rule.
managed
Managed retry Policy.
storage
Replay storage for the HTTP retry layer.

Structs§

ManagedPolicy
A managed retry Policy, which allows for an easier interface to configure retrying requests.
Retry
Configure retrying requests of “failed” responses.
RetryBody
A replayable request body, or an original stream sent without retries.
RetryError
Error type for Retry
RetryLayer
Retry requests based on a policy. See the retry module for default body limits.

Enums§

PolicyResult
A retry decision and the result to forward if no further attempt is made.

Traits§

Policy
A “retry policy” to classify if a request should be retried.