1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/// Rate limit information extracted from response headers.
///
/// Envoy's rate limiter returns these headers in the IETF draft format:
/// - `x-ratelimit-limit`: e.g. `"50, 50;w=60"` (total quota for the window)
/// - `x-ratelimit-remaining`: e.g. `"40"` (remaining budget in window)
/// - `x-ratelimit-reset`: e.g. `"41"` (seconds until window resets)
/// - `x-ratelimit-cost`: e.g. `"10"` (budget consumed per request)
#[derive(Debug, Clone, Default)]
pub struct RateLimitInfo {
/// Total request quota for the current window.
///
/// Parsed from `x-ratelimit-limit`. For IETF draft format like `"50, 50;w=60"`,
/// the first integer before the comma is used.
pub limit: Option<u64>,
/// Remaining budget in the current window.
///
/// Parsed from `x-ratelimit-remaining`. Note this is budget units, not request count.
/// Divide by [`cost`](Self::cost) to get the number of requests remaining.
pub remaining: Option<u64>,
/// Seconds until the rate limit window resets.
///
/// Parsed from `x-ratelimit-reset`.
pub reset_secs: Option<u64>,
/// Budget consumed per request.
///
/// Parsed from `x-ratelimit-cost`. For example, if `limit` is 50 and `cost` is 10,
/// you can make 5 requests per window.
pub cost: Option<u64>,
}
impl std::fmt::Display for RateLimitInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut parts = Vec::new();
if let (Some(remaining), Some(limit)) = (self.remaining, self.limit) {
let cost = self.cost.unwrap_or(1);
parts.push(format!(
"remaining={}/{} reqs",
remaining / cost,
limit / cost,
));
} else {
if let Some(remaining) = self.remaining {
parts.push(format!("remaining={remaining}"));
}
if let Some(limit) = self.limit {
parts.push(format!("limit={limit}"));
}
}
if let Some(reset) = self.reset_secs {
parts.push(format!("resets_in={reset}s"));
}
write!(f, "{}", parts.join(", "))
}
}
impl RateLimitInfo {
/// Extracts rate limit information from HTTP response headers.
///
/// All parsing is best-effort: missing or unparseable headers become `None`.
pub(crate) fn from_response(res: &reqwest::Response) -> Self {
Self {
limit: Self::parse_limit_header(res),
remaining: Self::parse_u64_header(res, "x-ratelimit-remaining"),
reset_secs: Self::parse_u64_header(res, "x-ratelimit-reset"),
cost: Self::parse_u64_header(res, "x-ratelimit-cost"),
}
}
/// Returns `true` if the rate limit quota has been exhausted.
pub fn is_rate_limited(&self) -> bool {
self.remaining == Some(0)
}
/// Returns the suggested number of seconds to wait before making another request.
pub fn suggested_wait_secs(&self) -> Option<u64> {
self.reset_secs
}
/// Parses `x-ratelimit-limit` which uses IETF draft format: `"60, 60;w=60"`.
/// Extracts the first integer before the comma.
fn parse_limit_header(res: &reqwest::Response) -> Option<u64> {
let value = res.headers().get("x-ratelimit-limit")?.to_str().ok()?;
// Take first value before comma: "60, 60;w=60" -> "60"
let first = value.split(',').next()?.trim();
first.parse().ok()
}
/// Parses a simple u64 header value.
fn parse_u64_header(res: &reqwest::Response, name: &str) -> Option<u64> {
res.headers().get(name)?.to_str().ok()?.trim().parse().ok()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_is_rate_limited() {
let info = RateLimitInfo {
remaining: Some(0),
..Default::default()
};
assert!(info.is_rate_limited());
let info = RateLimitInfo {
remaining: Some(5),
..Default::default()
};
assert!(!info.is_rate_limited());
let info = RateLimitInfo::default();
assert!(!info.is_rate_limited());
}
#[test]
fn test_from_response_header_case_insensitive() {
// Build an http::Response with mixed-case headers, then convert to reqwest::Response.
// This confirms that HeaderMap normalizes names so our lowercase lookups match.
let http_resp = http::Response::builder()
.header("X-RateLimit-Remaining", "42")
.header("X-RATELIMIT-RESET", "30")
.header("X-Ratelimit-Limit", "100, 100;w=60")
.header("X-Ratelimit-Cost", "10")
.body("")
.unwrap();
let resp: reqwest::Response = http_resp.into();
let info = RateLimitInfo::from_response(&resp);
assert_eq!(info.limit, Some(100));
assert_eq!(info.remaining, Some(42));
assert_eq!(info.reset_secs, Some(30));
assert_eq!(info.cost, Some(10));
}
#[test]
fn test_suggested_wait_secs() {
// Uses reset_secs
let info = RateLimitInfo {
reset_secs: Some(30),
..Default::default()
};
assert_eq!(info.suggested_wait_secs(), Some(30));
// None when no info
let info = RateLimitInfo::default();
assert_eq!(info.suggested_wait_secs(), None);
}
#[test]
fn test_display_full() {
let info = RateLimitInfo {
limit: Some(50),
remaining: Some(0),
reset_secs: Some(59),
cost: Some(10),
};
assert_eq!(info.to_string(), "remaining=0/5 reqs, resets_in=59s");
}
#[test]
fn test_display_partial() {
let info = RateLimitInfo {
remaining: Some(3),
reset_secs: Some(30),
..Default::default()
};
assert_eq!(info.to_string(), "remaining=3, resets_in=30s");
}
#[test]
fn test_display_empty() {
let info = RateLimitInfo::default();
assert_eq!(info.to_string(), "");
}
}