Crate route_ratelimit

Crate route_ratelimit 

Source
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:

  • /order matches /order, /order/, and /order/123
  • /order does NOT match /orders or /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§

HostBuilder
Builder for configuring routes within a specific host scope.
HostRouteBuilder
Builder for configuring a single route within a host scope.
RateLimit
A single rate limit configuration.
RateLimitBuilder
Builder for configuring a RateLimitMiddleware.
RateLimitMiddleware
The rate limiting middleware.
Route
A route definition that matches requests and applies rate limits.
RouteBuilder
Builder for configuring a single route (without host scope).

Enums§

RateLimitError
Errors that can occur during rate limiting.
ThrottleBehavior
Behavior when a rate limit is exceeded.