Skip to main content

Limiter

Trait Limiter 

Source
pub trait Limiter {
    // Required method
    fn check_n(&self, key: impl Into<Key>, n: u32) -> Decision;

    // Provided method
    fn check(&self, key: impl Into<Key>) -> Decision { ... }
}
Expand description

The shared rate-limiting surface, independent of the algorithm behind it.

Every limiter — whatever algorithm it uses — answers the same question: is this key allowed right now? Limiter is that contract, so generic code can hold any limiter and call check without naming the concrete type or its clock. RateLimiter is the implementation this crate ships.

Implementors only need to provide check_n; check defaults to one unit.

§Examples

use rate_net::{Limiter, RateLimiter};

// Generic over any limiter implementation.
fn admit_one<L: Limiter>(limiter: &L, key: &str) -> bool {
    limiter.check(key).is_allow()
}

let limiter = RateLimiter::per_second(2);
assert!(admit_one(&limiter, "user:1"));

Required Methods§

Source

fn check_n(&self, key: impl Into<Key>, n: u32) -> Decision

Checks n units against key, returning the Decision.

Provided Methods§

Source

fn check(&self, key: impl Into<Key>) -> Decision

Checks a single unit against key. Equivalent to check_n(key, 1).

Dyn Compatibility§

This trait is not dyn compatible.

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

Implementors§