Function retry

Source
pub async fn retry<R, F, Fut, T, E>(
    operation: F,
    retry_policy: &R,
) -> Result<T, E>
where R: RetryPolicy<E = E>, F: FnMut() -> Fut, Fut: Future<Output = Result<T, E>>, E: Error,
Expand description

A function that performs and retries the given operation according to a retry policy.

Caution: A retry policy without the number of attempts capped by MaxAttemptsRetryPolicy decorator will result in infinite retries.

Example

let retry_policy = ExponentialBackoffRetryPolicy::new(Duration::from_millis(100))
	.with_max_attempts(5)
	.with_max_total_delay(Duration::from_secs(2))
	.with_max_jitter(Duration::from_millis(30))
	.skip_retry_on_error(|e| matches!(e, VssError::InvalidRequestError(..)));

let result = retry(operation, &retry_policy);

To use a retry policy as a member in a Send & Sync safe struct which needs to have known size at compile time, we can specify its concrete type as follows:


type VssRetryPolicy = FilteredRetryPolicy<ExponentialBackoffRetryPolicy<VssError>, Box<dyn 'static + Send + Sync + Fn(&VssError) -> bool>>;

struct SomeStruct {
	retry_policy: VssRetryPolicy,
}

impl SomeStruct {
	fn new() -> Self {
		let retry_policy = ExponentialBackoffRetryPolicy::new(Duration::from_millis(100))
			.skip_retry_on_error(Box::new(|e: &VssError| { matches!( e, VssError::NoSuchKeyError(..)) }) as _);
		Self { retry_policy }
	}
}