throttle_kit/metrics.rs
1use std::time::{Duration, Instant};
2
3/// Result of a rate limit check.
4#[derive(Debug, Clone)]
5pub struct RateLimitResult {
6 /// Whether the request was allowed.
7 pub allowed: bool,
8 /// Remaining requests in the current window.
9 pub remaining: u64,
10 /// When the next token becomes available.
11 pub reset_at: Instant,
12 /// The total rate limit for this window.
13 pub limit: u64,
14 /// When to retry if the request was rejected.
15 pub retry_after: Option<Duration>,
16}
17
18impl RateLimitResult {
19 /// Build standard rate limit HTTP headers.
20 ///
21 /// Returns `(header_name, header_value)` pairs for:
22 /// - `X-RateLimit-Limit`
23 /// - `X-RateLimit-Remaining`
24 /// - `X-RateLimit-Reset`
25 pub fn headers(&self) -> Vec<(&str, String)> {
26 vec![
27 ("X-RateLimit-Limit", self.limit.to_string()),
28 ("X-RateLimit-Remaining", self.remaining.to_string()),
29 (
30 "X-RateLimit-Reset",
31 self.reset_at
32 .duration_since(Instant::now())
33 .as_secs()
34 .to_string(),
35 ),
36 ]
37 }
38}