Skip to main content

secrets_provider_tests/
retry.rs

1use std::future::Future;
2use std::time::Duration;
3
4/// Parse a truthy env var in a tolerant way.
5pub fn parse_bool_env(var: &str) -> bool {
6    std::env::var(var)
7        .ok()
8        .map(|v| matches_ignore_ascii(&v, &["1", "true", "yes"]))
9        .unwrap_or(false)
10}
11
12fn matches_ignore_ascii(value: &str, expected: &[&str]) -> bool {
13    expected.iter().any(|pat| value.eq_ignore_ascii_case(pat))
14}
15
16/// Retry an async operation with fixed backoff.
17pub async fn retry_async<F, Fut, T, E>(
18    mut op: F,
19    max_attempts: usize,
20    base_delay: Duration,
21) -> Result<T, E>
22where
23    F: FnMut() -> Fut,
24    Fut: Future<Output = Result<T, E>>,
25{
26    let mut attempt = 0usize;
27    loop {
28        attempt += 1;
29        match op().await {
30            Ok(v) => return Ok(v),
31            Err(err) if attempt >= max_attempts => return Err(err),
32            Err(_) => {
33                tokio::time::sleep(base_delay * attempt as u32).await;
34            }
35        }
36    }
37}