stynx_code_services/rate_limit/
mod.rs1#[derive(Debug, Clone)]
2pub struct RateLimitInfo {
3 pub retry_after_ms: u64,
4 pub limit_type: String,
5 pub message: String,
6}
7
8pub fn format_rate_limit_message(info: &RateLimitInfo) -> String {
9 let secs = info.retry_after_ms / 1000;
10 if secs > 0 {
11 format!(
12 "Rate limited ({}): {}. Retry in {}s.",
13 info.limit_type, info.message, secs
14 )
15 } else {
16 format!("Rate limited ({}): {}", info.limit_type, info.message)
17 }
18}
19
20pub fn parse_rate_limit_headers(status: u16, retry_after: Option<&str>) -> Option<RateLimitInfo> {
21 if status != 429 {
22 return None;
23 }
24
25 let retry_after_ms = retry_after
26 .and_then(|v| v.parse::<f64>().ok())
27 .map(|secs| (secs * 1000.0) as u64)
28 .unwrap_or(60_000);
29
30 Some(RateLimitInfo {
31 retry_after_ms,
32 limit_type: "api".to_string(),
33 message: "Too many requests".to_string(),
34 })
35}