Skip to main content

Backoff

Struct Backoff 

Source
pub struct Backoff { /* private fields */ }
Available on crate feature std only.
Expand description

A backoff policy: a base curve plus a jitter mode and a delay ceiling.

Construct with constant, linear, or exponential, then tune with with_max and with_jitter. It is usable on its own — pair it with Retry, or call iter and drive your own loop.

§Examples

use std::time::Duration;
use throttle_net::{Backoff, Jitter};

// Exponential from 50ms, doubling, capped at 5s, with full jitter.
let backoff = Backoff::exponential(Duration::from_millis(50), 2.0)
    .with_max(Duration::from_secs(5))
    .with_jitter(Jitter::Full);

let mut delays = backoff.iter();
let first = delays.next_delay();
assert!(first <= Duration::from_millis(50)); // full jitter: in [0, 50ms]

Implementations§

Source§

impl Backoff

Source

pub fn constant(delay: Duration) -> Self

A fixed delay on every attempt, with no jitter.

§Examples
use std::time::Duration;
use throttle_net::Backoff;

let mut delays = Backoff::constant(Duration::from_millis(200)).iter();
assert_eq!(delays.next_delay(), Duration::from_millis(200));
assert_eq!(delays.next_delay(), Duration::from_millis(200));
Source

pub fn linear(initial: Duration, increment: Duration) -> Self

A delay that grows by increment each attempt: initial, initial + increment, initial + 2*increment, … capped at the maximum.

§Examples
use std::time::Duration;
use throttle_net::Backoff;

let mut delays = Backoff::linear(Duration::from_millis(100), Duration::from_millis(100)).iter();
assert_eq!(delays.next_delay(), Duration::from_millis(100));
assert_eq!(delays.next_delay(), Duration::from_millis(200));
assert_eq!(delays.next_delay(), Duration::from_millis(300));
Source

pub fn exponential(initial: Duration, factor: f64) -> Self

A delay that multiplies by factor each attempt: initial, initial * factor, initial * factor^2, … capped at the maximum.

A factor of 2.0 doubles each time. Non-finite or sub-one factors are accepted but make the curve flat or shrinking; pair with jitter for the usual behavior.

§Examples
use std::time::Duration;
use throttle_net::Backoff;

let mut delays = Backoff::exponential(Duration::from_millis(100), 2.0).iter();
assert_eq!(delays.next_delay(), Duration::from_millis(100));
assert_eq!(delays.next_delay(), Duration::from_millis(200));
assert_eq!(delays.next_delay(), Duration::from_millis(400));
Source

pub fn with_max(self, max: Duration) -> Self

Returns a copy with the delay ceiling set to max. No delay this backoff produces will exceed it.

Source

pub fn with_jitter(self, jitter: Jitter) -> Self

Returns a copy with the jitter mode set.

Source

pub const fn max(&self) -> Duration

The configured delay ceiling.

Source

pub const fn jitter(&self) -> Jitter

The configured jitter mode.

Source

pub fn iter(&self) -> BackoffIter

Starts a delay sequence, seeded from system entropy.

Each call returns an independent BackoffIter with its own random state, so two retry loops sharing one policy still jitter independently.

Source

pub fn iter_seeded(&self, seed: u64) -> BackoffIter

Starts a delay sequence with an explicit seed, for deterministic, reproducible tests.

§Examples
use std::time::Duration;
use throttle_net::{Backoff, Jitter};

let backoff = Backoff::exponential(Duration::from_millis(10), 2.0)
    .with_jitter(Jitter::Full);
// The same seed always yields the same sequence.
let a: Vec<_> = (0..5).scan(backoff.iter_seeded(7), |it, _| Some(it.next_delay())).collect();
let b: Vec<_> = (0..5).scan(backoff.iter_seeded(7), |it, _| Some(it.next_delay())).collect();
assert_eq!(a, b);

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 Copy for Backoff

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

Exponential from 100ms, doubling, capped at 30s, with decorrelated jitter — a sane production default that resists thundering herds.

Source§

impl PartialEq for Backoff

Source§

fn eq(&self, other: &Backoff) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
Source§

impl<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error