Expand description
§Volga Rate Limiter
A lightweight and efficient rate-limiting library for Rust.
This crate provides in-memory rate limiting algorithms designed for high-performance HTTP services and middleware.
§Overview
Rate limiting is used to control the number of requests that a client (or a group of clients) can perform within a given time window. Typical use cases include:
- Protecting APIs from abuse or accidental overload
- Enforcing fair usage policies
- Applying different limits for anonymous users, authenticated users, tenants, or API keys
This crate focuses on per-node, in-memory rate limiting. It is intentionally simple and fast, and does not attempt to synchronize state across multiple processes or machines.
§Algorithms
The following rate-limiting algorithms are provided:
-
- Counts requests in discrete, fixed-size time windows
- Very fast and simple
- May allow short bursts at window boundaries
-
- Uses a sliding time window with linear weighting
- Provides smoother request distribution
- Slightly more expensive than a fixed window
-
- Allows bursts up to a token bucket capacity
- Enforces a steady average refill rate
- Simple and flexible for bursty traffic
-
- Uses the Generic Cell Rate Algorithm (GCRA)
- Smooths traffic with explicit burst tolerance
- Accurate average rate enforcement
§Time Source Abstraction
All rate limiters are built on top of a pluggable TimeSource abstraction.
This allows:
- Deterministic and fast unit testing
- Custom time implementations if needed
The default implementation, SystemTimeSource, is based on
std::time::Instant.
§Concurrency Model
The rate limiters are designed to be:
- Thread-safe
- Lock-free or minimally locking on the hot path
- Safe to share between async tasks and threads
Internal state is optimized for frequent reads and updates under high contention.
§Usage
The rate limiters are intended to be embedded into higher-level frameworks or middleware layers.
Structs§
- Fixed
Window Params - Parameters for a single
FixedWindowStoreoperation. - Fixed
Window Rate Limiter - A fixed-window rate limiter.
- Gcra
Params - Parameters for a single
GcraStoreoperation. - Gcra
Rate Limiter - A GCRA (Generic Cell Rate Algorithm) rate limiter.
- InMemory
Fixed Window Store - In-memory
FixedWindowStorebacked by a concurrent hash map. - InMemory
Gcra Store - In-memory
GcraStorebacked by a concurrent hash map. - InMemory
Sliding Window Store - In-memory
SlidingWindowStorebacked by a concurrent hash map. - InMemory
Token Bucket Store - In-memory
TokenBucketStorebacked by a concurrent hash map. - Sliding
Window Params - Parameters for a single
SlidingWindowStoreoperation. - Sliding
Window Rate Limiter - A sliding-window rate limiter.
- System
Time Source - Monotonic system time source backed by
Instant. - Token
Bucket Params - Parameters for a single
TokenBucketStoreoperation. - Token
Bucket Rate Limiter - A token-bucket rate limiter.
Traits§
- Fixed
Window Store - A pluggable storage backend for the fixed-window algorithm.
- Gcra
Store - A pluggable storage backend for the GCRA algorithm.
- Rate
Limiter - A generic rate limiter interface.
- Sliding
Window Store - A pluggable storage backend for the sliding-window algorithm.
- Time
Source - A source of time used by rate-limiting algorithms.
- Token
Bucket Store - A pluggable storage backend for the token-bucket algorithm.