Skip to main content

with_retry

Function with_retry 

Source
pub async fn with_retry<T, E, F, Fut>(
    config: &RetryConfig,
    operation: F,
) -> Result<T, RetryError<E>>
where F: FnMut() -> Fut, Fut: Future<Output = Result<T, E>>, E: RetryableError,
Expand description

Execute an async operation with retries

Uses exponential backoff with optional jitter. If the error provides a retry-after duration, that will be used instead of the calculated delay.

§Arguments

  • config - Retry configuration
  • operation - Async closure that returns Result<T, E>

§Returns

  • Ok(T) - If the operation succeeds
  • Err(RetryError<E>) - If all retries are exhausted or error is not retryable

§Example

use yldfi_common::{with_retry, RetryConfig, RetryableError};

#[derive(Debug)]
struct ApiError { retryable: bool }

impl RetryableError for ApiError {
    fn is_retryable(&self) -> bool { self.retryable }
}

async fn call_api() -> Result<String, ApiError> {
    Ok("success".to_string())
}

async fn example() {
    let config = RetryConfig::default();
    let result = with_retry(&config, call_api).await;
}