1use std::str::FromStr;
12use std::time::Duration;
13
14use http::StatusCode;
15use qubit_config::{
16 ConfigReader,
17 ConfigResult,
18};
19use qubit_retry::{
20 RetryDelay,
21 RetryJitter,
22 RetryOptions,
23};
24
25use super::http_retry_method_policy::HttpRetryMethodPolicy;
26use super::HttpConfigError;
27use crate::{
28 HttpErrorKind,
29 HttpRequest,
30};
31
32const DEFAULT_RETRY_MAX_ATTEMPTS: u32 = 3;
33const DEFAULT_RETRY_INITIAL_DELAY: Duration = Duration::from_millis(200);
34const DEFAULT_RETRY_MAX_DELAY: Duration = Duration::from_secs(5);
35const DEFAULT_RETRY_MULTIPLIER: f64 = 2.0;
36const DEFAULT_RETRY_JITTER_FACTOR: f64 = 0.1;
37
38#[derive(Debug, Clone, PartialEq)]
40pub struct HttpRetryOptions {
41 pub enabled: bool,
43 pub max_attempts: u32,
45 pub max_duration: Option<Duration>,
47 pub delay_strategy: RetryDelay,
49 pub jitter_factor: f64,
51 pub method_policy: HttpRetryMethodPolicy,
53 pub retry_status_codes: Option<Vec<StatusCode>>,
57 pub retry_error_kinds: Option<Vec<HttpErrorKind>>,
61}
62
63fn is_retryable_status(status: StatusCode, retry_status_codes: Option<&[StatusCode]>) -> bool {
67 if let Some(status_codes) = retry_status_codes {
68 status_codes.contains(&status)
69 } else {
70 default_retryable_status(status)
71 }
72}
73
74fn is_retryable_error_kind(kind: HttpErrorKind, retry_error_kinds: Option<&[HttpErrorKind]>) -> bool {
78 if let Some(error_kinds) = retry_error_kinds {
79 error_kinds.contains(&kind)
80 } else {
81 default_retryable_error_kind(kind)
82 }
83}
84
85impl HttpRetryOptions {
86 pub fn new() -> Self {
91 Self::default()
92 }
93
94 pub fn from_config<R>(config: &R) -> Result<Self, HttpConfigError>
102 where
103 R: ConfigReader + ?Sized,
104 {
105 let raw = Self::read_config(config).map_err(HttpConfigError::from)?;
106 let mut opts = Self::default();
107
108 if let Some(enabled) = raw.enabled {
109 opts.enabled = enabled;
110 }
111 if let Some(max_attempts) = raw.max_attempts {
112 opts.max_attempts = max_attempts;
113 }
114 opts.max_duration = raw.max_duration;
115 if let Some(jitter_factor) = raw.jitter_factor {
116 opts.jitter_factor = jitter_factor;
117 }
118 if let Some(method_policy) = raw.method_policy.as_ref() {
119 opts.method_policy = HttpRetryMethodPolicy::from_config_value(method_policy)?;
120 }
121 if let Some(status_codes) = raw.status_codes.as_ref() {
122 opts.retry_status_codes = Some(parse_retry_status_codes(status_codes)?);
123 }
124 if let Some(error_kinds) = raw.error_kinds.as_ref() {
125 opts.retry_error_kinds = Some(parse_retry_error_kinds(error_kinds)?);
126 }
127
128 if let Some(delay_strategy) = raw.delay_strategy.as_ref() {
129 opts.delay_strategy = parse_retry_delay_strategy(delay_strategy, &raw)?;
130 }
131
132 opts.validate()?;
133 Ok(opts)
134 }
135
136 fn read_config<R>(config: &R) -> ConfigResult<HttpRetryConfigInput>
145 where
146 R: ConfigReader + ?Sized,
147 {
148 Ok(HttpRetryConfigInput {
149 enabled: config.get_optional("enabled")?,
150 max_attempts: config.get_optional("max_attempts")?,
151 max_duration: config.get_optional("max_duration")?,
152 delay_strategy: config.get_optional_string("delay_strategy")?,
153 fixed_delay: config.get_optional("fixed_delay")?,
154 random_min_delay: config.get_optional("random_min_delay")?,
155 random_max_delay: config.get_optional("random_max_delay")?,
156 backoff_initial_delay: config.get_optional("backoff_initial_delay")?,
157 backoff_max_delay: config.get_optional("backoff_max_delay")?,
158 backoff_multiplier: config.get_optional("backoff_multiplier")?,
159 jitter_factor: config.get_optional("jitter_factor")?,
160 method_policy: config.get_optional_string("method_policy")?,
161 status_codes: config.get_optional_string_list("status_codes")?,
162 error_kinds: config.get_optional_string_list("error_kinds")?,
163 })
164 }
165
166 pub fn validate(&self) -> Result<(), HttpConfigError> {
171 if self.max_attempts == 0 {
172 return Err(HttpConfigError::invalid_value(
173 "max_attempts",
174 "Retry max_attempts must be greater than 0",
175 ));
176 }
177 if !(0.0..=1.0).contains(&self.jitter_factor) {
178 return Err(HttpConfigError::invalid_value(
179 "jitter_factor",
180 "Retry jitter_factor must be between 0.0 and 1.0",
181 ));
182 }
183 self.delay_strategy
184 .validate()
185 .map_err(|message| HttpConfigError::invalid_value("delay_strategy", message))?;
186 Ok(())
187 }
188
189 pub fn allows_method(&self, method: &http::Method) -> bool {
197 self.enabled && self.method_policy.allows_method(method)
198 }
199
200 pub fn should_retry(&self, request: &HttpRequest) -> bool {
209 self.max_attempts > 1 && self.allows_method(request.method())
210 }
211
212 pub fn resolve(&self, request: &HttpRequest) -> Self {
220 let mut options = self.clone();
221 options.enabled = request.retry_override().resolve_enabled(options.enabled);
222 options.method_policy = request.retry_override().resolve_method_policy(options.method_policy);
223 options
224 }
225
226 pub fn is_retryable_status(&self, status: StatusCode) -> bool {
234 is_retryable_status(status, self.retry_status_codes.as_deref())
235 }
236
237 pub fn is_retryable_error_kind(&self, kind: HttpErrorKind) -> bool {
246 is_retryable_error_kind(kind, self.retry_error_kinds.as_deref())
247 }
248
249 pub(crate) fn to_executor_options(&self) -> RetryOptions {
260 RetryOptions::new(
261 self.max_attempts,
262 None,
263 self.max_duration,
264 self.delay_strategy.clone(),
265 RetryJitter::factor(self.jitter_factor),
266 )
267 .expect("validated HTTP retry options should convert to retry executor options")
268 }
269}
270
271impl Default for HttpRetryOptions {
272 fn default() -> Self {
273 Self {
274 enabled: false,
275 max_attempts: DEFAULT_RETRY_MAX_ATTEMPTS,
276 max_duration: None,
277 delay_strategy: RetryDelay::Exponential {
278 initial: DEFAULT_RETRY_INITIAL_DELAY,
279 max: DEFAULT_RETRY_MAX_DELAY,
280 multiplier: DEFAULT_RETRY_MULTIPLIER,
281 },
282 jitter_factor: DEFAULT_RETRY_JITTER_FACTOR,
283 method_policy: HttpRetryMethodPolicy::default(),
284 retry_status_codes: None,
285 retry_error_kinds: None,
286 }
287 }
288}
289
290struct HttpRetryConfigInput {
291 enabled: Option<bool>,
292 max_attempts: Option<u32>,
293 max_duration: Option<Duration>,
294 delay_strategy: Option<String>,
295 fixed_delay: Option<Duration>,
296 random_min_delay: Option<Duration>,
297 random_max_delay: Option<Duration>,
298 backoff_initial_delay: Option<Duration>,
299 backoff_max_delay: Option<Duration>,
300 backoff_multiplier: Option<f64>,
301 jitter_factor: Option<f64>,
302 method_policy: Option<String>,
303 status_codes: Option<Vec<String>>,
304 error_kinds: Option<Vec<String>>,
305}
306
307fn parse_retry_delay_strategy(value: &str, raw: &HttpRetryConfigInput) -> Result<RetryDelay, HttpConfigError> {
308 let normalized = value.trim().to_ascii_lowercase().replace('-', "_");
309 match normalized.as_str() {
310 "none" => Ok(RetryDelay::None),
311 "fixed" => Ok(RetryDelay::Fixed(
312 raw.fixed_delay.unwrap_or(DEFAULT_RETRY_INITIAL_DELAY),
313 )),
314 "random" => Ok(RetryDelay::Random {
315 min: raw.random_min_delay.unwrap_or(DEFAULT_RETRY_INITIAL_DELAY),
316 max: raw.random_max_delay.unwrap_or(DEFAULT_RETRY_MAX_DELAY),
317 }),
318 "exponential_backoff" | "exponential" => Ok(RetryDelay::Exponential {
319 initial: raw.backoff_initial_delay.unwrap_or(DEFAULT_RETRY_INITIAL_DELAY),
320 max: raw.backoff_max_delay.unwrap_or(DEFAULT_RETRY_MAX_DELAY),
321 multiplier: raw.backoff_multiplier.unwrap_or(DEFAULT_RETRY_MULTIPLIER),
322 }),
323 _ => Err(HttpConfigError::invalid_value(
324 "delay_strategy",
325 format!("Unsupported retry delay strategy: {value}"),
326 )),
327 }
328}
329
330fn parse_retry_status_codes(values: &[String]) -> Result<Vec<StatusCode>, HttpConfigError> {
342 let mut result = Vec::<StatusCode>::new();
343 for value in values {
344 let trimmed = value.trim();
345 if trimmed.is_empty() {
346 return Err(HttpConfigError::invalid_value(
347 "status_codes",
348 "Retry status_codes cannot contain blank values",
349 ));
350 }
351 let raw_code = trimmed.parse::<u16>().map_err(|error| {
352 HttpConfigError::invalid_value(
353 "status_codes",
354 format!("Invalid retry status code '{trimmed}': {error}"),
355 )
356 })?;
357 if !(100..=599).contains(&raw_code) {
358 return Err(HttpConfigError::invalid_value(
359 "status_codes",
360 format!("Retry status code must be in range 100..=599, got {raw_code}"),
361 ));
362 }
363 let status = StatusCode::from_u16(raw_code).expect("retry status code range is pre-validated to 100..=599");
364 if !result.contains(&status) {
365 result.push(status);
366 }
367 }
368 result.sort_by_key(|status| status.as_u16());
369 Ok(result)
370}
371
372fn parse_retry_error_kinds(values: &[String]) -> Result<Vec<HttpErrorKind>, HttpConfigError> {
383 let mut result = Vec::<HttpErrorKind>::new();
384 for value in values {
385 let trimmed = value.trim();
386 if trimmed.is_empty() {
387 return Err(HttpConfigError::invalid_value(
388 "error_kinds",
389 "Retry error_kinds cannot contain blank values",
390 ));
391 }
392 let normalized = trimmed.replace('-', "_");
393 let kind = HttpErrorKind::from_str(&normalized).map_err(|_| {
394 HttpConfigError::invalid_value("error_kinds", format!("Unsupported retry error kind: {trimmed}"))
395 })?;
396 if !result.contains(&kind) {
397 result.push(kind);
398 }
399 }
400 Ok(result)
401}
402
403fn default_retryable_status(status: StatusCode) -> bool {
412 status == StatusCode::TOO_MANY_REQUESTS || status.is_server_error()
413}
414
415fn default_retryable_error_kind(kind: HttpErrorKind) -> bool {
424 matches!(
425 kind,
426 HttpErrorKind::ConnectTimeout
427 | HttpErrorKind::ReadTimeout
428 | HttpErrorKind::WriteTimeout
429 | HttpErrorKind::RequestTimeout
430 | HttpErrorKind::Transport
431 )
432}