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.
This struct computes successive delays for reconnect attempts.
It starts from an initial delay and multiplies it by a factor on each iteration,
capping the delay at a maximum value. Random jitter is added (up to a configured
maximum) to the delay. When immediate_first
is true, the first call to next_duration
returns zero delay, triggering an immediate reconnect, after which the immediate flag is disabled.
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_initial
is zero.delay_max
is less thandelay_initial
.factor
is 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
Return the next backoff delay with jitter and update 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.
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 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for ExponentialBackoff
impl RefUnwindSafe for ExponentialBackoff
impl Send for ExponentialBackoff
impl Sync for ExponentialBackoff
impl Unpin for ExponentialBackoff
impl UnwindSafe for ExponentialBackoff
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more