use std::future::Future;
use std::time::Duration;
use tokio::time::sleep;
pub trait RetryableError: std::fmt::Display {
fn is_retryable(&self) -> bool;
fn retry_after(&self) -> Option<Duration> {
None
}
}
#[derive(Debug, Clone)]
pub struct RetryConfig {
pub max_attempts: u32,
pub initial_delay: Duration,
pub max_delay: Duration,
pub backoff_multiplier: f64,
pub jitter: f64,
}
impl Default for RetryConfig {
fn default() -> Self {
Self {
max_attempts: 4,
initial_delay: Duration::from_secs(1),
max_delay: Duration::from_secs(30),
backoff_multiplier: 2.0,
jitter: 0.1,
}
}
}
impl RetryConfig {
pub fn database() -> Self {
Self {
max_attempts: 5,
initial_delay: Duration::from_millis(50),
max_delay: Duration::from_secs(5),
backoff_multiplier: 2.0,
jitter: 0.0, }
}
pub fn database_aggressive() -> Self {
Self {
max_attempts: 10,
initial_delay: Duration::from_millis(100),
max_delay: Duration::from_secs(10),
backoff_multiplier: 1.5,
jitter: 0.0,
}
}
pub fn api() -> Self {
Self::default()
}
pub fn api_aggressive() -> Self {
Self {
max_attempts: 5,
initial_delay: Duration::from_secs(1),
max_delay: Duration::from_secs(60),
backoff_multiplier: 2.0,
jitter: 0.2,
}
}
pub fn no_retry() -> Self {
Self {
max_attempts: 0,
..Default::default()
}
}
pub fn qwen_cli_match() -> Self {
Self {
max_attempts: 4,
initial_delay: Duration::from_secs(3),
max_delay: Duration::from_secs(30),
backoff_multiplier: 2.0,
jitter: 0.2,
}
}
pub fn calculate_delay(&self, attempt: u32) -> Duration {
let base_delay_ms = self.initial_delay.as_millis() as f64;
let exponential = base_delay_ms * self.backoff_multiplier.powi(attempt as i32);
let capped = exponential.min(self.max_delay.as_millis() as f64);
let final_delay = if self.jitter > 0.0 {
use rand::Rng;
let mut rng = rand::rng();
let jitter_factor = 1.0 + rng.random_range(-self.jitter..self.jitter);
(capped * jitter_factor).max(0.0)
} else {
capped
};
Duration::from_millis(final_delay as u64)
}
}
pub async fn retry<F, Fut, T, E>(
mut operation: F,
config: &RetryConfig,
) -> std::result::Result<T, E>
where
F: FnMut() -> Fut,
Fut: Future<Output = std::result::Result<T, E>>,
E: RetryableError,
{
let mut attempt = 0;
let mut last_error: Option<E> = None;
loop {
match operation().await {
Ok(result) => {
if attempt > 0 {
tracing::info!("Operation succeeded after {} retries", attempt);
}
return Ok(result);
}
Err(err) => {
if config.max_attempts == 0 || !err.is_retryable() {
tracing::debug!("Error is not retryable: {}", err);
return Err(err);
}
if attempt >= config.max_attempts {
tracing::warn!("Max retry attempts ({}) exceeded", config.max_attempts);
return Err(last_error.unwrap_or(err));
}
let delay = if let Some(retry_after) = err.retry_after() {
tracing::info!(
"Error provided retry_after hint: {}ms",
retry_after.as_millis()
);
retry_after
} else {
config.calculate_delay(attempt)
};
tracing::info!(
"Retry attempt {}/{} after {}ms for error: {}",
attempt + 1,
config.max_attempts,
delay.as_millis(),
err
);
last_error = Some(err);
sleep(delay).await;
attempt += 1;
}
}
}
}
pub async fn retry_with_notify<F, Fut, T, E, N>(
mut operation: F,
config: &RetryConfig,
mut on_retry: N,
) -> std::result::Result<T, E>
where
F: FnMut() -> Fut,
Fut: Future<Output = std::result::Result<T, E>>,
E: RetryableError,
N: FnMut(u32, u32, &E),
{
let mut attempt = 0;
let mut last_error: Option<E> = None;
loop {
match operation().await {
Ok(result) => {
if attempt > 0 {
tracing::info!("Operation succeeded after {} retries", attempt);
}
return Ok(result);
}
Err(err) => {
if config.max_attempts == 0 || !err.is_retryable() {
tracing::debug!("Error is not retryable: {}", err);
return Err(err);
}
if attempt >= config.max_attempts {
tracing::warn!("Max retry attempts ({}) exceeded", config.max_attempts);
return Err(last_error.unwrap_or(err));
}
let delay = err
.retry_after()
.unwrap_or_else(|| config.calculate_delay(attempt));
tracing::info!(
"Retry attempt {}/{} after {}ms for error: {}",
attempt + 1,
config.max_attempts,
delay.as_millis(),
err
);
on_retry(attempt + 1, config.max_attempts, &err);
last_error = Some(err);
sleep(delay).await;
attempt += 1;
}
}
}
}
pub async fn retry_with_check<F, Fut, T, E, C>(
mut operation: F,
config: &RetryConfig,
is_retryable: C,
) -> std::result::Result<T, E>
where
F: FnMut() -> Fut,
Fut: Future<Output = std::result::Result<T, E>>,
E: std::fmt::Display,
C: Fn(&E) -> bool,
{
let mut attempt = 0;
let mut last_error: Option<E> = None;
loop {
match operation().await {
Ok(result) => {
if attempt > 0 {
tracing::info!("Operation succeeded after {} retries", attempt);
}
return Ok(result);
}
Err(err) => {
if config.max_attempts == 0 || !is_retryable(&err) {
tracing::debug!("Error is not retryable: {}", err);
return Err(err);
}
if attempt >= config.max_attempts {
tracing::warn!("Max retry attempts ({}) exceeded", config.max_attempts);
return Err(last_error.unwrap_or(err));
}
let delay = config.calculate_delay(attempt);
tracing::info!(
"Retry attempt {}/{} after {}ms for error: {}",
attempt + 1,
config.max_attempts,
delay.as_millis(),
err
);
last_error = Some(err);
sleep(delay).await;
attempt += 1;
}
}
}
}