Trait Policy
pub trait Policy<R, E>:
Send
+ Sync
+ 'static {
// Required methods
fn prepare_body(
&self,
parts: &Parts,
is_end_stream: bool,
size_hint: &SizeHint,
) -> Option<CollectOptions>;
fn replay_store(&self, parts: &Parts) -> &(dyn ReplayStore + 'static);
fn retry(
&self,
req: Request<RetryBody>,
result: Result<R, E>,
) -> impl Future<Output = PolicyResult<R, E>> + Send;
fn next_attempt(&self) -> impl Future<Output = bool> + Send;
fn clone_input(
&self,
req: &Request<RetryBody>,
) -> Option<Request<RetryBody>>;
}http and std only.Expand description
A “retry policy” to classify if a request should be retried.
ManagedPolicy implements this trait from plain
functions. A custom policy that keeps the defaults returns
MemoryReplay::DEFAULT from
Self::replay_store and true from Self::next_attempt.
Required Methods§
fn prepare_body(
&self,
parts: &Parts,
is_end_stream: bool,
size_hint: &SizeHint,
) -> Option<CollectOptions>
fn prepare_body( &self, parts: &Parts, is_end_stream: bool, size_hint: &SizeHint, ) -> Option<CollectOptions>
Decide whether to prepare a replay before polling the request body.
None streams the request once, without cloning it or calling retry.
These options can tighten the selected store’s byte limit and timeout.
Exceeding a limit disables retries while the first upload continues.
is_end_stream is the body’s own assertion that no frames remain.
A zero size hint alone does not exclude trailers. Each policy explicitly
chooses whether collection is allowed and which limits apply.
fn replay_store(&self, parts: &Parts) -> &(dyn ReplayStore + 'static)
fn replay_store(&self, parts: &Parts) -> &(dyn ReplayStore + 'static)
Select storage for an approved, nonempty body before it is first polled.
A store can be selected using request metadata.
MemoryReplay::DEFAULT
keeps up to 64 KiB in memory with a 30-second deadline.
fn retry(
&self,
req: Request<RetryBody>,
result: Result<R, E>,
) -> impl Future<Output = PolicyResult<R, E>> + Send
fn retry( &self, req: Request<RetryBody>, result: Result<R, E>, ) -> impl Future<Output = PolicyResult<R, E>> + Send
Classify the result of an attempt and optionally prepare the next request.
Returning PolicyResult::Abort forwards the result immediately.
Returning PolicyResult::Retry asks the middleware to finish any
outstanding capture before issuing the next request. Supply the current
result as the fallback if the body cannot be recovered. Reading the
candidate body inside this method also requests that capture be finished.
While the first attempt’s reader is still alive, finishing waits up to
the store’s handoff_timeout
for that reader to be dropped or to reach the end of the upload.
§Mutating Requests
The policy MAY chose to mutate the req: if the request is mutated, the
mutated request will be sent to the inner service in the next retry.
This can be helpful for use cases like tracking the retry count in a
header. Mutate the handed-in request in place — its
Extensions are the caller’s
store, so recorded metadata both reaches the next attempt and stays
visible to the caller after the call. Returning a freshly built request
instead keeps its body and headers but not its extensions: each attempt
is re-forked from the caller’s store (see the
module docs).
§Mutating Results
The policy MAY chose to mutate the result. This enables the retry policy to convert a failure into a success and vice versa. For example, if the policy is used to poll while waiting for a state change, the policy can switch the result to emit a specific error when retries are exhausted.
As above, record such metadata on the handed-in request (e.g. the number of retries required, or that a failure exhausted all retries).
fn next_attempt(&self) -> impl Future<Output = bool> + Send
fn next_attempt(&self) -> impl Future<Output = bool> + Send
Apply the retry delay or attempt budget after a complete body is ready.
Return false to return the candidate outcome to the caller. Keeping
backoff here allows a pending upload to finish before a delay begins.
fn clone_input(&self, req: &Request<RetryBody>) -> Option<Request<RetryBody>>
fn clone_input(&self, req: &Request<RetryBody>) -> Option<Request<RetryBody>>
Tries to clone a request before being passed to the inner service.
Return None to skip retry classification and abandon capture. Use
RetryBody::try_clone to retain a candidate while a streamed upload
is still being captured. Capture can later exceed its limits.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".