mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-11-13 04:06:54 +00:00
chore: merge 1818 nex/perf/get-missing-events
This commit is contained in:
28 files changed
+1724
-729
No files matched your search
@@ -1,7 +1,10 @@
|
||||
pub mod exponential_backoff;
|
||||
pub mod jitter;
|
||||
|
||||
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
||||
|
||||
pub use jitter::jitter;
|
||||
|
||||
use crate::{Result, err};
|
||||
|
||||
#[inline]
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
use std::{ops::RangeInclusive, time::Duration};
|
||||
|
||||
/// Returns a `Duration` that is jittered by a random percentage of the base
|
||||
/// duration. The jitter is applied as a random percentage in the range of
|
||||
/// `-jitter_range` to `jitter_range`.
|
||||
///
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use conduwuit_core::utils::time::jitter;
|
||||
/// let sleep_duration = jitter(Duration::from_secs(1), -10..=10);
|
||||
/// // Adds a jitter of between -10% and 10% to the duration.
|
||||
/// ```
|
||||
#[must_use]
|
||||
pub fn jitter(base: Duration, jitter_range: RangeInclusive<f64>) -> Duration {
|
||||
base.mul_f64(1.0 + (rand::random_range(jitter_range)) / 100.0)
|
||||
}
|
||||
Reference in new issue
Block a user