Expand description
§Distributed Rate Limiting System
This crate provides a rate limiter with integrated PID controller to manage request rates dynamically. It is designed for distributed systems where controlling the rate of requests is crucial for stability and performance.
§Example
The following example demonstrates how to use the RateLimiter
with the builder pattern to create
a rate limiter instance and make throttling decisions based on the request rate.
use nenya::RateLimiterBuilder;
use nenya::pid_controller::PIDControllerBuilder;
use std::time::Duration;
// Create a PID controller with specific parameters
let pid_controller = PIDControllerBuilder::new(10.0)
.kp(1.0)
.ki(0.1)
.kd(0.01)
.build();
// Create a rate limiter using the builder pattern
let mut rate_limiter = RateLimiterBuilder::new(10.0)
.min_rate(5.0)
.max_rate(15.0)
.pid_controller(pid_controller)
.update_interval(Duration::from_secs(1))
.build();
// Simulate request processing and check if throttling is necessary
for _ in 0..20 {
if rate_limiter.should_throttle() {
println!("Request throttled");
} else {
println!("Request accepted");
}
}
Modules§
Structs§
- Rate
Limiter - Sliding window rate limiter with an integrated PID controller for dynamic target rate adjustment.
- Rate
Limiter Builder - Builder for creating a
RateLimiter
instance.