You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
23 lines
545 B
23 lines
545 B
2 years ago
|
use std::{fmt::Debug, time::Duration};
|
||
|
use tokio::time::sleep;
|
||
|
|
||
|
pub async fn retry_with_interval<T, E: Debug>(
|
||
|
retries: usize,
|
||
|
interval: Duration,
|
||
|
f: impl Fn() -> Result<T, E>,
|
||
|
) -> T {
|
||
|
let mut err = String::new();
|
||
|
for i in 0..retries {
|
||
|
eprintln!("retrier: {} attempt...", i + 1);
|
||
|
let result = f();
|
||
|
match result {
|
||
|
Ok(r) => return r,
|
||
|
Err(e) => {
|
||
|
err = format!("{e:?}");
|
||
|
sleep(interval).await;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
panic!("{err}");
|
||
|
}
|