1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
use std::{thread, time};

#[doc(hidden)]
/// Executes the provided function a given number of times with the given interval between
/// the retries. This function swallows all results and only returns the last result.
pub fn with_retry<T, U>(
    retries: usize,
    interval: u64,
    f: impl Fn() -> Result<T, U>,
) -> Result<T, U> {
    let mut result = (f)();
    for _ in 1..=retries {
        if result.is_ok() {
            return result;
        }
        thread::sleep(time::Duration::from_millis(interval));
        result = (f)();
    }
    result
}