debot_utils/
retry_loop.rs

1use tokio::time::sleep;
2use tokio::time::Duration;
3
4pub async fn retry_loop<F, Fut, T, E>(
5    mut operation: F,
6    retries: usize,
7    delay: Duration,
8    label: &str,
9) -> Result<T, E>
10where
11    F: FnMut() -> Fut,
12    Fut: std::future::Future<Output = Result<T, E>>,
13    E: std::fmt::Debug,
14{
15    for attempt in 0..=retries {
16        match operation().await {
17            Ok(result) => return Ok(result),
18            Err(e) => {
19                log::warn!(
20                    "Attempt {}/{} failed during {}: {:?}",
21                    attempt + 1,
22                    retries + 1,
23                    label,
24                    e
25                );
26                sleep(delay).await;
27            }
28        }
29    }
30
31    operation().await
32}