Axum Rate Limiter
A flexible and powerful rate limiting middleware for Axum web framework that can be configured to limit requests based on various strategies including IP, URL, Headers, Query parameters, and Request body content.
Quick Start
Add the dependency to your Cargo.toml:
[]
= "0.1.0"
Basic usage example:
use SocketAddr;
use Arc;
use ;
use Body;
use ;
use from_fn_with_state;
use Response;
use Settings;
use ;
// Simple handler that returns "Hello, World!"
async
async
Features
- Multiple rate limiting strategies
- Redis-based token bucket implementation
- IP whitelisting
- Configurable token bucket parameters
- Support for global and per-value rate limits
- Easy configuration through TOML file
- Flexible configuration path setup via environment variables
Configuration
The rate limiter is configured through a Settings.toml file. You can specify the path to your configuration file using the RL_SETTINGS_PATH environment variable. If not specified, the rate limiter will look for the Settings.toml file in the current directory.
# Example of setting custom configuration path
Here's a detailed breakdown of the configuration options:
Rate Limiter Base Configuration
[]
= "redis:6379" # Redis server address for token bucket storage
= ["127.0.0.1"] # List of IPs that bypass rate limiting
Rate Limiting Strategies
The rate limiter supports multiple strategies that can be configured simultaneously. Each strategy is defined under [[rate_limiter.limiter]] section.
Available Strategies
- URL-based Rate Limiting
[[]]
= "url"
= { = 10, = 120 } # Global limit for all URLs
= [
{ = "/example", = 1, = 10 }, # Specific limit for /hello
{ = "/", = 5, = 10 } # Specific limit for /
]
- IP-based Rate Limiting
[[]]
= "ip"
= { = 10, = 120 } # Limit requests per IP
- Header-based Rate Limiting
[[]]
= "header"
= { = 3, = 120 }
= [
{ = "X-Telegram-User-Token", = 1, = 100 },
]
- Query Parameter Rate Limiting
[[]]
= "query"
= [
{ = "user_id", = 1, = 30 },
]
- Request Body Rate Limiting
[[]]
= "body"
= [
{ = "user_id", = 1, = 30 },
]
Configuration Parameters Explained
strategy: The type of rate limiting to apply (url,ip,header,query, orbody)global_bucket: Global rate limit settings that apply to all values for the strategytokens_count: Number of tokens (requests) allowedadd_tokens_every: Time in seconds after which tokens are replenished
buckets_per_value: Specific rate limits for individual valuesvalue: The specific value to apply the limit to (e.g., URL path, header name, query parameter)tokens_count: Number of tokens (requests) allowed for this specific valueadd_tokens_every: Time in seconds after which tokens are replenished for this value
Usage Examples
Example 1: Basic URL Rate Limiting
[[]]
= "url"
= [
{ = "/api/users", = 5, = 60 }, # 5 requests per minute
]
Example 2: Combined IP and Header Rate Limiting
[[]]
= "ip"
= { = 100, = 3600 } # 100 requests per hour per IP
[[]]
= "header"
= [
{ = "API-Key", = 1000, = 3600 }, # 1000 requests per hour per API key
]
Error Responses
When rate limits are exceeded, the service will return:
- HTTP Status Code: 429 (Too Many Requests)
- A message indicating the rate limit has been exceeded
Notes
- The rate limiter uses a token bucket algorithm implemented with Redis
- Whitelisted IPs bypass all rate limiting rules
- Each strategy can have both global and specific limits (Except
queryandbody) - Token buckets are replenished gradually over time
- Configuration changes require service restart to take effect