Struct ExponentialBackoffConfig

Source
pub struct ExponentialBackoffConfig {
    pub max_retries: i32,
    pub t_wait: Duration,
    pub backoff: f64,
    pub t_wait_max: Option<Duration>,
    pub backoff_max: Option<Duration>,
}
Expand description

Configuration for an exponential backoff, allowing control over the entire strategy.

This will retry a failing operation up to max_tries, waiting for a duration of t_wait on the first failure and t_wait * backoff**attempt for all remaining attempts. The maximum single wait can be capped by backoff_max, and the total waiting time before failing can be set with t_wait_max.

The behavior of t_wait_max is such that the function guarantees it will not begin sleeping if sleeping would cause the function to exceed a total execution time of t_wait_max. It is however possible for the execution to exceed t_wait_max if the decorated code (e.g. calling an API) causes it to exceed this time.

§Example: Classic Exponential Backoff

This backoff configuration will retry up to 5 times, waiting 1 second at first, then 2 seconds, 4 seconds, etc.


const BACKOFF_CONFIG: ExponentialBackoffConfig = ExponentialBackoffConfig {
    max_retries: 5,
    t_wait: Duration::from_secs(1),
    backoff: 2.0,
    t_wait_max: None,
    backoff_max: None,
};

§Example: Constant Linear Retries

This configuration will retry up to five times, with no exponential behavior, since the backoff exponent is 1.0. It will wait 1 second for all retry attempts.


const BACKOFF_CONFIG: ExponentialBackoffConfig = ExponentialBackoffConfig {
    max_retries: 5,
    t_wait: Duration::from_secs(1),
    backoff: 1.0,
    t_wait_max: None,
    backoff_max: None,
};

§Example: Backoff With a Maximum Wait Time

This backoff configuration will retry up to 25 times, doubling the wait with each retry.

Setting t_wait_max to 10 minutes means that regardless of the number of retries or backoff exponent, it will return in 10 minutes or less.


const BACKOFF_CONFIG: ExponentialBackoffConfig = ExponentialBackoffConfig {
    max_retries: 25,
    t_wait: Duration::from_secs(1),
    backoff: 2.0,
    t_wait_max: Some(Duration::from_secs(600)),
    backoff_max: None,
};

§Example: Limited Backoff

This backoff configuration will retry up to 15 times, waiting 1 second at first, then 2 seconds, 4 seconds, etc.

Setting backoff_max to 30 seconds ensures it will exponentially back off until reaching a 30s wait time, then will continue to retry every 30s until it succeeds, exhausts retries, or hits t_wait_max (not configured in this case).


const BACKOFF_CONFIG: ExponentialBackoffConfig = ExponentialBackoffConfig {
    max_retries: 15,
    t_wait: Duration::from_secs(1),
    backoff: 2.0,
    t_wait_max: None,
    backoff_max: Some(Duration::from_secs(30)),
};

Fields§

§max_retries: i32

maximum number of retry attempts to make

§t_wait: Duration

initial duration to wait

§backoff: f64

backoff exponent, e.g. 2.0 for a classic exponential backoff

§t_wait_max: Option<Duration>

maximum time to attempt retries before returning the last result

§backoff_max: Option<Duration>

maximum time to wait for any single retry, i.e. backoff exponentially up to this duration, then wait in constant time of backoff_max

Trait Implementations§

Source§

impl Clone for ExponentialBackoffConfig

Source§

fn clone(&self) -> ExponentialBackoffConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for ExponentialBackoffConfig

Source§

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

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

impl PartialEq for ExponentialBackoffConfig

Source§

fn eq(&self, other: &ExponentialBackoffConfig) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for ExponentialBackoffConfig

Source§

impl StructuralPartialEq for ExponentialBackoffConfig

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, 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> 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 = 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.