abstract expoential backoff to math utils.
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
5e72d36800
commit
52a561ff9e
5 changed files with 60 additions and 53 deletions
|
@ -24,3 +24,21 @@ macro_rules! validated {
|
|||
macro_rules! validated {
|
||||
($($input:tt)*) => { $crate::checked!($($input)*) }
|
||||
}
|
||||
|
||||
/// Returns false if the exponential backoff has expired based on the inputs
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn continue_exponential_backoff_secs(min: u64, max: u64, elapsed: Duration, tries: u32) -> bool {
|
||||
let min = Duration::from_secs(min);
|
||||
let max = Duration::from_secs(max);
|
||||
continue_exponential_backoff(min, max, elapsed, tries)
|
||||
}
|
||||
|
||||
/// Returns false if the exponential backoff has expired based on the inputs
|
||||
#[inline]
|
||||
#[must_use]
|
||||
pub fn continue_exponential_backoff(min: Duration, max: Duration, elapsed: Duration, tries: u32) -> bool {
|
||||
let min = min.saturating_mul(tries).saturating_mul(tries);
|
||||
let min = cmp::min(min, max);
|
||||
elapsed < min
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue