Expand description
This module provides a simple exponential back-off generator with jitter (exponent 2, custom base).
Jitter uses the “full jitter” strategy: each backoff sleeps for a random duration in
[0, base) (with the default jitter factor). This de-synchronizes concurrent clients retrying
the same endpoint, avoiding a thundering herd.
§Example
use std::time::Duration;
use re_backoff::BackoffGenerator;
let mut generator = BackoffGenerator::new(Duration::from_secs(1), Duration::from_secs(8)).expect("valid generator");
let b = generator.gen_next();
assert_eq!(b.base(), Duration::from_secs(1));
// Full jitter: the actual sleep is somewhere in `[0, base)`.
assert!(b.jittered() <= b.base());
// sleep with:
// b.sleep().await;
let expected_backoffs = [2, 4, 8, 8, 8];
for expected in expected_backoffs {
let b = generator.gen_next();
assert_eq!(b.base(), Duration::from_secs(expected));
assert!(b.jittered() <= b.base());
}Structs§
- Backoff
Backoffrepresent an await-able back-off duration.- Backoff
Generator BackoffGeneratoris a generator for exponential back-off durations with jitter. See module-level docs for an example how to use it.