Struct Backoff

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

Exponential backoff type.

Implementations§

Source§

impl Backoff

Source

pub fn new( max_attempts: u32, min: Duration, max: impl Into<Option<Duration>>, ) -> Self

Create a new instance of Backoff.

§Examples

With an explicit max duration:

use exponential_backoff::Backoff;
use std::time::Duration;

let backoff = Backoff::new(3, Duration::from_millis(100), Duration::from_secs(10));
assert_eq!(backoff.max_attempts(), 3);
assert_eq!(backoff.min(), &Duration::from_millis(100));
assert_eq!(backoff.max(), &Duration::from_secs(10));

With no max duration (sets it to 584,942,417,355 years):

use exponential_backoff::Backoff;
use std::time::Duration;

let backoff = Backoff::new(5, Duration::from_millis(50), None);
assert_eq!(backoff.max(), &Duration::MAX);
Source

pub fn min(&self) -> &Duration

Get the min duration

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

let mut backoff = Backoff::default();
assert_eq!(backoff.min(), &Duration::from_millis(100));
Source

pub fn set_min(&mut self, min: Duration)

Set the min duration.

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

let mut backoff = Backoff::default();
backoff.set_min(Duration::from_millis(50));
assert_eq!(backoff.min(), &Duration::from_millis(50));
Source

pub fn max(&self) -> &Duration

Get the max duration

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

let mut backoff = Backoff::default();
assert_eq!(backoff.max(), &Duration::from_secs(10));
Source

pub fn set_max(&mut self, max: Duration)

Set the max duration.

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

let mut backoff = Backoff::default();
backoff.set_max(Duration::from_secs(30));
assert_eq!(backoff.max(), &Duration::from_secs(30));
Source

pub fn max_attempts(&self) -> u32

Get the maximum number of attempts

§Examples
use exponential_backoff::Backoff;

let mut backoff = Backoff::default();
assert_eq!(backoff.max_attempts(), 3);
Source

pub fn set_max_attempts(&mut self, max_attempts: u32)

Set the maximum number of attempts.

§Examples
use exponential_backoff::Backoff;

let mut backoff = Backoff::default();
backoff.set_max_attempts(5);
assert_eq!(backoff.max_attempts(), 5);
Source

pub fn jitter(&self) -> f32

Get the jitter factor

§Examples
use exponential_backoff::Backoff;

let mut backoff = Backoff::default();
assert_eq!(backoff.jitter(), 0.3);
Source

pub fn set_jitter(&mut self, jitter: f32)

Set the amount of jitter per backoff.

§Panics

This method panics if a number smaller than 0 or larger than 1 is provided.

§Examples
use exponential_backoff::Backoff;

let mut backoff = Backoff::default();
backoff.set_jitter(0.3);  // default value
backoff.set_jitter(0.0);  // min value
backoff.set_jitter(1.0);  // max value
Source

pub fn factor(&self) -> u32

Get the growth factor

§Examples
use exponential_backoff::Backoff;

let mut backoff = Backoff::default();
assert_eq!(backoff.factor(), 2);
Source

pub fn set_factor(&mut self, factor: u32)

Set the growth factor for each iteration of the backoff.

§Examples
use exponential_backoff::Backoff;

let mut backoff = Backoff::default();
backoff.set_factor(3);
assert_eq!(backoff.factor(), 3);
Source

pub fn iter(&self) -> IntoIter

Create an iterator.

Trait Implementations§

Source§

impl Clone for Backoff

Source§

fn clone(&self) -> Backoff

Returns a duplicate of the value. Read more
1.0.0 · 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

Implements the Default trait for Backoff.

§Examples

use exponential_backoff::Backoff;
use std::time::Duration;

let backoff = Backoff::default();
assert_eq!(backoff.max_attempts(), 3);
assert_eq!(backoff.min(), &Duration::from_millis(100));
assert_eq!(backoff.max(), &Duration::from_secs(10));
assert_eq!(backoff.jitter(), 0.3);
assert_eq!(backoff.factor(), 2);
Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'b> IntoIterator for &'b Backoff

Implements the IntoIterator trait for borrowed Backoff instances.

§Examples

use exponential_backoff::Backoff;
use std::time::Duration;

let backoff = Backoff::default();
let mut count = 0;

for duration in &backoff {
    count += 1;
    if count > 1 {
        break;
    }
}
Source§

type Item = Option<Duration>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Backoff

Implements the IntoIterator trait for owned Backoff instances.

§Examples

use exponential_backoff::Backoff;
use std::time::Duration;

let backoff = Backoff::default();
let mut count = 0;

for duration in backoff {
    count += 1;
    if count > 1 {
        break;
    }
}
Source§

type Item = Option<Duration>

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. 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 = 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.