Struct governor::Jitter

source ·
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§

source§

impl Jitter

source

pub fn up_to(max: Duration) -> Jitter

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.
source

pub fn new(min: Duration, interval: Duration) -> Jitter

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

Trait Implementations§

source§

impl Add<Duration> for Jitter

§

type Output = Duration

The resulting type after applying the + operator.
source§

fn add(self, rhs: Duration) -> Duration

Performs the + operation. Read more
source§

impl Add<Instant> for Jitter

§

type Output = Instant

The resulting type after applying the + operator.
source§

fn add(self, rhs: Instant) -> Instant

Performs the + operation. Read more
source§

impl Add<Nanos> for Jitter

§

type Output = Nanos

The resulting type after applying the + operator.
source§

fn add(self, rhs: Nanos) -> Nanos

Performs the + operation. Read more
source§

impl Clone for Jitter

source§

fn clone(&self) -> Jitter

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Jitter

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Jitter

source§

fn default() -> Jitter

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

impl PartialEq for Jitter

source§

fn eq(&self, other: &Jitter) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for Jitter

source§

impl Eq for Jitter

source§

impl StructuralPartialEq for Jitter

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V