Skip to main content

tuwunel_core/utils/time/
exponential_backoff.rs

1use std::{cmp, time::Duration};
2
3/// Returns false if the exponential backoff has expired based on the inputs
4#[inline]
5#[must_use]
6pub fn continue_exponential_backoff_secs(
7	min: u64,
8	max: u64,
9	elapsed: Duration,
10	tries: u32,
11) -> bool {
12	let min = Duration::from_secs(min);
13	let max = Duration::from_secs(max);
14	continue_exponential_backoff(min, max, elapsed, tries)
15}
16
17/// Returns false if the exponential backoff has expired based on the inputs
18#[inline]
19#[must_use]
20pub fn continue_exponential_backoff(
21	min: Duration,
22	max: Duration,
23	elapsed: Duration,
24	tries: u32,
25) -> bool {
26	let min = min.saturating_mul(tries).saturating_mul(tries);
27	let min = cmp::min(min, max);
28	elapsed < min
29}