pub struct ExponentialBackoffBuilder { /* private fields */ }Expand description
Implements truncated exponential backoff with jitter.
Implementations§
Source§impl ExponentialBackoffBuilder
impl ExponentialBackoffBuilder
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a builder with the default parameters.
§Example
use std::time::Duration;
fn configure_backoff(config: options::ClientConfig) -> Result<options::ClientConfig> {
let policy = ExponentialBackoffBuilder::new()
.with_initial_delay(Duration::from_millis(100))
.with_maximum_delay(Duration::from_secs(5))
.with_scaling(4.0)
.build()?;
Ok(config.set_backoff_policy(policy))
}§Example
use std::time::Instant;
let policy = ExponentialBackoffBuilder::new().build();
assert!(policy.is_ok());
let policy = policy?;
assert!(policy.on_failure(Instant::now(), 1) > std::time::Duration::ZERO);Sourcepub fn with_initial_delay<V: Into<Duration>>(self, v: V) -> Self
pub fn with_initial_delay<V: Into<Duration>>(self, v: V) -> Self
Change the initial delay.
Sourcepub fn with_maximum_delay<V: Into<Duration>>(self, v: V) -> Self
pub fn with_maximum_delay<V: Into<Duration>>(self, v: V) -> Self
Change the initial delay.
Sourcepub fn with_scaling<V: Into<f64>>(self, v: V) -> Self
pub fn with_scaling<V: Into<f64>>(self, v: V) -> Self
Change the scaling factor in this backoff policy.
Sourcepub fn build(self) -> Result<ExponentialBackoff>
pub fn build(self) -> Result<ExponentialBackoff>
Creates a new exponential backoff policy.
§Example
use std::time::Duration;
use std::time::Instant;
let backoff = ExponentialBackoffBuilder::new()
.with_initial_delay(Duration::from_secs(5))
.with_maximum_delay(Duration::from_secs(50))
.with_scaling(2.0)
.build()?;
let _ = backoff.on_failure(Instant::now(), 1);Sourcepub fn clamp(self) -> ExponentialBackoff
pub fn clamp(self) -> ExponentialBackoff
Creates a new exponential backoff policy clamping the ranges to barely recommended values.
The maximum delay is clamped first, to be between one second and one day (both inclusive). The upper value is hardly useful, except maybe in tests and very long running operations.
Then the initial delay is clamped to be between one millisecond and the maximum delay. One millisecond is rarely useful outside of tests, but at is unlikely to cause problems.
Finally, the scaling factor is clamped to the [1.0, 32.0] range.
Neither extreme is very useful, but neither are necessarily going to
cause trouble.
§Example
use std::time::Duration;
use std::time::Instant;
let mut backoff = ExponentialBackoffBuilder::new().clamp();
assert!(backoff.on_failure(Instant::now(), 1) > Duration::ZERO);Trait Implementations§
Source§impl Clone for ExponentialBackoffBuilder
impl Clone for ExponentialBackoffBuilder
Source§fn clone(&self) -> ExponentialBackoffBuilder
fn clone(&self) -> ExponentialBackoffBuilder
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more