Skip to main content

Crate tokio_rate_limit

Crate tokio_rate_limit 

Source
Expand description

§tokio-rate-limit

High-performance rate limiting library with lock-free token accounting and sharded state management.

§Features

  • Lock-free token accounting: Atomic operations on token counts (17M+ ops/sec)
  • 256-shard state management: Lock-free concurrent hashmap (flurry) with micro-sharding
  • Pluggable algorithms: Token bucket, leaky bucket, probabilistic sampling
  • Axum middleware: Drop-in rate limiting for web apps
  • Zero allocations: In the hot path

§Architecture

This library uses a hybrid approach for maximum performance:

  • Token updates: True lock-free atomic compare-and-swap operations
  • Key lookup: 256-shard lock-free concurrent hashmap (flurry) for near-linear multi-threaded scaling

This design provides excellent concurrency while maintaining per-key isolation.

§Quick Start

use tokio_rate_limit::{RateLimiter, RateLimiterConfig};

#[tokio::main]
async fn main() {
    let limiter = RateLimiter::new(RateLimiterConfig {
        requests_per_second: 100,
        burst: 200,
    });

    let decision = limiter.check("client-id").await.unwrap();
    if decision.permitted {
        // Process request
    } else {
        // Rate limit exceeded, retry after decision.retry_after
    }
}

Re-exports§

pub use algorithm::Algorithm;

Modules§

algorithm
Rate limiting algorithms.

Structs§

RateLimitDecision
Result of a rate limit check.
RateLimiter
A rate limiter that tracks requests per key and enforces limits.
RateLimiterBuilder
Builder for creating a rate limiter with a fluent API.
RateLimiterConfig
Configuration for creating a rate limiter.

Enums§

Error
Error types that can occur during rate limiting operations.

Type Aliases§

Result
A specialized Result type for rate limiting operations.