Algorithm

Trait Algorithm 

Source
pub trait Algorithm:
    Send
    + Sync
    + 'static {
    // Required methods
    fn name(&self) -> &'static str;
    fn check_and_record<S: Storage>(
        &self,
        storage: &S,
        key: &str,
        quota: &Quota,
    ) -> impl Future<Output = Result<Decision>> + Send;
    fn check<S: Storage>(
        &self,
        storage: &S,
        key: &str,
        quota: &Quota,
    ) -> impl Future<Output = Result<Decision>> + Send;

    // Provided method
    fn reset<S: Storage>(
        &self,
        storage: &S,
        key: &str,
    ) -> impl Future<Output = Result<()>> + Send { ... }
}
Expand description

Rate limiting algorithm trait.

Each algorithm provides different trade-offs between accuracy, memory usage, and burst handling. All implementations must be thread-safe.

§Algorithm Comparison

AlgorithmAccuracyMemoryBurstBest For
GCRAHighestLow (1 timestamp)ControlledPrecise rate control
Token BucketHighLowExcellentBursty traffic
Leaky BucketHighMediumNoneSmooth output
Sliding LogHighestHighGoodPrecision critical
Sliding WindowMediumLowGoodGeneral purpose
Fixed WindowLowLowPoorSimple use cases
ConcurrentN/ALowN/ALimit parallelism

Required Methods§

Source

fn name(&self) -> &'static str

Get the algorithm name (for logging/metrics).

Source

fn check_and_record<S: Storage>( &self, storage: &S, key: &str, quota: &Quota, ) -> impl Future<Output = Result<Decision>> + Send

Check if a request is allowed AND record it atomically.

This is the primary method for rate limiting. It checks whether the request should be allowed and, if so, records it against the quota.

Source

fn check<S: Storage>( &self, storage: &S, key: &str, quota: &Quota, ) -> impl Future<Output = Result<Decision>> + Send

Check without recording (peek at current state).

Useful for displaying rate limit info without consuming quota.

Provided Methods§

Source

fn reset<S: Storage>( &self, storage: &S, key: &str, ) -> impl Future<Output = Result<()>> + Send

Reset the rate limit for a key.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§