use std::time::{Duration, Instant};
pub const RECHECK_SOON: Duration = Duration::from_millis(100);
#[derive(Debug, Clone, Copy)]
pub struct ScanTimer {
hard_deadline: Instant,
min_runtime: Instant,
last_activity: Instant,
}
impl ScanTimer {
pub fn new(max_total_duration: Duration, min_runtime_duration: Duration) -> Self {
let now = Instant::now();
Self {
hard_deadline: now + max_total_duration,
min_runtime: now + min_runtime_duration,
last_activity: now,
}
}
pub fn mark_activity(&mut self) {
self.last_activity = Instant::now();
}
pub fn time_until_next_tick(&self, max_silence: Duration) -> Duration {
let now = Instant::now();
let time_since_last = now.duration_since(self.last_activity);
max_silence
.checked_sub(time_since_last)
.unwrap_or(RECHECK_SOON)
}
pub fn has_expired(&self, max_silence: Duration) -> bool {
let now = Instant::now();
if now > self.hard_deadline {
return true;
}
let time_since_last = now.duration_since(self.last_activity);
now > self.min_runtime && time_since_last >= max_silence
}
pub fn hard_deadline_passed(&self) -> bool {
Instant::now() > self.hard_deadline
}
pub fn should_break_on_timeout(&self) -> bool {
Instant::now() >= self.min_runtime
}
}
#[must_use]
#[derive(Debug, Clone, Copy)]
pub struct ScanBudget {
base: Duration,
per_target: Duration,
ceiling: Duration,
}
impl ScanBudget {
pub const fn new(base: Duration, per_target: Duration, ceiling: Duration) -> Self {
Self {
base,
per_target,
ceiling,
}
}
pub fn with_base_at_least(self, minimum: Duration) -> Self {
Self {
base: self.base.max(minimum),
..self
}
}
pub fn with_per_target_at_least(self, minimum: Duration) -> Self {
Self {
per_target: self.per_target.max(minimum),
..self
}
}
pub fn covering(self, target_count: usize) -> Self {
Self {
ceiling: self.ceiling.max(self.unclamped(target_count)),
..self
}
}
pub fn for_target_count(&self, target_count: usize) -> Duration {
self.unclamped(target_count).min(self.ceiling)
}
fn unclamped(&self, target_count: usize) -> Duration {
let target_count = u32::try_from(target_count).unwrap_or(u32::MAX);
self.base
.saturating_add(self.per_target.saturating_mul(target_count))
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::thread::sleep;
#[test]
fn a_fresh_timer_has_neither_expired_nor_earned_the_right_to() {
let timer = ScanTimer::new(Duration::from_secs(10), Duration::from_secs(5));
assert!(!timer.has_expired(Duration::from_secs(1)));
assert!(!timer.should_break_on_timeout());
}
#[test]
fn the_next_check_moves_with_the_silence_and_resets_with_activity() {
let mut timer = ScanTimer::new(Duration::from_secs(10), Duration::from_secs(5));
let max_silence = Duration::from_millis(500);
let wait_time1 = timer.time_until_next_tick(max_silence);
sleep(Duration::from_millis(50));
let wait_time2 = timer.time_until_next_tick(max_silence);
assert!(wait_time2 < wait_time1);
timer.mark_activity();
let wait_time3 = timer.time_until_next_tick(max_silence);
assert!(
wait_time3 > wait_time2,
"activity restarts the silence clock"
);
}
#[test]
fn a_spent_tolerance_waits_a_short_fixed_time_rather_than_none() {
let timer = ScanTimer::new(Duration::from_secs(10), Duration::from_secs(5));
let max_silence = Duration::from_millis(10);
sleep(Duration::from_millis(15));
assert_eq!(timer.time_until_next_tick(max_silence), RECHECK_SOON);
}
#[test]
fn the_hard_deadline_fires_even_before_the_minimum_runtime() {
let timer = ScanTimer::new(
Duration::from_millis(10), Duration::from_millis(100), );
let max_silence = Duration::from_secs(1);
assert!(!timer.has_expired(max_silence));
sleep(Duration::from_millis(15));
assert!(timer.has_expired(max_silence));
}
#[test]
fn silence_ends_a_scan_once_the_minimum_runtime_is_behind_it() {
let timer = ScanTimer::new(
Duration::from_secs(10),
Duration::from_millis(10), );
let max_silence = Duration::from_millis(10);
assert!(!timer.has_expired(max_silence));
sleep(Duration::from_millis(25)); assert!(timer.has_expired(max_silence));
}
#[test]
fn a_socket_timeout_may_end_the_loop_only_after_the_minimum_runtime() {
let timer = ScanTimer::new(Duration::from_secs(10), Duration::from_millis(10));
assert!(!timer.should_break_on_timeout());
sleep(Duration::from_millis(15));
assert!(timer.should_break_on_timeout());
}
#[test]
fn budget_scales_linearly_with_target_count() {
let budget = ScanBudget::new(
Duration::from_millis(100),
Duration::from_millis(10),
Duration::from_secs(10),
);
assert_eq!(budget.for_target_count(0), Duration::from_millis(100));
assert_eq!(budget.for_target_count(10), Duration::from_millis(200));
}
#[test]
fn a_budget_can_be_widened_to_the_pace_a_scan_will_actually_keep() {
let budget = ScanBudget::new(
Duration::from_millis(100),
Duration::from_millis(1),
Duration::from_secs(60),
);
let paced = budget.with_per_target_at_least(Duration::from_millis(4));
assert_eq!(paced.for_target_count(100), Duration::from_millis(500));
assert_eq!(
budget
.with_per_target_at_least(Duration::from_micros(500))
.for_target_count(100),
budget.for_target_count(100),
"a slower pace than the budget already allows for changes nothing"
);
}
#[test]
fn a_ceiling_cannot_truncate_a_size_and_a_pace_it_was_told_about() {
let budget = ScanBudget::new(
Duration::from_millis(2_000),
Duration::from_nanos(1_562_500),
Duration::from_secs(60),
);
assert_eq!(
budget.for_target_count(65_535),
Duration::from_secs(60),
"the ceiling is what decides, and it is wrong"
);
let covering = budget.covering(65_535);
assert!(
covering.for_target_count(65_535) > Duration::from_secs(100),
"told the size, it allows what the pace implies"
);
}
#[test]
fn covering_only_ever_raises_the_ceiling() {
let budget = ScanBudget::new(
Duration::from_millis(100),
Duration::from_millis(1),
Duration::from_secs(60),
);
let small = budget.covering(10);
assert_eq!(
small.for_target_count(10),
Duration::from_millis(110),
"a scan well inside the ceiling is unaffected"
);
assert_eq!(
small.for_target_count(1_000_000),
Duration::from_secs(60),
"and a size it was never told about is still bounded"
);
}
#[test]
fn budget_is_clamped_to_its_ceiling() {
let budget = ScanBudget::new(
Duration::from_millis(100),
Duration::from_millis(10),
Duration::from_millis(500),
);
assert_eq!(budget.for_target_count(1000), Duration::from_millis(500));
}
}