pub struct DirectRateLimiter<A: Algorithm = DefaultAlgorithm> { /* private fields */ }
Expand description

An in-memory rate limiter that makes direct (un-keyed) rate-limiting decisions. Direct rate limiters can be used to e.g. regulate the transmission of packets on a single connection, or to ensure that an API client stays within a server’s rate limit.

Implementations

Construct a new rate limiter that allows capacity cells per time unit through.

Examples

You can construct a GCRA rate limiter like so:

use ratelimit_meter::{DirectRateLimiter, GCRA};
let _gcra = DirectRateLimiter::<GCRA>::new(nonzero!(100u32), Duration::from_secs(5));

and similarly, for a leaky bucket:

use ratelimit_meter::{DirectRateLimiter, LeakyBucket};
let _lb = DirectRateLimiter::<LeakyBucket>::new(nonzero!(100u32), Duration::from_secs(5));

Construct a new rate limiter that allows capacity cells per second.

Examples

Constructing a GCRA rate limiter that lets through 100 cells per second:

use ratelimit_meter::{DirectRateLimiter, GCRA};
let _gcra = DirectRateLimiter::<GCRA>::per_second(nonzero!(100u32));

and a leaky bucket:

use ratelimit_meter::{DirectRateLimiter, LeakyBucket};
let _gcra = DirectRateLimiter::<LeakyBucket>::per_second(nonzero!(100u32));

Return a builder that can be used to construct a rate limiter using the parameters passed to the Builder.

Tests if a single cell can be accommodated at Instant::now(). If it can be, check updates the rate limiter state to account for the conforming cell and returns Ok(()).

If the cell is non-conforming (i.e., it can’t be accomodated at this time stamp), check_at returns Err with information about the earliest time at which a cell could be considered conforming.

Tests if n cells can be accommodated at the current time stamp. If (and only if) all cells in the batch can be accomodated, the MultiDecider updates the internal state to account for all cells and returns Ok(()).

If the entire batch of cells would not be conforming but the rate limiter has the capacity to accomodate the cells at any point in time, check_n_at returns error NegativeMultiDecision::BatchNonConforming, holding the number of cells the rate limiter’s negative outcome result.

If n exceeds the bucket capacity, check_n_at returns NegativeMultiDecision::InsufficientCapacity, indicating that a batch of this many cells can never succeed.

Tests whether a single cell can be accommodated at the given time stamp. See check.

Tests if n cells can be accommodated at the given time (Instant::now()), using check_n

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. 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.