Struct ExponentialBackoffBuilder

Source
pub struct ExponentialBackoffBuilder { /* private fields */ }
Expand description

Builds an exponential backoff policy.

§Example

use retry_policies::{RetryDecision, RetryPolicy, Jitter};
use retry_policies::policies::ExponentialBackoff;
use std::time::Duration;

let backoff = ExponentialBackoff::builder()
    .retry_bounds(Duration::from_secs(1), Duration::from_secs(60))
    .jitter(Jitter::Bounded)
    .base(2)
    .build_with_total_retry_duration(Duration::from_secs(24 * 60 * 60));

Implementations§

Source§

impl ExponentialBackoffBuilder

Source

pub fn retry_bounds( self, min_retry_interval: Duration, max_retry_interval: Duration, ) -> ExponentialBackoffBuilder

Add min & max retry interval bounds. Default [1s, 30m].

See ExponentialBackoff::min_retry_interval, ExponentialBackoff::max_retry_interval.

Panics if min_retry_interval > max_retry_interval.

Source

pub fn jitter(self, jitter: Jitter) -> ExponentialBackoffBuilder

Set what type of jitter to apply.

Source

pub fn base(self, base: u32) -> ExponentialBackoffBuilder

Set what base to use for the exponential.

Source

pub fn build_with_max_retries(self, n: u32) -> ExponentialBackoff

Builds an ExponentialBackoff with the given maximum retries.

See ExponentialBackoff::max_n_retries.

Source

pub fn build_with_total_retry_duration( self, total_duration: Duration, ) -> ExponentialBackoffTimed

Builds an ExponentialBackoff with the given maximum total duration for which retries will continue to be performed.

§Example
use retry_policies::{RetryDecision, RetryPolicy};
use retry_policies::policies::ExponentialBackoff;
use std::time::{Duration, SystemTime};

let backoff = ExponentialBackoff::builder()
    .build_with_total_retry_duration(Duration::from_secs(24 * 60 * 60));

let started_at = SystemTime::now()
    .checked_sub(Duration::from_secs(25 * 60 * 60))
    .unwrap();

let should_retry = backoff.should_retry(started_at, 0);
assert!(matches!(RetryDecision::DoNotRetry, should_retry));
Source

pub fn build_with_total_retry_duration_and_limit_retries( self, total_duration: Duration, ) -> ExponentialBackoffTimed

Builds an ExponentialBackoffTimed with the given maximum total duration and limits the number of retries to a calculated maximum.

This calculated maximum is based on how many attempts would be made without jitter applied.

For example if we set total duration 24 hours, with retry bounds [1s, 24h] and 2 as base of the exponential, we would calculate 17 max retries, as 1s * pow(2, 16) = 65536s = ~18 hours and 18th attempt would be way after the 24 hours total duration.

If the 17th retry ends up being scheduled after 10 hours due to jitter, ExponentialBackoff::should_retry will return false anyway: no retry will be allowed after total duration.

If one of the 17 allowed retries for some reason (e.g. previous attempts taking a long time) ends up being scheduled after total duration, ExponentialBackoff::should_retry will return false.

Basically we will enforce whatever comes first, max retries or total duration.

§Example
use retry_policies::{RetryDecision, RetryPolicy};
use retry_policies::policies::ExponentialBackoff;
use std::time::{Duration, SystemTime};

let exponential_backoff_timed = ExponentialBackoff::builder()
    .retry_bounds(Duration::from_secs(1), Duration::from_secs(6 * 60 * 60))
    .build_with_total_retry_duration_and_limit_retries(Duration::from_secs(24 * 60 * 60));

assert_eq!(exponential_backoff_timed.max_retries(), Some(17));

let started_at = SystemTime::now()
    .checked_sub(Duration::from_secs(25 * 60 * 60))
    .unwrap();

let should_retry = exponential_backoff_timed.should_retry(started_at, 0);
assert!(matches!(RetryDecision::DoNotRetry, should_retry));

let started_at = SystemTime::now()
    .checked_sub(Duration::from_secs(1 * 60 * 60))
    .unwrap();

let should_retry = exponential_backoff_timed.should_retry(started_at, 18);
assert!(matches!(RetryDecision::DoNotRetry, should_retry));
Source

pub fn build_with_total_retry_duration_and_max_retries( self, total_duration: Duration, max_n_retries: u32, ) -> ExponentialBackoffTimed

Builds an ExponentialBackoffTimed with the given maximum total duration and maximum retries.

Trait Implementations§

Source§

impl Default for ExponentialBackoffBuilder

Source§

fn default() -> ExponentialBackoffBuilder

Returns the “default value” for a type. 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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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> ServiceExt for T

Source§

fn map_response_body<F>(self, f: F) -> MapResponseBody<Self, F>
where Self: Sized,

Apply a transformation to the response body. Read more
Source§

fn decompression(self) -> Decompression<Self>
where Self: Sized,

Decompress response bodies. Read more
Source§

fn trace_for_http(self) -> Trace<Self, SharedClassifier<ServerErrorsAsFailures>>
where Self: Sized,

High level tracing that classifies responses using HTTP status codes. Read more
Source§

fn trace_for_grpc(self) -> Trace<Self, SharedClassifier<GrpcErrorsAsFailures>>
where Self: Sized,

High level tracing that classifies responses using gRPC headers. Read more
Source§

fn follow_redirects(self) -> FollowRedirect<Self>
where Self: Sized,

Follow redirect resposes using the Standard policy. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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