Skip to main content

Module middleware

Module middleware 

Source
Expand description

Middleware implementations. Middleware: the core Middleware trait + ClientWithMiddleware chain runner, plus the bundled feature-gated implementations.

§Ordering semantics

Middleware wrap the transport in an onion; the middleware registered first is the outermost layer — it observes the request first and the response last:

ClientBuilder::new(client)
    .with(AuthMiddleware)       // 1st registered → outermost
    .with(RetryMiddleware::new(config))
    .build();

request  →  Auth  →  Retry  →  transport
response ←  Auth  ←  Retry  ←  ╹

Inner middleware therefore see every attempt a retry-style outer middleware issues, and outer middleware see only the final outcome. The built-in default client composes [retry, breaker]: the breaker sits inside the retry loop and records each HTTP attempt individually.

§Built-ins vs middleware (the composition story)

The bundled middleware are the composition points; the crate::ClientBuilder knobs are the defaults:

Knob (default)Middleware equivalent (composable)
retries / retry_boundsRetryMiddleware (feature retry, default-on)
with_breakerCircuitBreakerMiddleware (feature circuit-breaker)
—ThrottleMiddleware (feature throttle)
—TimeoutMiddleware (feature timeout)
base_url (eager)BaseUrlMiddleware (feature base-url)

crate::ClientBuilder::with_middleware registers custom middleware outermost — wrapping the built-ins — so user middleware observe the final outcome of the whole default stack.

Structs§

ClientBuilder
Builder for ClientWithMiddleware: wrap a reqwest::Client with middleware, first-registered = outermost.
ClientWithMiddleware
A reqwest::Client wrapped in a middleware chain.
Next
The continuation passed to Middleware::handle.
RequestBuilder
A request builder bound to a ClientWithMiddleware.
RetryConfig
Configuration for retry behavior.
RetryMiddleware
Retry middleware wrapping the remainder of the chain in the estate’s loop-retry backoff loop.

Enums§

Error
Error type produced by the middleware stack.

Traits§

Middleware
Async middleware that wraps a ClientWithMiddleware.

Functions§

is_retryable_error
Whether err is retried by RetryMiddleware: transport-level timeouts and connection failures. Middleware errors are permanent by design — a short-circuit (e.g. an open circuit breaker) must not be masked by retries.
is_retryable_status
Whether status is retried by RetryMiddleware: 408 Request Timeout, 429 Too Many Requests, and 5xx except 501 Not Implemented.

Type Aliases§

Result
Result alias used throughout the middleware stack.