use std::time::Duration;
pub(crate) const UNMEASURED_PATH_WAIT: Duration = Duration::from_secs(3);
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct PathAllowance {
allowance: Duration,
round_trip: Duration,
}
impl PathAllowance {
pub(crate) const NONE: Self = Self {
allowance: Duration::ZERO,
round_trip: Duration::ZERO,
};
#[cfg(test)]
pub(crate) fn of_round_trip(round_trip: Duration) -> Self {
Self::of_round_trips([round_trip])
}
pub(crate) fn of_round_trips(round_trips: impl IntoIterator<Item = Duration>) -> Self {
let mut samples = round_trips.into_iter();
let Some(first) = samples.next() else {
return Self::NONE;
};
let (mut smoothed, mut variation, mut alone) = (first, first / 2, true);
for sample in samples {
variation = (variation * 3 + smoothed.abs_diff(sample)) / 4;
smoothed = (smoothed * 7 + sample) / 8;
alone = false;
}
let floor = smoothed.saturating_add(smoothed / 4);
let mut allowance = smoothed.saturating_add((variation * 4).max(smoothed / 4));
if alone {
allowance = allowance.min(UNMEASURED_PATH_WAIT.max(floor));
}
Self {
allowance,
round_trip: smoothed,
}
}
pub(crate) fn over(self, wait: Duration) -> Duration {
wait.saturating_add(self.allowance)
}
pub(crate) fn over_each(self, wait: Duration, waits: u32) -> Duration {
wait.saturating_add(self.allowance.saturating_mul(waits))
}
pub(crate) fn service_time(self, elapsed: Duration) -> Duration {
elapsed.saturating_sub(self.round_trip)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_wait_on_a_measured_path_outlasts_its_round_trip() {
let wait = Duration::from_millis(500);
let path = Duration::from_millis(1_900);
let stretched = PathAllowance::of_round_trip(path).over(wait);
assert!(stretched > wait + path, "{stretched:?}");
assert_eq!(PathAllowance::of_round_trips([]).over(wait), wait);
assert_eq!(
PathAllowance::of_round_trip(path).over_each(wait, 2),
wait + (stretched - wait) * 2
);
}
#[test]
fn a_steady_path_earns_less_headroom_than_one_sample_or_a_wandering_path() {
let path = Duration::from_millis(1_900);
let one = PathAllowance::of_round_trip(path).over(Duration::ZERO);
let steady = PathAllowance::of_round_trips([path; 10]).over(Duration::ZERO);
let wandering = PathAllowance::of_round_trips(
[
900, 2_900, 1_000, 2_800, 1_100, 2_700, 900, 2_900, 1_000, 2_800,
]
.map(Duration::from_millis),
)
.over(Duration::ZERO);
assert_eq!(one, UNMEASURED_PATH_WAIT, "held to the path-finding wait");
assert_eq!(steady, path + path / 4, "the floor, once the path agrees");
assert!(wandering > path * 2, "{wandering:?}");
}
#[test]
fn a_lone_slow_sample_earns_no_more_than_the_path_finding_wait() {
let alone = |millis| {
PathAllowance::of_round_trip(Duration::from_millis(millis)).over(Duration::ZERO)
};
let outlier = Duration::from_millis(2_790);
assert_eq!(alone(2_790), outlier + outlier / 4, "the floor over it");
assert_eq!(alone(1_900), UNMEASURED_PATH_WAIT);
assert_eq!(alone(140), Duration::from_millis(420), "three round trips");
assert_eq!(alone(4_000), Duration::from_millis(5_000), "the floor");
let two = PathAllowance::of_round_trips([1_900, 1_900].map(Duration::from_millis))
.over(Duration::ZERO);
assert!(two > UNMEASURED_PATH_WAIT, "{two:?}");
}
#[test]
fn a_reply_spends_the_services_time_only_beyond_the_paths_round_trip() {
let path = Duration::from_millis(1_900);
let prompt = path + Duration::from_millis(30);
let steady = PathAllowance::of_round_trips([path; 10]);
assert_eq!(steady.service_time(prompt), Duration::from_millis(30));
assert_eq!(PathAllowance::NONE.service_time(prompt), prompt);
}
}