| Crates.io | rate_limiter_aimd |
| lib.rs | rate_limiter_aimd |
| version | 0.1.1 |
| created_at | 2025-05-30 15:56:57.475551+00 |
| updated_at | 2025-06-01 01:42:29.500061+00 |
| description | A Rust library for adaptive concurrency control based on network service feedback (forked from vector.dev) |
| homepage | https://github.com/TwistingTwists/rate_limiter_aimd |
| repository | https://github.com/TwistingTwists/rate_limiter_aimd |
| max_upload_size | |
| id | 1695258 |
| size | 251,389 |
An adaptive concurrency limiter using the AIMD (Additive Increase Multiplicative Decrease) algorithm.
This crate implements an adaptive concurrency controller that dynamically adjusts the maximum number of concurrent requests based on observed response times. It uses the AIMD algorithm:
The controller helps prevent overloading services by automatically finding the optimal concurrency level.
Add to your Cargo.toml:
rate_limiter_aimd = "0.1"
Basic example with reqwest:
use rate_limiter_aimd::adaptive_concurrency::AdaptiveConcurrencyLayer;
use reqwest_middleware::ClientBuilder;
async fn make_requests() {
let client = reqwest::Client::new();
let adaptive_layer = AdaptiveConcurrencyLayer::new();
let client = ClientBuilder::new(client).with(adaptive_layer).build();
// Use client to make requests (concurrency will be automatically limited)
}
Create settings with:
use rate_limiter_aimd::adaptive_concurrency::AdaptiveConcurrencySettings;
let settings = AdaptiveConcurrencySettings::default()
.initial_concurrency(10)
.max_concurrency_limit(100)
.decrease_ratio(0.8);
Available parameters:
initial_concurrency: Starting concurrency limit (default: 1)max_concurrency_limit: Maximum allowed concurrency (default: 200)decrease_ratio: Multiplier for decreasing concurrency (default: 0.9)ewma_alpha: Smoothing factor for latency measurements (default: 0.4)rtt_deviation_scale: Threshold for latency increases (default: 2.5)The controller uses a ResponseClassifier to determine if responses indicate backpressure. By default, it classifies:
x-ratelimit-remaining: 0 as backpressureCustom classifiers can be implemented.
The controller:
(1 + rtt_deviation_scale) * EWMATo run the OpenAI chat example:
export OPENAI_API_KEY='your-api-key'
cargo run --example openai_chat
This demonstrates:
This work is mostly taken from vector.dev's implementation of adaptive concurrency. Changes lie in ExponentialPolicy and DefaultReqwestRetryLogic.
This project is under MPL: Mozilla Public License 2.0
See LICENSE for details.