Expand description
Route-based rate limiting middleware for reqwest.
This crate provides a RateLimitMiddleware that can be used with
reqwest_middleware to enforce rate limits based on endpoint matching.
§Features
- Endpoint matching: Match requests by host, HTTP method, and path prefix
- Multiple rate limits: Stack burst and sustained limits on the same endpoint
- Configurable behavior: Choose to delay requests or return errors per endpoint
- Lock-free performance: Uses GCRA algorithm with atomic operations
- Shared state: Rate limits are tracked across all client clones
§Route Matching Behavior
Routes are checked in the order they are defined, and all matching routes’ limits are applied. This means you can layer general limits with specific ones:
use route_ratelimit::RateLimitMiddleware;
use std::time::Duration;
let middleware = RateLimitMiddleware::builder()
// General limit: 9000 requests per 10 seconds for all endpoints
.host("api.example.com", |host| {
host.route(|r| r.limit(9000, Duration::from_secs(10)))
// Specific limit: /book endpoints also limited to 1500/10s
// Both limits are enforced - a request to /book must pass BOTH
.route(|r| r.path("/book").limit(1500, Duration::from_secs(10)))
})
.build();§Host Matching
Host matching uses only the hostname portion of the URL, excluding the port.
For example, host("api.example.com") will match https://api.example.com:8443/path.
§Path Matching
Path matching uses segment boundaries, not simple prefix matching:
/ordermatches/order,/order/, and/order/123/orderdoes NOT match/ordersor/order-test
§Example
use route_ratelimit::{RateLimitMiddleware, ThrottleBehavior};
use reqwest_middleware::ClientBuilder;
use std::time::Duration;
use http::Method;
let middleware = RateLimitMiddleware::builder()
// Configure routes by host for clean organization
.host("clob.polymarket.com", |host| {
host.route(|r| r.limit(9000, Duration::from_secs(10))) // General limit
.route(|r| r.path("/book").limit(1500, Duration::from_secs(10)))
.route(|r| r.path("/price").limit(1500, Duration::from_secs(10)))
.route(|r| {
r.method(Method::POST)
.path("/order")
.limit(3500, Duration::from_secs(10)) // Burst
.limit(36000, Duration::from_secs(600)) // Sustained
})
})
.build();
let client = ClientBuilder::new(reqwest::Client::new())
.with(middleware)
.build();
// Requests will be automatically rate-limited
client.get("https://clob.polymarket.com/book").send().await.unwrap();Structs§
- Host
Builder - Builder for configuring routes within a specific host scope.
- Host
Route Builder - Builder for configuring a single route within a host scope.
- Rate
Limit - A single rate limit configuration.
- Rate
Limit Builder - Builder for configuring a
RateLimitMiddleware. - Rate
Limit Middleware - The rate limiting middleware.
- Route
- A route definition that matches requests and applies rate limits.
- Route
Builder - Builder for configuring a single route (without host scope).
Enums§
- Rate
Limit Error - Errors that can occur during rate limiting.
- Throttle
Behavior - Behavior when a rate limit is exceeded.