pub struct ExponentialBackoff { /* private fields */ }Expand description
Exponential backoff strategy with optional jitter
This strategy increases the delay exponentially with each attempt, and can add random jitter to prevent multiple retries from synchronizing.
Implementations§
Source§impl ExponentialBackoff
impl ExponentialBackoff
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new exponential backoff with default settings
Defaults:
- Initial delay: 100ms
- Max delay: 30000ms (30 seconds)
- Factor: 2.0 (doubles with each attempt)
- Jitter: false
Sourcepub fn with_initial_delay(self, delay_ms: u64) -> Self
pub fn with_initial_delay(self, delay_ms: u64) -> Self
Set the initial delay in milliseconds
Sourcepub fn with_max_delay(self, delay_ms: u64) -> Self
pub fn with_max_delay(self, delay_ms: u64) -> Self
Set the maximum delay in milliseconds
Sourcepub fn with_factor(self, factor: f64) -> Self
pub fn with_factor(self, factor: f64) -> Self
Set the multiplication factor for each attempt
Sourcepub fn with_jitter(self, jitter: bool) -> Self
pub fn with_jitter(self, jitter: bool) -> Self
Enable or disable ±20% jitter on each calculated delay.
Jitter applies to every attempt, including the first. Each delay is drawn uniformly from 80% to 120% of the calculated delay, and never above the maximum delay: once the calculated delay nears the maximum, the upper end of the range is the maximum itself, so delays at the cap stay spread out instead of landing on it.
Off by default. Since 1.2.0 jitter works with or without the
jitter cargo feature (it no longer needs rand); the feature
now turns jitter on by default for the exponential
RetryExecutor and
RetryPolicy.
§Example
use error_forge::recovery::{Backoff, ExponentialBackoff};
use std::time::Duration;
let backoff = ExponentialBackoff::new().with_jitter(true);
let delay = backoff.next_delay(0); // around the 100 ms initial delay
assert!(delay >= Duration::from_millis(80) && delay <= Duration::from_millis(120));