Skip to main content

retry_with_policy

Function retry_with_policy 

Source
pub async fn retry_with_policy<F, Fut, T>(
    policy: &RetryPolicy,
    operation_name: &str,
    context: &str,
    operation: F,
) -> Result<T>
where F: FnMut() -> Fut, Fut: Future<Output = Result<T>>,
Expand description

What: Retry an operation with exponential backoff and jitter.

Inputs:

  • policy: Retry policy configuration
  • operation_name: Name of the operation for logging
  • context: Operation context (query/package name) for error messages
  • operation: Async closure that performs the operation and returns Result<T>

Output:

  • Result<T> from the operation, or the last error after all retries exhausted

Details:

  • Implements exponential backoff: delay = min(initial_delay * 2^attempt, max_delay)
  • Adds random jitter: final_delay = delay + random(0..=jitter_max)
  • Respects Retry-After header when available from response errors
  • Logs retry attempts with tracing
  • Returns immediately on success or non-retryable errors
  • Preserves operation context in error messages

ยงErrors

  • Returns context-specific errors (SearchFailed, InfoFailed, etc.) with preserved context
  • Returns Err(ArchToolkitError::Parse) for non-retryable errors