Skip to main content

Backoff

Struct Backoff 

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

Exponential backoff with optional jitter and deadline.

Tracks current delay, retry count, and optional wall-clock deadline. Call .wait(err).await to check exhaustion, sleep if retries remain, and advance state. Returns Err(Exhausted(err)) when done.

Implementations§

Source§

impl Backoff

Source

pub fn builder() -> BackoffBuilder

Create a builder.

Source

pub async fn wait<E>(&mut self, err: E) -> Result<(), Exhausted<E>>

Check exhaustion, sleep for the current delay, then advance.

Returns Ok(()) if retries remain (caller should loop). Returns Err(Exhausted(err)) if max retries or deadline reached — the caller should escalate or fail over.

If a deadline is set, the sleep is capped to not exceed it.

§Panics

Panics if called outside Runtime::block_on.

Source

pub fn advance(&mut self)

Advance the backoff state without sleeping.

Useful when the caller manages timing externally.

Source

pub fn is_exhausted(&self) -> bool

Whether retries are exhausted (max retries or deadline).

Returns false if neither limit is set.

Source

pub fn retries(&self) -> u32

Current retry count.

Source

pub fn current_delay(&self) -> Duration

The next delay (before jitter).

Source

pub fn remaining(&self) -> Option<Duration>

Time remaining until deadline, if one is set.

Source

pub fn reset(&mut self)

Reset delay and retry count to initial state.

The deadline is not reset — it is wall-clock absolute.

Trait Implementations§

Source§

impl Debug for Backoff

Source§

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

Formats the value using the given formatter. 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> 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, 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.