pub struct ExponentialBackoff {
pub initial_delay: Duration,
pub max_delay: Duration,
pub max_attempts: u32,
pub multiplier: f64,
}Expand description
Exponential backoff retry policy.
Each retry waits exponentially longer than the previous one, up to a maximum delay. This is the recommended policy for most network operations.
§Example
use error_rail::async_ext::ExponentialBackoff;
use core::time::Duration;
let policy = ExponentialBackoff {
initial_delay: Duration::from_millis(100),
max_delay: Duration::from_secs(10),
max_attempts: 5,
multiplier: 2.0,
};
// Delays: 100ms, 200ms, 400ms, 800ms, 1600ms (capped at 10s)Fields§
§initial_delay: DurationInitial delay before first retry.
max_delay: DurationMaximum delay between retries.
max_attempts: u32Maximum number of retry attempts.
multiplier: f64Multiplier applied to delay after each attempt.
Implementations§
Source§impl ExponentialBackoff
impl ExponentialBackoff
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new exponential backoff policy with default settings.
The default configuration provides:
- Initial delay: 100 milliseconds
- Maximum delay: 30 seconds
- Maximum attempts: 5
- Multiplier: 2.0
Sourcepub fn with_initial_delay(self, delay: Duration) -> Self
pub fn with_initial_delay(self, delay: Duration) -> Self
Sets the initial delay duration for the first retry attempt.
This serves as the base value for the exponential calculation.
Sourcepub fn with_max_delay(self, delay: Duration) -> Self
pub fn with_max_delay(self, delay: Duration) -> Self
Sets the maximum duration allowed between retry attempts.
The delay will never exceed this value regardless of the number of attempts or the multiplier.
Sourcepub fn with_max_attempts(self, attempts: u32) -> Self
pub fn with_max_attempts(self, attempts: u32) -> Self
Sets the maximum number of retry attempts allowed.
Once this number of retries is reached, the policy will stop suggesting delays.
Sourcepub fn with_multiplier(self, multiplier: f64) -> Self
pub fn with_multiplier(self, multiplier: f64) -> Self
Sets the multiplier applied to the delay after each failed attempt.
For example, a multiplier of 2.0 doubles the delay duration each time.
Trait Implementations§
Source§impl Clone for ExponentialBackoff
impl Clone for ExponentialBackoff
Source§fn clone(&self) -> ExponentialBackoff
fn clone(&self) -> ExponentialBackoff
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more