touch_ratelimit 0.1.0

A composable, extensible rate limiting crate for Rust
Documentation
pub mod in_memory;

pub use in_memory::InMemoryStore;

use std::sync::Arc;

/// Storage backend for rate limiting.
///
/// Implementations are responsible for:
/// - Managing rate limiter instances per key
/// - Handling concurrency
///
/// The store owns *many* rate limiters, while each limiter handles
/// *one* identity.
pub trait RateLimitStore: Send + Sync + 'static {
    /// Attempt to consume a unit for the given key.
    ///
    /// Returns `true` if the request is allowed, or `false` if the
    /// rate limit has been exceeded.
    fn allow(&self, key: &str) -> bool;
}

impl<T: RateLimitStore> RateLimitStore for Arc<T> {
    fn allow(&self, key: &str) -> bool {
        (**self).allow(key)
    }
}