Expand description
Rate limiting for Rust.
Implements the GCRA (Generic Cell Rate Algorithm) with pluggable
backends (in-memory via DashMap, optional Redis) and optional Tower
layer integration.
§Quick Start
use throttle_kit::{RateLimiter, Quota, InMemoryBackend};
let backend = InMemoryBackend::new();
let limiter = RateLimiter::new(
Quota::per_second(10),
backend,
);
let result = limiter.check("user-123").await;
assert!(result.allowed);§Client identity (tower feature)
The Tower layer keys requests by the client’s socket address by
default and ignores X-Forwarded-For (secure by default). Behind
proxies you control, configure ClientIpConfig; see the
client_ip module docs for the resolution algorithm and the
README for axum wiring (.into_make_service_with_connect_info).
Re-exports§
pub use client_ip::ClientIpConfig;towerpub use client_ip::ClientIpError;towerpub use client_ip::ClientIpSource;towerpub use client_ip::IpNet;towerpub use client_ip::MissingClientIdentity;towerpub use client_ip::MissingClientPolicy;towerpub use client_ip::ResolvedClient;tower
Modules§
- client_
ip tower - Client identity resolution for rate limiting behind proxies.
Structs§
- InMemory
Backend in-memory - In-memory backend backed by a
DashMap. - Keyed
Rate Limiter in-memory - Per-key rate limiter that tracks separate rate limits for each key.
- Noop
Backend test-util - No-op backend: allows every request with the quota’s full budget.
- Quota
- A rate limit quota expressed in the GCRA style.
- Rate
Limit Layer tower - Tower layer that applies rate limiting to inner services.
- Rate
Limit Result - Result of a rate limit check.
- Rate
Limit Service tower - Tower service that rate-limits requests by client identity.
- Rate
Limiter - Core rate limiter that delegates to a
RateLimitBackend. - Redis
Backend redis - Redis-backed rate limiter using the GCRA algorithm via a Lua script.
- Sliding
Window Backend sliding-window - In-memory backend using a sliding window algorithm.
- Sqlite
Backend sqlite - SQLite-backed GCRA rate limiter.
Enums§
- Rate
Limit Error - Errors returned by the rate limiter.
Traits§
- Rate
Limit Backend - Backend trait for rate limit state storage.
Functions§
- gcra_
decide - Pure GCRA (Generic Cell Rate Algorithm) conformance decision.
Type Aliases§
- KeyExtractor
tower - Custom, non-IP key extractor for callers that key requests by something other than client IP (API key, tenant id, …). Receives the request’s headers and extensions; returns the rate-limit key.