### Initialize and Use Leaky Bucket Rate Limiter with Reqwest Middleware (Rust) Source: https://github.com/xdarksome/reqwest-leaky-bucket/blob/main/README.md Demonstrates how to set up a leaky bucket rate limiter with custom capacity, initial fill, and refill rates. It then integrates this limiter into a reqwest client using reqwest-middleware to apply rate limiting to all outgoing requests. ```rust use reqwest_leaky_bucket::leaky_bucket::RateLimiter; use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; async fn run() { let limiter = RateLimiter::builder().max(10).initial(0).refill(5).build(); let client = ClientBuilder::new(reqwest::Client::new()) .with(reqwest_leaky_bucket::rate_limit_all(limiter)) .build(); client.get("https://crates.io").send().await.unwrap(); } ``` -------------------------------- ### Create Rate-Limited HTTP Client with Leaky Bucket Source: https://context7.com/xdarksome/reqwest-leaky-bucket/llms.txt Demonstrates how to create a reqwest client with rate-limiting middleware using the leaky bucket algorithm. It configures the rate limiter with maximum burst capacity, initial permits, and refill rate, then applies it to a reqwest client. ```rust use reqwest_leaky_bucket::leaky_bucket::RateLimiter; use reqwest_middleware::{ClientBuilder, ClientWithMiddleware}; #[tokio::main] async fn main() -> Result<(), Box> { // Create a rate limiter with: // - max: 10 permits (burst capacity) // - initial: 0 permits available at start // - refill: 5 permits added per interval let limiter = RateLimiter::builder() .max(10) .initial(0) .refill(5) .build(); // Wrap the reqwest client with rate-limiting middleware let client: ClientWithMiddleware = ClientBuilder::new(reqwest::Client::new()) .with(reqwest_leaky_bucket::rate_limit_all(limiter)) .build(); // All requests through this client are now rate-limited let response = client .get("https://api.example.com/data") .send() .await?; println!("Status: {}", response.status()); Ok(()) } ``` -------------------------------- ### Configure Leaky Bucket Rate Limiter for API Limits Source: https://context7.com/xdarksome/reqwest-leaky-bucket/llms.txt Shows how to configure a leaky bucket rate limiter to match specific API rate limits, such as 100 requests per minute. It demonstrates setting the maximum burst capacity, initial permits, and refill rate to control request flow. ```rust use reqwest_leaky_bucket::leaky_bucket::RateLimiter; use reqwest_middleware::ClientBuilder; #[tokio::main] async fn main() -> Result<(), Box> { // Configure rate limiter for an API allowing 100 requests per minute // (approximately 1.67 requests per second) let limiter = RateLimiter::builder() .max(100) // Maximum burst capacity .initial(100) // Start with full capacity .refill(2) // Refill 2 permits per interval (default 1 second) .build(); let client = ClientBuilder::new(reqwest::Client::new()) .with(reqwest_leaky_bucket::rate_limit_all(limiter)) .build(); // Make multiple requests - they will be automatically throttled for i in 0..10 { let resp = client .get(format!("https://api.example.com/item/{}", i)) .send() .await?; println!("Request {}: {}", i, resp.status()); } Ok(()) } ``` -------------------------------- ### Use Middleware Type Alias for Custom Rate Limiter Clients Source: https://context7.com/xdarksome/reqwest-leaky-bucket/llms.txt Illustrates using the `Middleware` type alias for advanced scenarios, such as creating multiple reqwest clients with different rate-limiting policies. This allows for granular control over request throttling for various API endpoints. ```rust use reqwest_leaky_bucket::leaky_bucket::RateLimiter; use reqwest_leaky_bucket::Middleware; use reqwest_middleware::ClientBuilder; #[tokio::main] async fn main() -> Result<(), Box> { // Create multiple clients with different rate limits let aggressive_limiter = RateLimiter::builder() .max(5) .initial(5) .refill(1) .build(); let conservative_limiter = RateLimiter::builder() .max(50) .initial(50) .refill(10) .build(); // Client for rate-limited API endpoints let limited_client = ClientBuilder::new(reqwest::Client::new()) .with(reqwest_leaky_bucket::rate_limit_all(aggressive_limiter)) .build(); // Client for less restricted endpoints let standard_client = ClientBuilder::new(reqwest::Client::new()) .with(reqwest_leaky_bucket::rate_limit_all(conservative_limiter)) .build(); // Use appropriate client based on endpoint restrictions let limited_resp = limited_client .get("https://api.example.com/limited-endpoint") .send() .await?; let standard_resp = standard_client .get("https://api.example.com/standard-endpoint") .send() .await?; Ok(()) } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.