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

Implements the industry-standard leaky bucket rate-limiting as-a-meter. The bucket keeps a “fill height”, pretending to drip steadily (which reduces the fill height), and increases the fill height with every cell that is found conforming. If cells would make the bucket overflow, they count as non-conforming.

Drip implementation

Instead of having a background task update the bucket’s fill level, this implementation re-computes the fill level of the bucket on every call to check and related methods.

Wait time calculation

If the cell does not fit, this implementation computes the minimum wait time until the cell can be accommodated. This minimum wait time does not account for thundering herd effects or other problems in concurrent resource acquisition, so users of this library must take care to apply positive jitter to these wait times.

Example

let mut lb = DirectRateLimiter::<LeakyBucket>::per_second(nonzero!(2u32));
assert_eq!(Ok(()), lb.check());

Trait Implementations

The state of a single rate limiting bucket. Read more
The type returned when a rate limiting decision for a single cell is negative. Each rate limiting algorithm can decide to return the type that suits it best, but most algorithms’ decisions also implement NonConformance, to ease handling of how long to wait. Read more
Constructs a rate limiter with the given parameters: capacity is the number of cells to allow, weighing cell_weight, every per_time_unit. Read more
Tests if n cells can be accommodated in the rate limiter at the instant at and updates the rate-limiter state to account for the weight of the cells and updates the ratelimiter state. Read more
Tests if a single cell can be accommodated in the rate limiter at the instant at and updates the rate-limiter state to account for the weight of the cell. 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
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
Returns the last time instant that the state had any relevance (i.e. the rate limiter would behave exactly as if it was a new rate limiter after this time). Read more

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

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
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.