[−][src]Struct ratelimit_meter::state::direct::DirectRateLimiter
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, P> DirectRateLimiter<A, P> where
P: Relative,
A: Algorithm<P>,
[src]
P: Relative,
A: Algorithm<P>,
pub fn new(capacity: NonZeroU32, per_time_unit: Duration) -> Self
[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));
pub fn per_second(capacity: NonZeroU32) -> Self
[src]
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));
pub fn build_with_capacity(capacity: NonZeroU32) -> Builder<P, A>
[src]
Return a builder that can be used to construct a rate limiter using the parameters passed to the Builder.
pub fn check_at(
&mut self,
at: P
) -> Result<(), <A as Algorithm<P>>::NegativeDecision>
[src]
&mut self,
at: P
) -> Result<(), <A as Algorithm<P>>::NegativeDecision>
Tests whether a single cell can be accommodated at the given
time stamp. See check
.
pub fn check_n_at(
&mut self,
n: u32,
at: P
) -> Result<(), NegativeMultiDecision<<A as Algorithm<P>>::NegativeDecision>>
[src]
&mut self,
n: u32,
at: P
) -> Result<(), NegativeMultiDecision<<A as Algorithm<P>>::NegativeDecision>>
Tests if n
cells can be accommodated at the given time
(Instant::now()
), using check_n
impl<A, P> DirectRateLimiter<A, P> where
P: Absolute,
A: Algorithm<P>,
[src]
P: Absolute,
A: Algorithm<P>,
pub fn check(&mut self) -> Result<(), <A as Algorithm<P>>::NegativeDecision>
[src]
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.
pub fn check_n(
&mut self,
n: u32
) -> Result<(), NegativeMultiDecision<<A as Algorithm<P>>::NegativeDecision>>
[src]
&mut self,
n: u32
) -> Result<(), NegativeMultiDecision<<A as Algorithm<P>>::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.
Trait Implementations
impl<A: Clone + Algorithm<P>, P: Clone + Relative> Clone for DirectRateLimiter<A, P> where
A::BucketState: Clone,
[src]
A::BucketState: Clone,
fn clone(&self) -> DirectRateLimiter<A, P>
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<A: Debug + Algorithm<P>, P: Debug + Relative> Debug for DirectRateLimiter<A, P> where
A::BucketState: Debug,
[src]
A::BucketState: Debug,
Auto Trait Implementations
impl<A, P> Send for DirectRateLimiter<A, P> where
<A as Algorithm<P>>::BucketState: Send,
<A as Algorithm<P>>::BucketState: Send,
impl<A, P> Sync for DirectRateLimiter<A, P> where
<A as Algorithm<P>>::BucketState: Sync,
<A as Algorithm<P>>::BucketState: Sync,
Blanket Implementations
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From for T
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
ⓘImportant traits for &'_ mut Rfn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,