use std::sync::atomic::{AtomicU32, Ordering};
use std::time::Duration;
use mlua::Table;
use reqwest::StatusCode;
const RETRY_BASE_DELAY: Duration = Duration::from_millis(100);
const RETRY_MAX_DELAY: Duration = Duration::from_secs(5);
#[derive(Debug, Clone, Copy)]
pub(super) struct Retry {
pub(super) attempts: u32,
pub(super) exponential: bool,
pub(super) idempotent: bool,
}
pub(super) fn is_retryable(status: StatusCode) -> bool {
matches!(status.as_u16(), 408 | 429 | 500 | 502 | 503 | 504)
}
pub(super) fn backoff(attempt: u32, exponential: bool) -> Duration {
let base = if exponential {
RETRY_BASE_DELAY.saturating_mul(1u32 << (attempt - 1).min(10))
} else {
RETRY_BASE_DELAY
}
.min(RETRY_MAX_DELAY);
static SEED: AtomicU32 = AtomicU32::new(0x9e37_79b9);
let mut x = SEED.fetch_add(0x9e37_79b9, Ordering::Relaxed) | 1;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
let half = base / 2;
half + half.mul_f64(f64::from(x % 1000) / 1000.0)
}
pub(super) fn parse_retry(table: &Table) -> mlua::Result<Retry> {
let attempts = table.get::<Option<u32>>("attempts")?.unwrap_or(3);
let exponential = match table.get::<Option<String>>("backoff")?.as_deref() {
None | Some("exponential") => true,
Some("constant") => false,
Some(other) => {
return Err(mlua::Error::RuntimeError(format!(
"unknown retry backoff `{other}`: expected \"exponential\" or \"constant\""
)));
}
};
Ok(Retry {
attempts,
exponential,
idempotent: table.get::<Option<bool>>("idempotent")?.unwrap_or(false),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn retry_options_parse_the_idempotent_flag() {
let lua = mlua::Lua::new();
let table: Table = lua.load("{ attempts = 2 }").eval().expect("table");
assert!(!parse_retry(&table).expect("retry").idempotent);
let table: Table = lua
.load("{ attempts = 2, idempotent = true }")
.eval()
.expect("table");
assert!(parse_retry(&table).expect("retry").idempotent);
}
#[test]
fn backoff_grows_and_stays_jittered_within_bounds() {
for attempt in 1..=4u32 {
let base = RETRY_BASE_DELAY * (1 << (attempt - 1));
for _ in 0..20 {
let delay = backoff(attempt, true);
assert!(
delay >= base / 2 && delay <= base,
"attempt {attempt}: {delay:?} outside [{:?}, {:?}]",
base / 2,
base
);
}
}
let delay = backoff(8, false);
assert!(delay <= RETRY_BASE_DELAY);
assert!(backoff(30, true) <= RETRY_MAX_DELAY);
}
}