pub struct Jitter { /* private fields */ }
Expand description

An interval specification for deviating from the nominal wait time.

Jitter can be added to wait time Durations 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.

Constructs a new Jitter interval, waiting at least min and at most min+interval.

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.