[−][src]Trait ratelimit_meter::algorithms::Algorithm
The trait that implementations of metered rate-limiter algorithms have to implement.
Implementing structures are expected to represent the "parameters"
(e.g., the allowed requests/s), and keep the information necessary
to make a decision, e.g. concrete usage statistics for an
in-memory rate limiter, in the associated structure
BucketState
.
Associated Types
type BucketState: RateLimitState<Self, P>
The state of a single rate limiting bucket.
Every new rate limiting state is initialized as Default
. The
states must be safe to share across threads (this crate uses a
parking_lot
Mutex to allow that).
type NegativeDecision: PartialEq + Display + Debug + Send + Sync
The type returned when a rate limiting decision for a single
cell is negative. Each rate limiting algorithm can decide to
return the type that suits it best, but most algorithms'
decisions also implement
NonConformance
, to ease
handling of how long to wait.
Required methods
fn construct(
capacity: NonZeroU32,
cell_weight: NonZeroU32,
per_time_unit: Duration
) -> Result<Self, InconsistentCapacity>
capacity: NonZeroU32,
cell_weight: NonZeroU32,
per_time_unit: Duration
) -> Result<Self, InconsistentCapacity>
Constructs a rate limiter with the given parameters:
capacity
is the number of cells to allow, weighing
cell_weight
, every per_time_unit
.
fn test_n_and_update(
&self,
state: &Self::BucketState,
n: u32,
at: P
) -> Result<(), NegativeMultiDecision<Self::NegativeDecision>>
&self,
state: &Self::BucketState,
n: u32,
at: P
) -> Result<(), NegativeMultiDecision<Self::NegativeDecision>>
Tests if n
cells can be accommodated in the rate limiter at
the instant at
and updates the rate-limiter state to account
for the weight of the cells and updates the ratelimiter state.
The update is all or nothing: Unless all n cells can be accommodated, the state of the rate limiter will not be updated.
Provided methods
fn test_and_update(
&self,
state: &Self::BucketState,
at: P
) -> Result<(), Self::NegativeDecision>
&self,
state: &Self::BucketState,
at: P
) -> Result<(), Self::NegativeDecision>
Tests if a single cell can be accommodated in the rate limiter
at the instant at
and updates the rate-limiter state to
account for the weight of the cell.
This method is provided by default, using the n
test&update
method.
Implementors
impl Algorithm<Always> for Allower
[src]
type BucketState = ()
type NegativeDecision = Impossible
fn construct(
_capacity: NonZeroU32,
_cell_weight: NonZeroU32,
_per_time_unit: Duration
) -> Result<Self, InconsistentCapacity>
[src]
_capacity: NonZeroU32,
_cell_weight: NonZeroU32,
_per_time_unit: Duration
) -> Result<Self, InconsistentCapacity>
fn test_n_and_update(
&self,
_state: &Self::BucketState,
_n: u32,
_t0: Always
) -> Result<(), NegativeMultiDecision<Impossible>>
[src]
&self,
_state: &Self::BucketState,
_n: u32,
_t0: Always
) -> Result<(), NegativeMultiDecision<Impossible>>
Allows all cells through unconditionally.
fn test_and_update(
&self,
state: &Self::BucketState,
at: P
) -> Result<(), Self::NegativeDecision>
[src]
&self,
state: &Self::BucketState,
at: P
) -> Result<(), Self::NegativeDecision>
impl<P: Relative> Algorithm<P> for GCRA<P>
[src]
type BucketState = State<P>
type NegativeDecision = NotUntil<P>
fn construct(
capacity: NonZeroU32,
cell_weight: NonZeroU32,
per_time_unit: Duration
) -> Result<Self, InconsistentCapacity>
[src]
capacity: NonZeroU32,
cell_weight: NonZeroU32,
per_time_unit: Duration
) -> Result<Self, InconsistentCapacity>
fn test_and_update(
&self,
state: &Self::BucketState,
t0: P
) -> Result<(), Self::NegativeDecision>
[src]
&self,
state: &Self::BucketState,
t0: P
) -> Result<(), Self::NegativeDecision>
Tests if a single cell can be accommodated by the rate-limiter. This is a threadsafe implementation of the method described directly in the GCRA algorithm.
fn test_n_and_update(
&self,
state: &Self::BucketState,
n: u32,
t0: P
) -> Result<(), NegativeMultiDecision<Self::NegativeDecision>>
[src]
&self,
state: &Self::BucketState,
n: u32,
t0: P
) -> Result<(), NegativeMultiDecision<Self::NegativeDecision>>
Tests if n
cells can be accommodated by the rate-limiter
and updates rate limiter state iff they can be.
As this method is an extension of GCRA (using multiplication), it is likely not as fast (and not as obviously "right") as the single-cell variant.
impl<P: Relative> Algorithm<P> for LeakyBucket<P>
[src]
type BucketState = State<P>
type NegativeDecision = TooEarly<P>
fn construct(
capacity: NonZeroU32,
cell_weight: NonZeroU32,
per_time_unit: Duration
) -> Result<Self, InconsistentCapacity>
[src]
capacity: NonZeroU32,
cell_weight: NonZeroU32,
per_time_unit: Duration
) -> Result<Self, InconsistentCapacity>
fn test_n_and_update(
&self,
state: &Self::BucketState,
n: u32,
t0: P
) -> Result<(), NegativeMultiDecision<TooEarly<P>>>
[src]
&self,
state: &Self::BucketState,
n: u32,
t0: P
) -> Result<(), NegativeMultiDecision<TooEarly<P>>>
fn test_and_update(
&self,
state: &Self::BucketState,
at: P
) -> Result<(), Self::NegativeDecision>
[src]
&self,
state: &Self::BucketState,
at: P
) -> Result<(), Self::NegativeDecision>