Struct governor::Jitter [−][src]
pub struct Jitter { /* fields omitted */ }
Expand description
An interval specification for deviating from the nominal wait time.
Jitter can be added to wait time Duration
s to ensure that multiple tasks waiting on the same
rate limit don’t wake up at the same time and attempt to measure at the same time.
Methods on rate limiters that work asynchronously like
DirectRateLimiter.until_ready_with_jitter
exist to automatically apply jitter to wait periods, thereby reducing the chance of a
thundering herd problem.
Examples
Jitter can be added manually to a Duration
:
let reference = Duration::from_secs(24);
let jitter = Jitter::new(Duration::from_secs(1), Duration::from_secs(1));
let result = jitter + reference;
assert!(result >= reference + Duration::from_secs(1));
assert!(result < reference + Duration::from_secs(2))
In a std
build (the default), Jitter can also be added to an Instant
:
let reference = Instant::now();
let jitter = Jitter::new(Duration::from_secs(1), Duration::from_secs(1));
let result = jitter + reference;
assert!(result >= reference + Duration::from_secs(1));
assert!(result < reference + Duration::from_secs(2))
Implementations
Constructs a new Jitter interval, waiting at most a duration of max
.
let jitter = Jitter::up_to(Duration::from_secs(20));
let now = Duration::from_secs(0);
assert!(jitter + now <= Duration::from_secs(20)); // always.
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Jitter
impl UnwindSafe for Jitter
Blanket Implementations
Mutably borrows from an owned value. Read more