Skip to main content

Module ratelimit

Module ratelimit 

Source
Expand description

Per-principal rate limiting.

Token-bucket algorithm with a pluggable store. The default in-memory implementation is appropriate for single-instance deployments; banks running multiple replicas bring a Redis-backed implementation through the RateLimitStore trait so all replicas share the same view of consumption.

The middleware computes a key per request (the default is the authorization-header fingerprint, the same shape the idempotency layer uses) and refuses with 429 plus a Retry-After header when the bucket is empty. Banks running tenant-scoped budgeting can swap the key function for tenant-id.

Structs§

InMemoryRateLimitStore
In-memory RateLimitStore. Suitable for single-replica deployments and development; banks running multi-replica clusters need a Redis-backed implementation so the limit is enforced cluster-wide.
RateLimitConfig
Configuration for a single bucket: capacity (max burst) and refill rate in tokens per second. Banks running high-frequency back-office traffic pick large bursts; consumer-facing channels use small bursts to dampen abuse.
RateLimitLayer
RateLimitService

Enums§

RateLimitDecision
Result of attempting to consume a token. Allowed carries the number of tokens left after consumption; Throttled carries seconds the caller should wait before retrying.

Traits§

RateLimitStore
Pluggable storage for token-bucket state. Implementations must be safe to share across tasks (use a Mutex internally, or rely on the backing store’s atomicity).