[][src]Struct ratelimit_meter::state::direct::DirectRateLimiter

pub struct DirectRateLimiter<A: Algorithm = DefaultAlgorithm> { /* fields omitted */ }

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.

Methods

impl<A> DirectRateLimiter<A> where
    A: Algorithm
[src]

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

impl<A: Clone + Algorithm> Clone for DirectRateLimiter<A> where
    A::BucketState: Clone
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<A: Debug + Algorithm> Debug for DirectRateLimiter<A> where
    A::BucketState: Debug
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl<A> Send for DirectRateLimiter<A> where
    <A as Algorithm>::BucketState: Send

impl<A> Sync for DirectRateLimiter<A> where
    <A as Algorithm>::BucketState: Sync

Blanket Implementations

impl<T> From for T
[src]

Performs the conversion.

impl<T, U> Into for T where
    U: From<T>, 
[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

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

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

recently added

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

impl<T, U> TryFrom for T where
    T: From<U>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T> Borrow for T where
    T: ?Sized
[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut for T where
    T: ?Sized
[src]

Mutably borrows from an owned value. Read more

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

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

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more

impl<T> Erased for T