Crates.io | nenya |
lib.rs | nenya |
version | 0.0.2 |
source | src |
created_at | 2024-05-25 16:14:36.78948 |
updated_at | 2024-05-30 04:23:16.666783 |
description | A Rust crate for adaptive rate limiting and PID control. |
homepage | |
repository | https://github.com/emersonmde/nenya |
max_upload_size | |
id | 1252048 |
size | 157,016 |
Nenya is an adaptive rate limiter using a Proportional-Integral-Derivative (PID) controller. This project contains two major components:
Nenya is a Rust crate that offers adaptive rate limiting functionality using a PID controller. The crate aims to provide a dynamic and efficient way to manage request rates, making it suitable for high-throughput services.
kp
, ki
, kd
),
error limits, output limits, and update intervalsNenya-Sentinel is a standalone rate limiting service that will support gRPC for easy integration as a sidecar in microservice architectures.
To get started with Nenya, add it to your Cargo.toml:
[dependencies]
nenya = "0.0.2"
A basic rate limiter with a static set point:
use nenya::RateLimiterBuilder;
use nenya::pid_controller::PIDControllerBuilder;
use std::time::Duration;
fn main() {
// Create a rate limiter
let mut rate_limiter = RateLimiterBuilder::new(10.0)
.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");
}
}
}
A dynamic rate limiter using a PID Controller:
use nenya::RateLimiterBuilder;
use nenya::pid_controller::PIDControllerBuilder;
use std::time::Duration;
fn main() {
// 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 PID Controller
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");
}
}
}
Nenya includes a request simulation example for testing and tuning. You can run the simulation with:
cargo run --example request_simulator_plot -- \
--target_tps 80.0 \
--min_tps 75.0 \
--max_tps 100.0 \
--trailing_window 1 \
--duration 120 \
--base_tps 80.0 \
--amplitudes 20.0,7.0,10.0 \
--frequencies 0.05,2.8,4.0 \
--kp 0.8 \
--ki 0.05 \
--kd 0.04 \
--error_limit 10.0 \
--output_limit 3.0 \
--update_interval 500 \
--error_bias 0.0
Most of these arguments have sane defaults and can be omitted. For more details see:
cargo run --example request_simulator_plot -- --help
The rate limiter achieves an adaptive rate limit using a Proportional–Integral–Derivative (PID) controller which determines the target rate limit based on the request rate. This implementation includes error bias, accumulated error clamping, anti-windup feedback, and output clamping.
The error $e(t)$ is calculated as the difference between the setpoint $S$ and the request rate $r(t)$:
e(t) = S - r(t)
The proportional term $P(t)$ is computed using the proportional gain $K_p$:
P(t) = K_p \cdot e(t)
The error is adjusted by a bias $B$ to react more to positive or negative errors:
\text{biased\_error}(t) =
\begin{cases}
e(t) \cdot (1 + B) & \text{if } e(t) > 0 \\
e(t) \cdot (1 - B) & \text{if } e(t) \leq 0
\end{cases}
The accumulated error $E(t)$ is clamped to prevent integral windup:
E(t) = \text{clamp}\left( E(t-1) + \text{biased\_error}(t), -L, L \right)
where $L$ is the error limit.
The integral term $I(t)$ is then:
I(t) = K_i \cdot E(t)
The derivative term $D(t)$ is computed using the derivative gain $K_d$ and the rate of change of the error:
D(t) = K_d \cdot \frac{d e(t)}{dt}
For discrete time steps, this can be approximated as:
D(t) = K_d \cdot \left( e(t) - e(t-1) \right)
The raw correction $u(t)$ is the sum of the proportional, integral, and derivative terms:
u(t) = P(t) + I(t) + D(t)
The output correction is clamped to prevent excessive output:
u_{\text{clamped}}(t) = \text{clamp}(u(t), -M, M)
where $M$ is the output limit.
If the correction is clamped, the accumulated error $E(t)$ is adjusted to prevent windup:
\text{if } u(t) \neq u_{\text{clamped}}(t) \text{ then } E(t) = E(t) - \frac{u(t) - u_{\text{clamped}}(t)}{K_i}
The final output of the PID controller is:
u_{\text{clamped}}(t)
The output is added to the current request limit $R(t-1)$ to derive the new request limit $R(t)$:
R(t) = R(t-1) + u_{\text{clamped}}(t)
This project is licensed under the MIT License. See the LICENSE file for more details.