Skip to main content

retry_async

Function retry_async 

Source
pub async fn retry_async<T, Fut>(
    policy: &RetryPolicy,
    cancel: Option<&CancellationToken>,
    should_retry: impl Fn(&Error) -> bool,
    op: impl FnMut() -> Fut,
) -> Result<T>
where Fut: Future<Output = Result<T>>,
Expand description

Run op, retrying its result while should_retry says so and policy has attempts left, sleeping the (jittered, exponential) backoff between tries. The op is re-invoked from scratch each attempt, so it must be idempotent for the errors should_retry selects (lock-contention failures are — the command never ran). Returns the first Ok, or the last Err.

When cancel is Some, the backoff between attempts is cancellation-aware: if the token fires before or during a wait, the wait stops immediately and the whole retry aborts with a structured Error::Cancelled (naming the just-failed attempt’s program). It does not sit out the rest of the delay, and — crucially — it launches no further attempt, so a cancel can never race a fresh op into flight (the attempt count stays deterministic). Pass None to keep the plain, uninterruptible backoff (behaviour unchanged from before this parameter existed).

The first attempt always runs; cancellation is only observed around the backoff. An op bound to the same token (a ManagedClient built with default_cancel_on) still surfaces its own Cancelled when the token was already fired as it ran — should_retry returns false for that terminal error, so the loop returns it without a backoff anyway.