Skip to main content

request_rate_limiter/
algorithms.rs

1//! Algorithms for controlling request rate limits.
2
3/// Additive Increase Multiplicative Decrease algorithm.
4mod 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/// An algorithm for controlling request rate limits.
16#[async_trait]
17pub trait RateLimitAlgorithm {
18    /// The current requests per second limit.
19    fn requests_per_second(&self) -> u64;
20
21    /// Update the rate limit in response to a request completion.
22    async fn update(&self, sample: RequestSample) -> u64;
23}
24
25/// The result of a request, including the [RequestOutcome] and response time.
26#[derive(Debug, Clone, PartialEq, Eq)]
27pub struct RequestSample {
28    /// Response time for the request
29    pub response_time: Duration,
30    /// Current requests per second when the sample was taken
31    pub current_rps: u64,
32    /// Outcome of the request
33    pub outcome: RequestOutcome,
34    /// Timestamp when the request was made
35    pub timestamp: std::time::Instant,
36}
37
38impl RequestSample {
39    /// Create a new request sample
40    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}