pub async fn run_with_retry<T, E, S, F, OnEvent, Synthesize>(
policy: &RetryPolicy,
state: &mut S,
on_event: OnEvent,
operation: F,
synthesize_exhausted_error: Synthesize,
) -> Result<T>where
F: for<'a> FnMut(&'a mut S) -> Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'a>>,
E: Into<VtCodeError>,
OnEvent: FnMut(&mut S, RetryEvent<'_>),
Synthesize: FnOnce(&RetryPolicy) -> VtCodeError,Expand description
Drive a retry loop per policy, invoking on_event for each
lifecycle event. Returns the first successful result, or the final
error if the policy gives up or all attempts are exhausted.
state is reborrowed mutably and passed to each callback on every
invocation, so callers can use it to thread a &mut self (or any
other mutable context) through the loop without resorting to
RefCell or split borrows. The operation callback must return a
boxed future so the helper can await it without tying its lifetime
to the closure’s own borrow of state.
The returned future is Send so it can be passed to tokio::spawn.
synthesize_exhausted_error is invoked only in the (degenerate)
case where the loop completes without ever recording a Backoff
error. The closure receives the &RetryPolicy so it can attach
site-specific context (e.g. policy.max_attempts) without having
to capture the policy in its environment — which is exactly the
pattern that conflicts with &mut state.