pub struct ExponentialBackoff { /* private fields */ }Implementations§
Source§impl ExponentialBackoff
An exponential backoff mechanism with optional jitter and immediate-first behavior.
impl ExponentialBackoff
An exponential backoff mechanism with optional jitter and immediate-first behavior.
The backoff starts at an initial delay, multiplies that delay by a factor after each call, and
caps it at the configured maximum. Each result includes bounded random jitter. When
immediate_first is true, the first call to Self::next_duration returns zero. Calling
Self::reset restores both the initial delay and the original immediate-first setting.
Sourcepub fn new(
delay_initial: Duration,
delay_max: Duration,
factor: f64,
jitter_ms: u64,
immediate_first: bool,
) -> Result<Self>
pub fn new( delay_initial: Duration, delay_max: Duration, factor: f64, jitter_ms: u64, immediate_first: bool, ) -> Result<Self>
Creates a new [ExponentialBackoff] instance.
§Errors
Returns an error if:
delay_initialis zero.delay_maxis less thandelay_initial.delay_maxexceedsDuration::from_nanos(u64::MAX)(≈584 years).factoris not in the range [1.0, 100.0] (to prevent reconnect spam).
Sourcepub fn next_duration(&mut self) -> Duration
pub fn next_duration(&mut self) -> Duration
Returns the next backoff delay with jitter and updates the internal state.
If the immediate_first flag is set and this is the first call (i.e. the current
delay equals the initial delay), it returns Duration::ZERO to trigger an immediate
reconnect and disables the immediate behavior for subsequent calls.
Near the cap the jittered base is lowered to delay_max - jitter so
the spread survives saturation; the result is clamped into
[min(delay_initial, delay_max), delay_max].
Sourcepub const fn current_delay(&self) -> Duration
pub const fn current_delay(&self) -> Duration
Returns the current base delay without jitter.
This represents the delay that would be used as the base for the next call to next(),
before any jitter is applied.
Trait Implementations§
Source§impl Clone for ExponentialBackoff
impl Clone for ExponentialBackoff
Source§fn clone(&self) -> ExponentialBackoff
fn clone(&self) -> ExponentialBackoff
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more