Struct ratelimit_meter::state::direct::DirectRateLimiter
source · 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
sourceimpl<A> DirectRateLimiter<A>where
A: Algorithm,
impl<A> DirectRateLimiter<A>where
A: Algorithm,
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<A>
pub fn build_with_capacity(capacity: NonZeroU32) -> Builder<A>
Return a builder that can be used to construct a rate limiter using the parameters passed to the Builder.
sourcepub fn check(&mut self) -> Result<(), <A as Algorithm>::NegativeDecision>
pub fn check(&mut self) -> Result<(), <A as Algorithm>::NegativeDecision>
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.
sourcepub fn check_n(
&mut self,
n: u32
) -> Result<(), NegativeMultiDecision<<A as Algorithm>::NegativeDecision>>
pub fn check_n(
&mut self,
n: u32
) -> Result<(), NegativeMultiDecision<<A as Algorithm>::NegativeDecision>>
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.
sourcepub fn check_at(
&mut self,
at: Instant
) -> Result<(), <A as Algorithm>::NegativeDecision>
pub fn check_at(
&mut self,
at: Instant
) -> Result<(), <A as Algorithm>::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: Instant
) -> Result<(), NegativeMultiDecision<<A as Algorithm>::NegativeDecision>>
pub fn check_n_at(
&mut self,
n: u32,
at: Instant
) -> Result<(), NegativeMultiDecision<<A as Algorithm>::NegativeDecision>>
Tests if n
cells can be accommodated at the given time
(Instant::now()
), using check_n
Trait Implementations
sourceimpl<A: Clone + Algorithm> Clone for DirectRateLimiter<A>where
A::BucketState: Clone,
impl<A: Clone + Algorithm> Clone for DirectRateLimiter<A>where
A::BucketState: Clone,
sourcefn clone(&self) -> DirectRateLimiter<A>
fn clone(&self) -> DirectRateLimiter<A>
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more