Skip to main content

Crate throttle_kit

Crate throttle_kit 

Source
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;tower
pub use client_ip::ClientIpError;tower
pub use client_ip::ClientIpSource;tower
pub use client_ip::IpNet;tower
pub use client_ip::MissingClientIdentity;tower
pub use client_ip::MissingClientPolicy;tower
pub use client_ip::ResolvedClient;tower

Modules§

client_iptower
Client identity resolution for rate limiting behind proxies.

Structs§

InMemoryBackendin-memory
In-memory backend backed by a DashMap.
KeyedRateLimiterin-memory
Per-key rate limiter that tracks separate rate limits for each key.
NoopBackendtest-util
No-op backend: allows every request with the quota’s full budget.
Quota
A rate limit quota expressed in the GCRA style.
RateLimitLayertower
Tower layer that applies rate limiting to inner services.
RateLimitResult
Result of a rate limit check.
RateLimitServicetower
Tower service that rate-limits requests by client identity.
RateLimiter
Core rate limiter that delegates to a RateLimitBackend.
RedisBackendredis
Redis-backed rate limiter using the GCRA algorithm via a Lua script.
SlidingWindowBackendsliding-window
In-memory backend using a sliding window algorithm.
SqliteBackendsqlite
SQLite-backed GCRA rate limiter.

Enums§

RateLimitError
Errors returned by the rate limiter.

Traits§

RateLimitBackend
Backend trait for rate limit state storage.

Functions§

gcra_decide
Pure GCRA (Generic Cell Rate Algorithm) conformance decision.

Type Aliases§

KeyExtractortower
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.