[][src]Trait ratelimit_meter::algorithms::Algorithm

pub trait Algorithm: Send + Sync + Sized + Debug {
    type BucketState: RateLimitState<Self> + Default + Send + Sync + Eq + ShallowCopy + Debug;
    type NegativeDecision: PartialEq + Fail;
    fn construct(
        capacity: NonZeroU32,
        cell_weight: NonZeroU32,
        per_time_unit: Duration
    ) -> Result<Self, InconsistentCapacity>;
fn test_n_and_update(
        &self,
        state: &Self::BucketState,
        n: u32,
        at: Instant
    ) -> Result<(), NegativeMultiDecision<Self::NegativeDecision>>; fn test_and_update(
        &self,
        state: &Self::BucketState,
        at: Instant
    ) -> Result<(), Self::NegativeDecision> { ... } }

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

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).

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

Constructs a rate limiter with the given parameters: capacity is the number of cells to allow, weighing cell_weight, every per_time_unit.

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

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 for GCRA
[src]

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.

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 Algorithm for LeakyBucket
[src]

impl Algorithm for Allower
[src]

Allows all cells through unconditionally.