mutant_lib/utils/
retry.rs1use crate::error::Error;
2use std::future::Future;
3use std::time::Duration;
4use tokio_retry::{
5 strategy::{jitter, ExponentialBackoff},
6 RetryIf,
7};
8
9pub fn standard_retry_strategy() -> impl Iterator<Item = Duration> {
11 ExponentialBackoff::from_millis(100)
12 .max_delay(Duration::from_secs(5))
13 .map(jitter)
14 .take(5)
15}
16
17pub async fn retry_operation<F, Fut, T, E, R>(
30 _action_description: &str,
31 action: F,
32 retry_if: R,
33) -> Result<T, E>
34where
35 F: Fn() -> Fut,
36 Fut: Future<Output = Result<T, E>>,
37 E: std::fmt::Display + From<Error> + std::fmt::Debug, R: FnMut(&E) -> bool,
39{
40 let strategy = standard_retry_strategy();
41 let result = RetryIf::spawn(strategy, action, retry_if).await;
42
43 result
44}