1use std::collections::hash_map::DefaultHasher;
33use std::hash::{Hash, Hasher};
34use std::time::{Duration, Instant};
35
36use reqwest::header::HeaderMap;
37use reqwest::StatusCode;
38
39use crate::config::Config;
40
41pub const DEFAULT_MAX_RETRIES: u32 = 3;
43pub const DEFAULT_RETRY_BASE_MS: u64 = 200;
45pub const DEFAULT_RETRY_MAX_DELAY_MS: u64 = 30_000;
47pub const HARD_MAX_RETRIES: u32 = 10;
49pub const HARD_MAX_DELAY_MS: u64 = 60_000;
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
57pub struct RetryConfig {
58 pub max_retries: u32,
60 pub base_ms: u64,
62 pub max_delay_ms: u64,
64 pub enabled: bool,
66}
67
68impl RetryConfig {
69 pub fn from_config(cfg: &Config) -> Self {
71 let max_retries = cfg.max_retries.min(HARD_MAX_RETRIES);
72 let base_ms = if cfg.retry_base_ms == 0 {
73 DEFAULT_RETRY_BASE_MS
74 } else {
75 cfg.retry_base_ms
76 };
77 let max_delay_ms = cfg
78 .retry_max_delay_ms
79 .min(HARD_MAX_DELAY_MS)
80 .max(base_ms);
81 let enabled = !cfg.disable_retry && max_retries > 0;
82 Self {
83 max_retries,
84 base_ms,
85 max_delay_ms,
86 enabled,
87 }
88 }
89
90 pub fn max_attempts(self) -> u32 {
92 if !self.enabled {
93 1
94 } else {
95 self.max_retries.saturating_add(1)
96 }
97 }
98
99 pub fn may_retry(self, attempt: u32) -> bool {
101 self.enabled && attempt < self.max_attempts()
102 }
103
104 pub fn delay_for_attempt(self, attempt: u32) -> Duration {
106 backoff_full_jitter(self.base_ms, attempt, self.max_delay_ms)
107 }
108}
109
110impl Default for RetryConfig {
111 fn default() -> Self {
112 Self {
113 max_retries: DEFAULT_MAX_RETRIES,
114 base_ms: DEFAULT_RETRY_BASE_MS,
115 max_delay_ms: DEFAULT_RETRY_MAX_DELAY_MS,
116 enabled: true,
117 }
118 }
119}
120
121#[derive(Debug, Clone, Copy, PartialEq, Eq)]
123pub enum RetryClass {
124 Permanent,
126 Transient,
128 RateLimited {
130 retry_after: Option<Duration>,
132 },
133}
134
135pub fn classify_http_status(status: StatusCode, retry_after: Option<Duration>) -> RetryClass {
137 match status.as_u16() {
138 429 => RetryClass::RateLimited { retry_after },
139 500 | 502 | 504 => RetryClass::Transient,
140 503 => {
142 if retry_after.is_some() {
143 RetryClass::RateLimited { retry_after }
144 } else {
145 RetryClass::Transient
146 }
147 }
148 _ => RetryClass::Permanent,
150 }
151}
152
153pub fn parse_retry_after(headers: &HeaderMap) -> Option<Duration> {
158 let raw = headers
159 .get(reqwest::header::RETRY_AFTER)
160 .and_then(|v| v.to_str().ok())?
161 .trim();
162 if raw.is_empty() || !raw.bytes().all(|b| b.is_ascii_digit()) {
164 return None;
165 }
166 let secs: u64 = raw.parse().ok()?;
167 Some(Duration::from_secs(secs.min(HARD_MAX_DELAY_MS / 1000)))
169}
170
171pub fn backoff_full_jitter(base_ms: u64, attempt: u32, max_delay_ms: u64) -> Duration {
176 let base = base_ms.max(1);
177 let max_delay = max_delay_ms.max(base);
178 let exp = base.saturating_mul(1u64 << attempt.min(16));
180 let cap = exp.min(max_delay);
181 let pick = mix_u64(attempt) % (cap.saturating_add(1));
182 Duration::from_millis(pick)
183}
184
185pub fn politeness_delay(base: Duration) -> Duration {
195 let base_ms = base.as_millis() as u64;
196 if base_ms == 0 {
197 return Duration::ZERO;
198 }
199 let span = (base_ms / 5).max(1);
200 let extra = mix_u64(base_ms as u32) % (span.saturating_add(1));
201 Duration::from_millis(base_ms.saturating_add(extra))
202}
203
204pub fn wait_for_retry(
206 policy: RetryConfig,
207 attempt: u32,
208 retry_after: Option<Duration>,
209) -> Duration {
210 if let Some(d) = retry_after {
211 return d.min(Duration::from_millis(policy.max_delay_ms));
213 }
214 policy.delay_for_attempt(attempt)
215}
216
217fn mix_u64(attempt: u32) -> u64 {
218 let mut h = DefaultHasher::new();
219 Instant::now().hash(&mut h);
220 attempt.hash(&mut h);
221 std::thread::current().id().hash(&mut h);
222 let marker = &h as *const DefaultHasher as usize;
224 marker.hash(&mut h);
225 h.finish()
226}
227
228#[cfg(test)]
229mod tests {
230 use super::*;
231 use reqwest::header::{HeaderValue, RETRY_AFTER};
232
233 #[test]
234 fn max_attempts_respects_kill_switch() {
235 let mut p = RetryConfig::default();
236 assert_eq!(p.max_attempts(), 4);
237 p.enabled = false;
238 assert_eq!(p.max_attempts(), 1);
239 assert!(!p.may_retry(1));
240 }
241
242 #[test]
243 fn from_config_disable_and_clamp() {
244 let cfg = Config {
245 disable_retry: true,
246 max_retries: 99,
247 retry_max_delay_ms: 999_999,
248 ..Config::default()
249 };
250 let p = RetryConfig::from_config(&cfg);
251 assert!(!p.enabled);
252 assert_eq!(p.max_retries, HARD_MAX_RETRIES);
253 assert_eq!(p.max_delay_ms, HARD_MAX_DELAY_MS);
254 assert_eq!(p.max_attempts(), 1);
255 }
256
257 #[test]
258 fn backoff_respects_cap() {
259 let d = backoff_full_jitter(200, 20, 1_000);
260 assert!(d.as_millis() <= 1_000);
261 }
262
263 #[test]
264 fn politeness_delay_zero_stays_zero() {
265 assert_eq!(politeness_delay(Duration::ZERO), Duration::ZERO);
266 }
267
268 #[test]
269 fn politeness_delay_never_below_floor() {
270 let base = Duration::from_millis(1000);
271 for _ in 0..32 {
272 let d = politeness_delay(base);
273 assert!(d >= base, "delay {d:?} below floor {base:?}");
274 assert!(
276 d <= base + Duration::from_millis(200),
277 "delay {d:?} above floor+20%"
278 );
279 }
280 }
281
282 #[test]
283 fn classify_matrix() {
284 assert_eq!(
285 classify_http_status(StatusCode::OK, None),
286 RetryClass::Permanent
287 );
288 assert_eq!(
289 classify_http_status(StatusCode::NOT_FOUND, None),
290 RetryClass::Permanent
291 );
292 assert_eq!(
293 classify_http_status(StatusCode::BAD_REQUEST, None),
294 RetryClass::Permanent
295 );
296 assert_eq!(
297 classify_http_status(StatusCode::INTERNAL_SERVER_ERROR, None),
298 RetryClass::Transient
299 );
300 assert_eq!(
301 classify_http_status(StatusCode::TOO_MANY_REQUESTS, Some(Duration::from_secs(2))),
302 RetryClass::RateLimited {
303 retry_after: Some(Duration::from_secs(2))
304 }
305 );
306 assert_eq!(
307 classify_http_status(StatusCode::SERVICE_UNAVAILABLE, Some(Duration::from_secs(1))),
308 RetryClass::RateLimited {
309 retry_after: Some(Duration::from_secs(1))
310 }
311 );
312 assert_eq!(
313 classify_http_status(StatusCode::SERVICE_UNAVAILABLE, None),
314 RetryClass::Transient
315 );
316 }
317
318 #[test]
319 fn parse_retry_after_seconds_only() {
320 let mut h = HeaderMap::new();
321 h.insert(RETRY_AFTER, HeaderValue::from_static("7"));
322 assert_eq!(parse_retry_after(&h), Some(Duration::from_secs(7)));
323
324 h.insert(RETRY_AFTER, HeaderValue::from_static("Wed, 21 Oct 2015 07:28:00 GMT"));
325 assert_eq!(parse_retry_after(&h), None);
326
327 h.insert(RETRY_AFTER, HeaderValue::from_static(""));
328 assert_eq!(parse_retry_after(&h), None);
329 }
330
331 #[test]
332 fn wait_prefers_retry_after() {
333 let p = RetryConfig::default();
334 let w = wait_for_retry(p, 1, Some(Duration::from_secs(3)));
335 assert_eq!(w, Duration::from_secs(3));
336 }
337}