BackoffStrategy

Struct BackoffStrategy 

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

Configuration type used to override default backoff strategy.

The BackoffStrategy type includes high-level associated functions to construct a configuration using a preset strategy.

use selium::keep_alive::BackoffStrategy;

let linear = BackoffStrategy::linear();
let constant = BackoffStrategy::constant();
let exponential = BackoffStrategy::exponential(2);

The preset strategy can then be tweaked further by overriding the default settings provided for the max attempts, max retry duration, and duration step.

use selium::keep_alive::BackoffStrategy;
use std::time::Duration;

let tweaked_linear = BackoffStrategy::linear()
    .with_max_attempts(5)
    .with_max_duration(Duration::from_secs(3))
    .with_step(Duration::from_secs(1));

Implementations§

Source§

impl BackoffStrategy

Source

pub fn linear() -> Self

Constructs a new RetryStrategy instance with a linear preset.

A linear retry strategy will increase each successive attempt linearly by the provided (or default if not provided) step.

§Examples

The following strategy will produce a sequence of Duration values equivalent to 2s, 4s, 6s, 8s, 10s.

BackoffStrategy::linear()
    .with_max_attempts(5)
    .with_step(Duration::from_secs(2));
Source

pub fn constant() -> Self

Constructs a new RetryStrategy instance with a constant preset.

A constant retry strategy will not increase the duration of each successive retry.

§Examples

The following strategy will produce a sequence of Duration values equivalent to 2s, 2s, 2s, 2s, 2s.

BackoffStrategy::constant()
    .with_max_attempts(5)
    .with_step(Duration::from_secs(2));
Source

pub fn exponential(factor: u64) -> Self

Constructs a new RetryStrategy instance with an exponential preset.

An exponential retry strategy will increase each successive attempt exponentially using the provided (or default if not provided) step and exponential factor.

§Examples

The following strategy will produce a sequence of Duration values equivalent to 2s, 4s, 8s, 16s, 32s.

BackoffStrategy::exponential(2)
    .with_max_attempts(5)
    .with_step(Duration::from_secs(2));
Source

pub fn with_max_attempts(self, attempts: u32) -> Self

Overrides the default max attempts value to specify the amount of Duration values that will be produced by the iterator.

§Examples

The following will produce a sequence equivalent to 2s, 4s, 6s

BackoffStrategy::linear()
    .with_max_attempts(3)
    .with_step(Duration::from_secs(2));

whereas the following will produce a sequence equivalent to 2s, 4s, 6s, 8s, 10s

BackoffStrategy::linear()
    .with_max_attempts(5) // Increased max attempts!
    .with_step(Duration::from_secs(2));
Source

pub fn with_max_duration(self, max: Duration) -> Self

Specifies a max duration to clamp produced Durations to a maximum value. Can be useful when creating an exponential strategy with a high amount of retries to keep the Duration values within reasonable boundaries.

§Examples

The following, uncapped exponential strategy will produce a sequence equivalent to 2s, 4s, 8s, 16s, 32s, 64s, which can be quite unwieldy.

BackoffStrategy::exponential(2)
    .with_max_attempts(6)
    .with_step(Duration::from_secs(2));

whereas the following, capped exponential strategy would produce a sequence equivalent to 2s, 4s, 8s, 8s, 8s, 8s

BackoffStrategy::exponential(2)
    .with_max_attempts(6)
    .with_max_duration(Duration::from_secs(8)) // Capped to 8 seconds maximum!
    .with_step(Duration::from_secs(2));
Source

pub fn with_step(self, step: Duration) -> Self

Overrides the default step to use for each strategy. Depending on the strategy, this will have different results.

  • For linear strategies, the step is used to increase the duration for each successive attempt.
  • For constant strategies, the step is used as a constant duration for every attempt.
  • For exponential strategies, each attempt is increased exponentially using the product of the step and an exponential factor
§Examples

The following linear strategy will produce a sequence equivalent to 2s, 4s, 6s, 8s, 10s

BackoffStrategy::linear()
    .with_max_attempts(5) // Increased max attempts!
    .with_step(Duration::from_secs(2));

whereas the following strategy will produce a sequence equivalent to 4s, 8s, 12s, 16s, 20s

BackoffStrategy::linear()
    .with_max_attempts(5)
    .with_step(Duration::from_secs(4)); // Increased step!

Trait Implementations§

Source§

impl Clone for BackoffStrategy

Source§

fn clone(&self) -> BackoffStrategy

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 BackoffStrategy

Source§

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

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

impl Default for BackoffStrategy

Source§

fn default() -> BackoffStrategy

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

impl IntoIterator for BackoffStrategy

Consumes the BackoffStrategy instance to create an iterator that produces Duration values representing a sequence of retry attempts.

Source§

type Item = NextAttempt

The type of the elements being iterated over.
Source§

type IntoIter = BackoffStrategyIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. 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> 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.
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