request-rate-limiter

Crates.iorequest-rate-limiter
lib.rsrequest-rate-limiter
version0.2.3
created_at2025-10-17 15:30:10.439848+00
updated_at2025-11-01 14:05:56.285032+00
descriptionRequest rate limiting with adaptive algorithms for controlling request throughput
homepagehttps://github.com/shajapas/request-rate-limiter
repositoryhttps://github.com/shajapas/request-rate-limiter
max_upload_size
id1887801
size45,539
Konstantin (ShaJaPas)

documentation

https://docs.rs/request-rate-limiter

README

request-rate-limiter

Dynamic request rate limiting with adaptive algorithms for controlling request throughput.

Crates.io Documentation License

Overview

A Rust library for request rate limiting using adaptive algorithms. The library provides rate limiting that automatically adjusts request rates based on success/failure patterns, helping prevent overload while maximizing throughput.

P.S. The idea was taken from congestion-limiter crate

Features

  • Adaptive Rate Limiting: Automatically adjusts request rates based on success/failure patterns
  • Multiple Algorithms: More than 2 in the future. Or you can make your own :)
  • Async/Await Support: Built for modern Rust async applications
  • Thread-Safe: Safe for use in concurrent environments
  • Real-time Adaptation: Responds to system feedback in real-time

Algorithms

AIMD (Additive Increase Multiplicative Decrease)

Loss-based rate limiting that increases the rate linearly on success and decreases it multiplicatively on overload.

  • Increases rate: When requests succeed
  • Decreases rate: When overload is detected (timeouts, 429/503 responses)
  • Best for: Systems with clear overload signals

Fixed Rate

Simple, constant rate limiting that maintains a fixed requests-per-second limit.

  • Constant rate: Never changes regardless of outcomes
  • Best for: Predictable workloads with known capacity limits

Installation

Add this to your Cargo.toml:

[dependencies]
request-rate-limiter = "0.1.0"

Quick Start

use std::sync::Arc;
use request_rate_limiter::{
    algorithms::Aimd,
    limiter::{DefaultRateLimiter, RateLimiter, RequestOutcome}
};

#[tokio::main]
async fn main() {
    // Create a rate limiter with AIMD algorithm
    let limiter = Arc::new(DefaultRateLimiter::new(
        Aimd::new_with_initial_rate(10)
            .decrease_factor(0.9)
            .increase_by(1)
    ));

    // Acquire permission to make a request
    let token = limiter.acquire().await;
    
    // Simulate doing work
    tokio::time::sleep(std::time::Duration::from_millis(100)).await;
    
    // Release the token with outcome
    limiter.release(token, Some(RequestOutcome::Success)).await;
    
    println!("Request completed successfully!");
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Commit count: 0

cargo fmt