Skip to main content

Backoff

Struct Backoff 

Source
pub struct Backoff {
    pub initial: Duration,
    pub multiplier: f64,
    pub max: Duration,
    pub jitter: Jitter,
}
Expand description

Exponential-backoff configuration.

delay(attempt) returns min(initial × multiplier^attempt, max), then applies the configured Jitter policy.

All fields are pub so you can construct + tune directly, or use one of the preset constructors (Backoff::smtp_outbound, Backoff::auth_lockout, etc.).

Fields§

§initial: Duration

Delay for attempt = 0.

§multiplier: f64

Geometric growth factor between attempts. Common values: 2.0 (doubling) or 1.5 (gentler ramp).

§max: Duration

Hard ceiling on delay — once initial × multiplier^attempt would exceed max, return max.

§jitter: Jitter

Jitter policy applied to the computed delay.

Implementations§

Source§

impl Backoff

Source

pub fn new( initial: Duration, multiplier: f64, max: Duration, jitter: Jitter, ) -> Self

Construct with explicit values. Use the preset constructors below for common shapes.

Source

pub fn smtp_outbound() -> Self

Preset for SMTP outbound delivery retries: 60s initial, 2.5× growth, 8-hour cap, Full jitter. Mirrors the legacy mailrs-outbound-queue schedule shape.

Source

pub fn auth_lockout() -> Self

Preset for failed-auth lockout: 30min initial, 2× growth, 24-hour cap, None jitter (lockouts are deterministic by design — you want every offender to see exactly the same penalty).

Source

pub fn webhook() -> Self

Preset for webhook delivery retries: 60s initial, 2× growth, 6-hour cap, Equal jitter. Smoother than Full jitter so subscriber endpoints see a less spiky retry pattern.

Source

pub fn base_delay(&self, attempt: u32) -> Duration

Base delay for attempt BEFORE jitter is applied. Useful when you want to log “scheduled delay” without the random part.

Source

pub fn delay(&self, attempt: u32, seed: u64) -> Duration

Compute the delay for attempt, applying the configured jitter. seed is a caller-supplied source of randomness — usually derived from a fresh rand::random::<u64>() or std::time::Instant::now().elapsed().as_nanos(). The crate itself has no RNG dependency.

For Jitter::None the seed is ignored. For Equal/Full it drives a deterministic mapping (same seed → same delay), so tests can be reproducible.

Source

pub fn should_give_up(attempt: u32, max_attempts: u32) -> bool

Convenience: should this attempt give up? Returns true if attempt >= max_attempts.

Trait Implementations§

Source§

impl Clone for Backoff

Source§

fn clone(&self) -> Backoff

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Backoff

Source§

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

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

impl Default for Backoff

Source§

fn default() -> Self

Default: 1 second initial, 2× growth, 1 hour cap, Full jitter. Reasonable for generic HTTP/RPC retry loops; pick a preset for other use cases.

Source§

impl Copy for Backoff

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, 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.