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
impl BackoffStrategy
Sourcepub fn linear() -> Self
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));Sourcepub fn constant() -> Self
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));Sourcepub fn exponential(factor: u64) -> Self
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));Sourcepub fn with_max_attempts(self, attempts: u32) -> Self
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));Sourcepub fn with_max_duration(self, max: Duration) -> Self
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));Sourcepub fn with_step(self, step: Duration) -> Self
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
linearstrategies, thestepis used to increase the duration for each successive attempt. - For
constantstrategies, thestepis used as a constant duration for every attempt. - For
exponentialstrategies, each attempt is increased exponentially using the product of thestepand an exponentialfactor
§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
impl Clone for BackoffStrategy
Source§fn clone(&self) -> BackoffStrategy
fn clone(&self) -> BackoffStrategy
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for BackoffStrategy
impl Debug for BackoffStrategy
Source§impl Default for BackoffStrategy
impl Default for BackoffStrategy
Source§fn default() -> BackoffStrategy
fn default() -> BackoffStrategy
Source§impl IntoIterator for BackoffStrategy
Consumes the BackoffStrategy instance to create an iterator that produces Duration values
representing a sequence of retry attempts.
impl IntoIterator for BackoffStrategy
Consumes the BackoffStrategy instance to create an iterator that produces Duration values
representing a sequence of retry attempts.