Skip to main content

Backoff

Struct Backoff 

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

Delays between attempts: first, doubling each attempt up to max, each one drawn from the upper half of its step when jitter is on, so retries spread out but never come faster than half the step (D-142).

use net_io::Backoff;
use std::time::Duration;

let mut b = Backoff::new()
    .first(Duration::from_millis(100))
    .max(Duration::from_secs(2))
    .jitter(false)
    .attempts(3);
assert_eq!(b.next_delay(), Some(Duration::from_millis(100)));
assert_eq!(b.next_delay(), Some(Duration::from_millis(200)));
assert_eq!(b.next_delay(), Some(Duration::from_millis(400)));
assert_eq!(b.next_delay(), None);
b.reset();
assert_eq!(b.attempt(), 0);

Implementations§

Source§

impl Backoff

Source

pub fn new() -> Backoff

100 ms doubling to 30 s, jitter on, no limit on attempts or time.

Source

pub fn first(self, d: Duration) -> Backoff

The first delay (at least 1 ms).

Source

pub fn max(self, d: Duration) -> Backoff

The longest delay (at least first).

Source

pub fn jitter(self, on: bool) -> Backoff

Whether each delay is drawn from the upper half of its step (on by default).

Source

pub fn attempts(self, n: u32) -> Backoff

At most n delays; then Backoff::next_delay returns None.

Source

pub fn give_up_after(self, total: Duration) -> Backoff

next_delay returns None once waiting the next delay would end later than total after the first call.

Source

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

The delay before the next attempt, or None when the attempts or the time are spent.

Source

pub fn attempt(&self) -> u32

Delays handed out since the last reset.

Source

pub fn reset(&mut self)

Back to the first delay, with the time limit counted again from the next call.

Trait Implementations§

Source§

impl Clone for Backoff

Source§

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
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() -> Backoff

Returns the “default value” for a type. 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.