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>
impl<A, C> DirectRateLimiter<A, C>
Sourcepub fn new(capacity: NonZeroU32, per_time_unit: Duration) -> Self
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));Sourcepub fn per_second(capacity: NonZeroU32) -> Self
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));Sourcepub fn build_with_capacity(capacity: NonZeroU32) -> Builder<C, A>
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.
Sourcepub fn check_at(
&mut self,
at: C::Instant,
) -> Result<(), <A as Algorithm<C::Instant>>::NegativeDecision>
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.
Sourcepub fn check_n_at(
&mut self,
n: u32,
at: C::Instant,
) -> Result<(), NegativeMultiDecision<<A as Algorithm<C::Instant>>::NegativeDecision>>
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
Sourcepub fn check(
&mut self,
) -> Result<(), <A as Algorithm<C::Instant>>::NegativeDecision>
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.
Sourcepub fn check_n(
&mut self,
n: u32,
) -> Result<(), NegativeMultiDecision<<A as Algorithm<C::Instant>>::NegativeDecision>>
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,
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>
fn clone(&self) -> DirectRateLimiter<A, C>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more