use retry_if::{ExponentialBackoffConfig, retry};
use std::num::TryFromIntError;
use std::time::Duration;
use tokio::time::{Instant, pause};
const BACKOFF_CONFIG: ExponentialBackoffConfig = ExponentialBackoffConfig {
max_retries: 5,
t_wait: Duration::from_secs(1),
backoff: 2.0,
t_wait_max: None,
backoff_max: None,
};
#[tokio::main(flavor = "current_thread")]
async fn main() {
pause();
let start = Instant::now();
let _ = fallible_call().await;
let end = Instant::now();
let elapsed = end - start;
assert!(elapsed > Duration::from_secs(31));
assert!(elapsed < Duration::from_millis(31100));
println!("Total test time: {elapsed:?}")
}
fn retry_if(result: &Result<i64, TryFromIntError>) -> bool {
result.is_err()
}
#[retry(BACKOFF_CONFIG, retry_if)]
async fn fallible_call() -> Result<i64, TryFromIntError> {
i64::try_from(i128::MAX)
}