move exponential backoff util to different submod
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
2259e2c82f
commit
7e4453620e
7 changed files with 39 additions and 33 deletions
|
@ -1,4 +1,4 @@
|
|||
use std::{cmp, convert::TryFrom, time::Duration};
|
||||
use std::{cmp, convert::TryFrom};
|
||||
|
||||
pub use checked_ops::checked_ops;
|
||||
|
||||
|
@ -53,34 +53,6 @@ macro_rules! validated {
|
|||
($($input:tt)+) => { $crate::expected!($($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
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[allow(clippy::as_conversions)]
|
||||
pub fn usize_from_f64(val: f64) -> Result<usize, Error> {
|
||||
|
|
|
@ -37,7 +37,10 @@ pub use self::{
|
|||
stream::{IterStream, ReadyExt, Tools as StreamTools, TryReadyExt},
|
||||
string::{str_from_bytes, string_from_bytes},
|
||||
sys::compute::parallelism as available_parallelism,
|
||||
time::{now_millis as millis_since_unix_epoch, timepoint_ago, timepoint_from_now},
|
||||
time::{
|
||||
exponential_backoff::{continue_exponential_backoff, continue_exponential_backoff_secs},
|
||||
now_millis as millis_since_unix_epoch, timepoint_ago, timepoint_from_now,
|
||||
},
|
||||
};
|
||||
|
||||
#[inline]
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
pub mod exponential_backoff;
|
||||
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::{err, Result};
|
||||
|
|
29
src/core/utils/time/exponential_backoff.rs
Normal file
29
src/core/utils/time/exponential_backoff.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
use std::{cmp, time::Duration};
|
||||
|
||||
/// 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
|
||||
}
|
|
@ -6,7 +6,7 @@ use std::{
|
|||
|
||||
use conduwuit::{
|
||||
debug, debug_error, debug_warn, implement, pdu, trace,
|
||||
utils::math::continue_exponential_backoff_secs, warn, PduEvent,
|
||||
utils::continue_exponential_backoff_secs, warn, PduEvent,
|
||||
};
|
||||
use futures::TryFutureExt;
|
||||
use ruma::{
|
||||
|
|
|
@ -5,7 +5,7 @@ use std::{
|
|||
};
|
||||
|
||||
use conduwuit::{
|
||||
debug, implement, utils::math::continue_exponential_backoff_secs, Err, PduEvent, Result,
|
||||
debug, implement, utils::continue_exponential_backoff_secs, Err, PduEvent, Result,
|
||||
};
|
||||
use ruma::{CanonicalJsonValue, EventId, OwnedEventId, RoomId, ServerName};
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ use conduwuit::{
|
|||
debug, err, error,
|
||||
result::LogErr,
|
||||
trace,
|
||||
utils::{calculate_hash, math::continue_exponential_backoff_secs, ReadyExt},
|
||||
utils::{calculate_hash, continue_exponential_backoff_secs, ReadyExt},
|
||||
warn, Error, Result,
|
||||
};
|
||||
use futures::{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue