Skip to main content

RateLimiter

Trait RateLimiter 

Source
pub trait RateLimiter: Send + Sync {
    // Required methods
    fn check(&self, key: &str) -> bool;
    fn peek(&self, key: &str) -> u32;
    fn reset(&self, key: &str);
}
Expand description

Trait for rate limiters. Implementations decide whether a request from key (typically a client ID or IP) should be allowed.

Required Methods§

Source

fn check(&self, key: &str) -> bool

Check if a request is allowed. Consumes one token if allowed. Returns true if allowed, false if rate-limited.

Source

fn peek(&self, key: &str) -> u32

Peek at remaining tokens without consuming. Returns the approximate number of available tokens (0 means limited).

Source

fn reset(&self, key: &str)

Reset the limiter state for a key (clears the bucket).

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§