pub struct ExponentialBackoff { /* private fields */ }Expand description
Exponential backoff calculator for retry logic.
§Examples
use chie_shared::ExponentialBackoff;
let mut backoff = ExponentialBackoff::new();
// Simulate retry attempts
let delay1 = backoff.next_delay_ms();
assert!(delay1 >= 75 && delay1 <= 125); // ~100ms ± 25% jitter
let delay2 = backoff.next_delay_ms();
assert!(delay2 >= 150 && delay2 <= 250); // ~200ms ± 25% jitter
assert_eq!(backoff.attempt_count(), 2);
assert!(!backoff.is_exhausted());
// Custom backoff for more aggressive retries
let mut fast_backoff = ExponentialBackoff::custom(50, 5_000, 1.5, 5);
let first_delay = fast_backoff.next_delay_ms();
assert!(first_delay >= 37 && first_delay <= 63); // ~50ms ± 25%Implementations§
Source§impl ExponentialBackoff
impl ExponentialBackoff
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new exponential backoff with default settings. Base: 100ms, Max: 30s, Multiplier: 2.0, Max attempts: 10
Sourcepub fn custom(
base_ms: u64,
max_ms: u64,
multiplier: f64,
max_attempts: u32,
) -> Self
pub fn custom( base_ms: u64, max_ms: u64, multiplier: f64, max_attempts: u32, ) -> Self
Create a custom exponential backoff.
Sourcepub fn next_delay_ms(&mut self) -> u64
pub fn next_delay_ms(&mut self) -> u64
Get the next delay in milliseconds with jitter.
Sourcepub fn is_exhausted(&self) -> bool
pub fn is_exhausted(&self) -> bool
Check if max attempts have been reached.
Sourcepub fn attempt_count(&self) -> u32
pub fn attempt_count(&self) -> u32
Get current attempt number.
Trait Implementations§
Source§impl Clone for ExponentialBackoff
impl Clone for ExponentialBackoff
Source§fn clone(&self) -> ExponentialBackoff
fn clone(&self) -> ExponentialBackoff
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ExponentialBackoff
impl Debug for ExponentialBackoff
Auto Trait Implementations§
impl Freeze for ExponentialBackoff
impl RefUnwindSafe for ExponentialBackoff
impl Send for ExponentialBackoff
impl Sync for ExponentialBackoff
impl Unpin for ExponentialBackoff
impl UnwindSafe for ExponentialBackoff
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