/// Retry a future until it succeeds or retries run out.
Retry a future until it succeeds or retries run out.
Retry a future until it succeeds or retries run out .
async fn retry_with_backoff<T, E, F>(
    mut backoff: std::time::Duration,
    mut remaining_retries: u8,
    f: impl Fn() -> F,
) -> Result<T, E>
where
    F: futures::Future<Output = Result<T, E>>,
    E: std::fmt::Debug,
{
    while remaining_retries > 1 {
        match f().await {
            Ok(r) => return Ok(r),
            Err(e) => {
                log::warn!("Operation failed: {:?} -- retrying in {:?}", e, backoff);
                std::thread::sleep(backoff);
                backoff *= 2;
                remaining_retries -= 1;
            }
        }
    }
    f().await
}
async fn retry_with_backoff < T , E , F > ( mut backoff : std :: time :: Duration , mut remaining_retries : u8 , f : impl Fn ( ) -> F , ) -> Result < T , E > where F : futures :: Future < Output = Result < T , E > > , E : std :: fmt :: Debug , { while remaining_retries > 1 { match f ( ) . await { Ok ( r ) => return Ok ( r ) , Err ( e ) => { log :: warn ! ( " " e backoff ) ; std :: thread :: sleep ( backoff ) ; backoff *= 2 ; remaining_retries -= 1 ; } } } f ( ) . await }
Retry a future until it succeeds or retries run out.
Retry a future until it succeeds or retries run out .
backoff
std::time::Duration
remaining_retries
u8
f
impl Fn() -> F