Expand description
Rate limiter policy using a token bucket algorithm.
The rate limiter controls the rate of operations by maintaining a bucket of tokens that refill at regular intervals.
§How It Works
- Each operation consumes one token
- If no tokens are available, the operation is rejected
- Tokens refill to capacity after each interval
§Examples
use do_over::{policy::Policy, rate_limit::RateLimiter, error::DoOverError};
use std::time::Duration;
// Allow 100 requests per second
let limiter = RateLimiter::new(100, Duration::from_secs(1));
match limiter.execute(|| async {
Ok::<_, DoOverError<std::io::Error>>("completed")
}).await {
Ok(result) => println!("Success: {}", result),
Err(DoOverError::BulkheadFull) => println!("Rate limit exceeded"),
Err(e) => println!("Error: {:?}", e),
}Structs§
- Rate
Limiter - A token bucket rate limiter.