Skip to main content

Crate tower_rate_tier

Crate tower_rate_tier 

Source
Expand description

Tier-based rate limiting middleware for tower services.

Assign users to named tiers (e.g. “free”, “pro”) with distinct quotas, and let the middleware enforce limits automatically via the GCRA algorithm.

§Features

  • buffered-body: identify users from the request body (TierLimitLayer::buffer_body).
  • redis: RedisStorage keeps the limits in Redis, shared by every instance of a service. Requires Rust 1.88.

Re-exports§

pub use cost::tier_cost;
pub use cost::TierCost;
pub use event::LimitEvent;
pub use gcra::RateLimitInfo;
pub use gcra::RateLimited;
pub use identifier::TierIdentifier;
pub use identifier::TierIdentity;
pub use layer::TierLimitLayer;
pub use on_missing::OnMissing;
pub use on_storage_error::OnStorageError;
pub use on_unknown_tier::OnUnknownTier;
pub use quota::Nanos;
pub use quota::Quota;
pub use storage::StorageError;
pub use storage::StorageKey;
pub use tier::CheckError;
pub use tier::RateTier;
pub use buffered::BufferedTierLimitLayer;
pub use buffered::BufferedTierLimitService;
pub use storage::redis::RedisStorage;

Modules§

buffered
Buffered-body variants that allow identifier access to the request body.
clock
Clock abstractions for real and deterministic (test) time sources.
cost
Cost extraction: decide how many tokens each request consumes.
event
Events reported while rate limiting, for logging and metrics.
gc
Background garbage collection for expired rate-limit entries.
gcra
Generic Cell Rate Algorithm (GCRA) implementation.
identifier
User identity and tier extraction from incoming requests.
layer
Tower Layer implementation that wraps services with rate limiting.
on_missing
Policy for handling requests whose identity cannot be resolved.
on_storage_error
Policy for handling storage backend errors.
on_unknown_tier
Policy for handling tiers that are not configured.
quota
Quota definitions (rate, burst, period).
response
HTTP response helpers for rate-limit headers and 429 replies.
service
Tower Service implementation that enforces per-tier rate limits.
storage
Storage backends for persisting rate-limit state.
tier
Tier configuration and the RateTier builder.