Skip to main content

Crate volga_rate_limiter

Crate volga_rate_limiter 

Source
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:

  • FixedWindowRateLimiter

    • Counts requests in discrete, fixed-size time windows
    • Very fast and simple
    • May allow short bursts at window boundaries
  • SlidingWindowRateLimiter

    • Uses a sliding time window with linear weighting
    • Provides smoother request distribution
    • Slightly more expensive than a fixed window
  • TokenBucketRateLimiter

    • Allows bursts up to a token bucket capacity
    • Enforces a steady average refill rate
    • Simple and flexible for bursty traffic
  • GcraRateLimiter

    • 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§

FixedWindowParams
Parameters for a single FixedWindowStore operation.
FixedWindowRateLimiter
A fixed-window rate limiter.
GcraParams
Parameters for a single GcraStore operation.
GcraRateLimiter
A GCRA (Generic Cell Rate Algorithm) rate limiter.
InMemoryFixedWindowStore
In-memory FixedWindowStore backed by a concurrent hash map.
InMemoryGcraStore
In-memory GcraStore backed by a concurrent hash map.
InMemorySlidingWindowStore
In-memory SlidingWindowStore backed by a concurrent hash map.
InMemoryTokenBucketStore
In-memory TokenBucketStore backed by a concurrent hash map.
SlidingWindowParams
Parameters for a single SlidingWindowStore operation.
SlidingWindowRateLimiter
A sliding-window rate limiter.
SystemTimeSource
Monotonic system time source backed by Instant.
TokenBucketParams
Parameters for a single TokenBucketStore operation.
TokenBucketRateLimiter
A token-bucket rate limiter.

Traits§

FixedWindowStore
A pluggable storage backend for the fixed-window algorithm.
GcraStore
A pluggable storage backend for the GCRA algorithm.
RateLimiter
A generic rate limiter interface.
SlidingWindowStore
A pluggable storage backend for the sliding-window algorithm.
TimeSource
A source of time used by rate-limiting algorithms.
TokenBucketStore
A pluggable storage backend for the token-bucket algorithm.