request_rate_limiter/
algorithms.rs1mod aimd;
5mod fixed;
6
7use async_trait::async_trait;
8use std::time::Duration;
9
10use crate::limiter::RequestOutcome;
11
12pub use aimd::Aimd;
13pub use fixed::Fixed;
14
15#[async_trait]
17pub trait RateLimitAlgorithm {
18 fn requests_per_second(&self) -> u64;
20
21 async fn update(&self, sample: RequestSample) -> u64;
23}
24
25#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct RequestSample {
28 pub response_time: Duration,
30 pub current_rps: u64,
32 pub outcome: RequestOutcome,
34 pub timestamp: std::time::Instant,
36}
37
38impl RequestSample {
39 pub fn new(response_time: Duration, current_rps: u64, outcome: RequestOutcome) -> Self {
41 Self {
42 response_time,
43 current_rps,
44 outcome,
45 timestamp: std::time::Instant::now(),
46 }
47 }
48}