Skip to main content

Policy

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>>;
}
Available on crate features 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>

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)

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

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

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>>

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".

Implementations on Foreign Types§

§

impl<P, R, E> Policy<R, E> for &'static P
where P: Policy<R, E>,

§

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>>

Implementors§

§

impl<A, B, C, D, E, F, G, H, I, Response, Error> Policy<Response, Error> for Either9<A, B, C, D, E, F, G, H, I>
where A: Policy<Response, Error>, B: Policy<Response, Error>, C: Policy<Response, Error>, D: Policy<Response, Error>, E: Policy<Response, Error>, F: Policy<Response, Error>, G: Policy<Response, Error>, H: Policy<Response, Error>, I: Policy<Response, Error>, Response: Send + 'static, Error: Send + 'static,

§

impl<A, B, C, D, E, F, G, H, Response, Error> Policy<Response, Error> for Either8<A, B, C, D, E, F, G, H>
where A: Policy<Response, Error>, B: Policy<Response, Error>, C: Policy<Response, Error>, D: Policy<Response, Error>, E: Policy<Response, Error>, F: Policy<Response, Error>, G: Policy<Response, Error>, H: Policy<Response, Error>, Response: Send + 'static, Error: Send + 'static,

§

impl<A, B, C, D, E, F, G, Response, Error> Policy<Response, Error> for Either7<A, B, C, D, E, F, G>
where A: Policy<Response, Error>, B: Policy<Response, Error>, C: Policy<Response, Error>, D: Policy<Response, Error>, E: Policy<Response, Error>, F: Policy<Response, Error>, G: Policy<Response, Error>, Response: Send + 'static, Error: Send + 'static,

§

impl<A, B, C, D, E, F, Response, Error> Policy<Response, Error> for Either6<A, B, C, D, E, F>
where A: Policy<Response, Error>, B: Policy<Response, Error>, C: Policy<Response, Error>, D: Policy<Response, Error>, E: Policy<Response, Error>, F: Policy<Response, Error>, Response: Send + 'static, Error: Send + 'static,

§

impl<A, B, C, D, E, Response, Error> Policy<Response, Error> for Either5<A, B, C, D, E>
where A: Policy<Response, Error>, B: Policy<Response, Error>, C: Policy<Response, Error>, D: Policy<Response, Error>, E: Policy<Response, Error>, Response: Send + 'static, Error: Send + 'static,

§

impl<A, B, C, D, Response, Error> Policy<Response, Error> for Either4<A, B, C, D>
where A: Policy<Response, Error>, B: Policy<Response, Error>, C: Policy<Response, Error>, D: Policy<Response, Error>, Response: Send + 'static, Error: Send + 'static,

§

impl<A, B, C, Response, Error> Policy<Response, Error> for Either3<A, B, C>
where A: Policy<Response, Error>, B: Policy<Response, Error>, C: Policy<Response, Error>, Response: Send + 'static, Error: Send + 'static,

§

impl<A, B, Response, Error> Policy<Response, Error> for Either<A, B>
where A: Policy<Response, Error>, B: Policy<Response, Error>, Response: Send + 'static, Error: Send + 'static,

§

impl<B, C, R, P, S, Response, Error> Policy<Response, Error> for ManagedPolicy<B, C, R, P, S>
where B: Backoff, P: PrepareBody, S: ReplayStore, C: CloneInput, R: RetryRule<Request<RetryBody>, Response, Error>, Response: Send + 'static, Error: Send + 'static,

§

impl<P, R, E> Policy<R, E> for Arc<P>
where P: Policy<R, E>,