| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
use rand::Rng; |
| 34 |
use std::time::{Duration, SystemTime}; |
| 35 |
|
| 36 |
|
| 37 |
pub(crate) const EPOCH_DURATION: Duration = Duration::from_secs(3_600 * 24); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
pub fn smoothing_factor() -> Duration { |
| 42 |
rand::rng().random_range(Duration::from_secs(0)..EPOCH_DURATION) |
| 43 |
} |
| 44 |
|
| 45 |
pub fn current_epoch() -> u64 { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
let now = SystemTime::now() |
| 50 |
.duration_since(SystemTime::UNIX_EPOCH) |
| 51 |
.expect("expected system time to be after UNIX epoch"); |
| 52 |
now.as_secs() / (EPOCH_DURATION.as_secs()) |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
pub fn epoch_start(epoch: u64) -> SystemTime { |
| 57 |
SystemTime::UNIX_EPOCH + (EPOCH_DURATION * (epoch as u32)) |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
pub fn until_epoch_start(epoch: u64) -> Option<Duration> { |
| 64 |
epoch_start(epoch).duration_since(SystemTime::now()).ok() |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
pub(crate) fn until_fetch(epoch: u64, smoothing_factor: Duration) -> Option<Duration> { |
| 72 |
|
| 73 |
|
| 74 |
let fetch_time = { |
| 75 |
let fetch_epoch = epoch - 2; |
| 76 |
|
| 77 |
let fetch_epoch_start = epoch_start(fetch_epoch); |
| 78 |
|
| 79 |
fetch_epoch_start + smoothing_factor |
| 80 |
}; |
| 81 |
|
| 82 |
fetch_time.duration_since(SystemTime::now()).ok() |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
#[cfg(test)] |
| 96 |
mod tests { |
| 97 |
use super::*; |
| 98 |
#[test] |
| 99 |
fn current() { |
| 100 |
let current_epoch = current_epoch(); |
| 101 |
let start = epoch_start(current_epoch); |
| 102 |
assert!(SystemTime::now().duration_since(start).is_ok()); |
| 103 |
let future_start = epoch_start(current_epoch + 1); |
| 104 |
assert!(future_start.duration_since(SystemTime::now()).is_ok()); |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn until_start() { |
| 109 |
let current = current_epoch(); |
| 110 |
|
| 111 |
assert!(until_epoch_start(current).is_none()); |
| 112 |
assert!(until_epoch_start(current + 1).is_some()); |
| 113 |
} |
| 114 |
|
| 115 |
#[test] |
| 116 |
fn fetch() { |
| 117 |
const ZERO_DURATION: Duration = Duration::from_secs(0); |
| 118 |
|
| 119 |
let current_epoch = current_epoch(); |
| 120 |
assert!(until_fetch(current_epoch, ZERO_DURATION).is_none()); |
| 121 |
assert!(until_fetch(current_epoch + 1, ZERO_DURATION).is_none()); |
| 122 |
assert!(until_fetch(current_epoch + 2, ZERO_DURATION).is_none()); |
| 123 |
assert!(until_fetch(current_epoch + 2, EPOCH_DURATION).is_some()); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn smoothing_factor() { |
| 128 |
for _ in 0..10 { |
| 129 |
assert!(super::smoothing_factor() < EPOCH_DURATION); |
| 130 |
} |
| 131 |
} |
| 132 |
} |