pub struct Backoff { /* private fields */ }
Expand description
Exponential backoff type.
Implementations§
Source§impl Backoff
impl Backoff
Sourcepub fn new(
max_attempts: u32,
min: Duration,
max: impl Into<Option<Duration>>,
) -> Self
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);
Sourcepub fn min(&self) -> &Duration
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));
Sourcepub fn set_min(&mut self, min: Duration)
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));
Sourcepub fn max(&self) -> &Duration
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));
Sourcepub fn set_max(&mut self, max: Duration)
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));
Sourcepub fn max_attempts(&self) -> u32
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);
Sourcepub fn set_max_attempts(&mut self, max_attempts: u32)
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);
Sourcepub fn jitter(&self) -> f32
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);
Sourcepub fn set_jitter(&mut self, jitter: f32)
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
Sourcepub fn factor(&self) -> u32
pub fn factor(&self) -> u32
Get the growth factor
§Examples
use exponential_backoff::Backoff;
let mut backoff = Backoff::default();
assert_eq!(backoff.factor(), 2);
Sourcepub fn set_factor(&mut self, factor: u32)
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);
Trait Implementations§
Source§impl Default for Backoff
Implements the Default
trait for Backoff
.
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§impl<'b> IntoIterator for &'b Backoff
Implements the IntoIterator
trait for borrowed Backoff
instances.
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§impl IntoIterator for Backoff
Implements the IntoIterator
trait for owned Backoff
instances.
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;
}
}
Auto Trait Implementations§
impl Freeze for Backoff
impl RefUnwindSafe for Backoff
impl Send for Backoff
impl Sync for Backoff
impl Unpin for Backoff
impl UnwindSafe for Backoff
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more