Skip to main content

RetryPolicy

Struct RetryPolicy 

Source
pub struct RetryPolicy { /* private fields */ }
Available on crate feature retry only.
Expand description

Retries 429 Too Many Requests, 408 Request Timeout and 5xx responses, and failures to connect.

  • It waits as long as the server asks, with retry-after-ms (milliseconds) or Retry-After (seconds or an HTTP date), up to RetryPolicy::max_retry_after (60 s by default). The wait is read once into a deadline, counted from when the response arrived, and the policy sleeps until it: time spent in between (reading a 429 body for RetryLayer::permanent_429) is not added. A server that asks for longer than max_retry_after gets its response returned at once: waiting less than it asked would likely fail again, and waiting an hour is rarely what a caller wants. A date in the past means retry now.
  • With RetryPolicy::server_directed_429, a 429 is retried only when the server says when.
  • Without those headers it backs off exponentially from RetryPolicy::base_delay (200 ms), doubling on each retry up to RetryPolicy::max_delay (8 s), with jitter: each delay is picked at random between half and all of it, so that clients that failed together do not retry together. RetryPolicy::jitter turns that off.
  • A request that timed out (a TimeoutLayer inside the retries, or a reqwest timeout) is retried only with RetryPolicy::retry_timeouts: it may have reached the server.
  • It never retries a response marked Permanent (see RetryLayer::permanent_429), nor a request whose body is a stream (a streamed file upload), which cannot be sent again.

Implementations§

Source§

impl RetryPolicy

Source

pub fn new(max_retries: usize) -> Self

A policy that retries at most max_retries times (after the first attempt).

Source

pub fn base_delay(self, delay: Duration) -> Self

The first backoff delay, doubled on each retry (default 200 ms).

Source

pub fn max_delay(self, delay: Duration) -> Self

The longest backoff delay (default 8 s). It does not limit how long a server can ask to wait with Retry-After: that is RetryPolicy::max_retry_after.

Source

pub fn max_retry_after(self, delay: Duration) -> Self

The longest wait a server can ask for with Retry-After or retry-after-ms (default 60 s). A response that asks for longer is not retried but returned.

Source

pub fn jitter(self, jitter: bool) -> Self

Whether each backoff delay is picked at random between half and all of it (default true). The waits a server asks for are never jittered.

Source

pub fn server_directed_429(self, server_directed: bool) -> Self

Whether a 429 is retried only when the server says when, with Retry-After or retry-after-ms (default false: every 429 is retried, with backoff when the server does not say when).

Some APIs answer a 429 without advice when waiting will not help (a quota, a request too large for a rate limit), and with it when it will. 408, 5xx and connect failures are retried either way.

Source

pub fn retry_timeouts(self, retry: bool) -> Self

Whether a request that timed out is retried (default false), like a failure to connect: with a TimeoutLayer inside the retries, an attempt that takes too long is given up and tried again. A request that timed out may have reached the server, so turn this on for requests that are safe to repeat (or that the API deduplicates, with an idempotency key).

Source

pub fn max_retries(&self) -> usize

The number of retries allowed.

Trait Implementations§

Source§

impl Clone for RetryPolicy

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RetryPolicy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RetryPolicy

Three retries.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<B> Policy<Request<Body>, Response<B>, Box<dyn Error + Sync + Send>> for RetryPolicy

Source§

type Future = Pin<Box<dyn Future<Output = ()> + Send>>

The Future type returned by Policy::retry.
Source§

fn retry( &mut self, _request: &mut Request<Body>, result: &mut Result<Response<B>, BoxError>, ) -> Option<Self::Future>

Check the policy if a certain request should be retried. Read more
Source§

fn clone_request(&mut self, request: &Request<Body>) -> Option<Request<Body>>

Tries to clone a request before being passed to the inner service. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> MaybeSend for T
where T: Send + ?Sized,

Source§

impl<T> MaybeSync for T
where T: Sync + ?Sized,

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more