DirectRateLimiter

Struct DirectRateLimiter 

Source
pub struct DirectRateLimiter<A: Algorithm<C::Instant> = DefaultAlgorithm, C: Clock = DefaultClock> { /* 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§

Source§

impl<A, C> DirectRateLimiter<A, C>
where C: Clock, A: Algorithm<C::Instant>,

Source

pub fn new(capacity: NonZeroU32, per_time_unit: Duration) -> Self

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));
Source

pub fn per_second(capacity: NonZeroU32) -> Self

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));
Source

pub fn build_with_capacity(capacity: NonZeroU32) -> Builder<C, A>

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

Source

pub fn check_at( &mut self, at: C::Instant, ) -> Result<(), <A as Algorithm<C::Instant>>::NegativeDecision>

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

Source

pub fn check_n_at( &mut self, n: u32, at: C::Instant, ) -> Result<(), NegativeMultiDecision<<A as Algorithm<C::Instant>>::NegativeDecision>>

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

Source

pub fn check( &mut self, ) -> Result<(), <A as Algorithm<C::Instant>>::NegativeDecision>

Tests if a single cell can be accommodated at the clock’s current reading. 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.

Source

pub fn check_n( &mut self, n: u32, ) -> Result<(), NegativeMultiDecision<<A as Algorithm<C::Instant>>::NegativeDecision>>

Tests if n cells can be accommodated at the clock’s current reading. 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.

Trait Implementations§

Source§

impl<A: Clone + Algorithm<C::Instant>, C: Clone + Clock> Clone for DirectRateLimiter<A, C>
where A::BucketState: Clone,

Source§

fn clone(&self) -> DirectRateLimiter<A, C>

Returns a duplicate 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<A: Debug + Algorithm<C::Instant>, C: Debug + Clock> Debug for DirectRateLimiter<A, C>
where A::BucketState: Debug,

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<A, C> Freeze for DirectRateLimiter<A, C>
where <A as Algorithm<<C as Clock>::Instant>>::BucketState: Freeze, A: Freeze, C: Freeze,

§

impl<A, C> RefUnwindSafe for DirectRateLimiter<A, C>

§

impl<A, C> Send for DirectRateLimiter<A, C>
where C: Send,

§

impl<A, C> Sync for DirectRateLimiter<A, C>
where C: Sync,

§

impl<A, C> Unpin for DirectRateLimiter<A, C>
where <A as Algorithm<<C as Clock>::Instant>>::BucketState: Unpin, A: Unpin, C: Unpin,

§

impl<A, C> UnwindSafe for DirectRateLimiter<A, C>

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
Source§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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>,

Source§

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>,

Source§

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.