#[cfg(feature = "async_retry")]
use std::time::Duration;
#[cfg(feature = "async_retry")]
use tokio::time::sleep;
#[cfg(feature = "async_retry")]
pub async fn async_retry<T, E, Fut, F>(times: usize, delay: Duration, mut op: F) -> Result<T, E>
where
F: FnMut() -> Fut,
Fut: std::future::Future<Output = Result<T, E>>,
{
let mut attempts = 0;
let mut current_delay = delay;
loop {
match op().await {
Ok(res) => return Ok(res),
Err(e) if attempts + 1 >= times => return Err(e),
Err(_) => {
attempts += 1;
sleep(current_delay).await;
current_delay *= 2; }
}
}
}