Skip to main content

Backoff

Struct Backoff 

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

A seeded, jittered, capped exponential backoff, with a limit on attempts.

next_delay draws the next wait, or says the attempts are spent; reset starts again after a success. The state is the caller’s to keep between runs, so a run that returns and is driven again does not restart the doubling unless the caller says so.

§Examples

use core::num::NonZeroU32;
use core::time::Duration;

use happenstance_sync::runner::Backoff;

let cap = Duration::from_secs(30);
let mut backoff = Backoff::new(Duration::from_millis(100), cap, 0x5EED)?
    .with_attempts(NonZeroU32::MIN.saturating_add(2));

let drawn: Vec<Duration> = core::iter::from_fn(|| backoff.next_delay()).collect();
assert_eq!(drawn.len(), 3, "three attempts, then spent");
assert!(drawn.iter().all(|delay| *delay <= cap));

Implementations§

§

impl Backoff

pub const DEFAULT_ATTEMPTS: NonZeroU32

How many delays a backoff draws before it is spent, unless with_attempts says otherwise. Ten doublings of a 100 ms base, capped at 30 s, wait about a minute in all.

pub const fn new( base: Duration, cap: Duration, seed: u64, ) -> Result<Self, BackoffError>

A backoff whose first delay is at most base, doubling to at most cap, jittered from seed, spent after DEFAULT_ATTEMPTS.

A zero cap (and so a zero base) is accepted: every delay is zero, and a run retries without waiting until the attempts are spent. That suits a test or a caller that paces retries itself; a deployment wants a cap above zero.

§Errors

BackoffError::BaseAboveCap if base exceeds cap.

pub const fn with_attempts(self, attempts: NonZeroU32) -> Self

The same backoff, spent after attempts delays.

pub fn next_delay(&mut self) -> Option<Duration>

The next delay, or None once the attempts are spent.

The n-th delay since the last reset (from zero) is drawn uniformly from [c/2, c] for c = min(cap, base · 2ⁿ), so none exceeds the cap.

pub const fn reset(&mut self)

Starts the doubling and the attempt count again, after a success. The generator keeps its place, so the next sequence is not the last one.

Trait Implementations§

§

impl Clone for Backoff

§

fn clone(&self) -> Self

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
§

impl Debug for Backoff

§

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> 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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.