retry_with_backoff

Function retry_with_backoff 

Source
pub async fn retry_with_backoff<F, Fut, T>(
    operation: F,
    max_retries: u32,
    initial_delay: Duration,
) -> Result<T>
where F: FnMut() -> Fut, Fut: Future<Output = Result<T>>,
Expand description

Retry a function with exponential backoff

Executes an async operation with automatic retry on failure. Each retry doubles the delay (exponential backoff) to handle transient failures gracefully.

§Arguments

  • operation - Async function to retry (FnMut returning Future<Output = Result<T>>)
  • max_retries - Maximum number of retry attempts (0 = no retries, just initial attempt)
  • initial_delay - Delay before first retry (doubles each subsequent retry)

§Returns

Returns the successful result or the last error after all retries exhausted.

§Examples

let result = retry_with_backoff(
    || async { Ok("success") },
    3,  // Try up to 3 times
    Duration::from_secs(1)  // Start with 1s delay
).await?;