hierarchical_hash_wheel_timer 1.4.0

A low-level timer implementation using a hierarchical four-level hash wheel with overflow.
Documentation
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! This module provides a manually-driven timer with the same queue-based
//! scheduling handle as the threaded timer.
//!
//! The timer does not own a worker thread. Instead, tests or simulations drive
//! time forward explicitly through [`ManualTimer::advance_by`] or
//! [`ManualTimer::advance_to_next`].

use super::*;
use crate::{
    queue_timer::{ClockRef, QueueTimerControl, QueueTimerCore, TimerMsg, TimerRef},
    wheels::Skip,
};
use crossbeam_channel as channel;
use std::{
    fmt,
    hash::Hash,
    sync::{
        atomic::{AtomicU64, Ordering},
        Arc,
        Mutex,
    },
    time::{Duration, Instant},
};

/// A timer implementation advanced explicitly by the caller.
///
/// This timer is useful for tests that need deterministic control over timeout
/// delivery without depending on wall-clock time or an extra scheduling thread.
pub struct ManualTimer<I, O, P>
where
    I: Hash + Clone + Eq + fmt::Debug,
    O: OneshotState<Id = I> + fmt::Debug,
    P: PeriodicState<Id = I> + fmt::Debug,
{
    inner: Arc<Mutex<ManualTimerInner<I, O, P>>>,
    work_queue: channel::Sender<TimerMsg<I, O, P>>,
    base: Instant,
    elapsed_millis: Arc<AtomicU64>,
}

impl<I, O, P> ManualTimer<I, O, P>
where
    I: Hash + Clone + Eq + fmt::Debug,
    O: OneshotState<Id = I> + fmt::Debug,
    P: PeriodicState<Id = I> + fmt::Debug,
{
    /// Create a new manual timer.
    pub fn new() -> Self {
        let (work_queue, receiver) = channel::unbounded();
        let elapsed_millis = Arc::new(AtomicU64::new(0));
        let inner = ManualTimerInner::new(receiver, Arc::clone(&elapsed_millis));
        Self {
            inner: Arc::new(Mutex::new(inner)),
            work_queue,
            base: Instant::now(),
            elapsed_millis,
        }
    }

    /// Returns a shareable scheduling handle for this timer.
    pub fn timer_ref(&self) -> TimerRef<I, O, P> {
        TimerRef::new(
            self.work_queue.clone(),
            ClockRef::manual(self.base, Arc::clone(&self.elapsed_millis)),
        )
    }

    /// Advances the timer by the supplied duration.
    ///
    /// The timer has millisecond resolution, matching the threaded timer.
    /// Sub-millisecond advances are accumulated and take effect once they sum
    /// to at least one millisecond.
    pub fn advance_by(&self, duration: Duration) {
        let mut inner = self.inner.lock().expect("manual timer mutex poisoned");
        inner.advance_by(duration);
    }

    /// Advances the timer just far enough to trigger the next due timeout.
    ///
    /// Returns `false` when there is currently no outstanding timeout to fire.
    pub fn advance_to_next(&self) -> bool {
        let mut inner = self.inner.lock().expect("manual timer mutex poisoned");
        inner.advance_to_next()
    }

    /// Returns the elapsed logical time since this manual timer was created.
    ///
    /// This is rounded down to the manual timer's millisecond resolution so it
    /// agrees with the deadlines the wheel can actually execute.
    pub fn elapsed(&self) -> Duration {
        Duration::from_millis(self.elapsed_millis.load(Ordering::Acquire))
    }

    /// Returns whether the timer has neither queued commands nor scheduled
    /// deadlines left to execute.
    pub fn is_idle(&self) -> bool {
        let mut inner = self.inner.lock().expect("manual timer mutex poisoned");
        inner.prepare();
        !inner.running || inner.core.is_idle()
    }

    /// Stops this shared manual timer so future advances no longer execute
    /// queued work, even through other clones of the same timer handle.
    pub fn stop(&self) {
        let mut inner = self.inner.lock().expect("manual timer mutex poisoned");
        inner.running = false;
    }
}

impl<I, O, P> Clone for ManualTimer<I, O, P>
where
    I: Hash + Clone + Eq + fmt::Debug,
    O: OneshotState<Id = I> + fmt::Debug,
    P: PeriodicState<Id = I> + fmt::Debug,
{
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
            work_queue: self.work_queue.clone(),
            base: self.base,
            elapsed_millis: Arc::clone(&self.elapsed_millis),
        }
    }
}

impl<I, O, P> Default for ManualTimer<I, O, P>
where
    I: Hash + Clone + Eq + fmt::Debug,
    O: OneshotState<Id = I> + fmt::Debug,
    P: PeriodicState<Id = I> + fmt::Debug,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<I, O, P> fmt::Debug for ManualTimer<I, O, P>
where
    I: Hash + Clone + Eq + fmt::Debug,
    O: OneshotState<Id = I> + fmt::Debug,
    P: PeriodicState<Id = I> + fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "<ManualTimer>")
    }
}

struct ManualTimerInner<I, O, P>
where
    I: Hash + Clone + Eq + fmt::Debug,
    O: OneshotState<Id = I> + fmt::Debug,
    P: PeriodicState<Id = I> + fmt::Debug,
{
    core: QueueTimerCore<I, O, P>,
    /// Shared logical clock visible to non-blocking readers through `TimerRef::now()`.
    elapsed_millis: Arc<AtomicU64>,
    /// Carries sub-millisecond advances until they add up to one wheel tick.
    sub_millis_remainder: Duration,
    /// Counts how many logical milliseconds have already been applied to the wheel.
    processed_millis: u64,
    running: bool,
}

// Safety: the queue timer core still uses `Rc`/`Weak` internally through the
// cancellable wheel implementation, but a `ManualTimerInner` is only ever
// accessed while holding the surrounding `Mutex`. The shared scheduling handle
// talks to the timer over a channel and never touches the wheel state
// directly, so moving the manually-driven timer between threads is sound as
// long as the user-provided timer state is itself `Send`.
unsafe impl<I, O, P> Send for ManualTimerInner<I, O, P>
where
    I: Hash + Clone + Eq + fmt::Debug + Send,
    O: OneshotState<Id = I> + fmt::Debug + Send,
    P: PeriodicState<Id = I> + fmt::Debug + Send,
{
}

impl<I, O, P> ManualTimerInner<I, O, P>
where
    I: Hash + Clone + Eq + fmt::Debug,
    O: OneshotState<Id = I> + fmt::Debug,
    P: PeriodicState<Id = I> + fmt::Debug,
{
    fn new(
        work_queue: channel::Receiver<TimerMsg<I, O, P>>,
        elapsed_millis: Arc<AtomicU64>,
    ) -> Self {
        Self {
            core: QueueTimerCore::new(work_queue),
            elapsed_millis,
            sub_millis_remainder: Duration::ZERO,
            processed_millis: 0,
            running: true,
        }
    }

    fn advance_by(&mut self, duration: Duration) {
        if !self.running {
            return;
        }

        self.prepare();
        let total_advanced = self
            .sub_millis_remainder
            .checked_add(duration)
            .expect("manual timer duration overflow");
        let advanced_millis =
            u64::try_from(total_advanced.as_millis()).expect("manual timer duration overflow");
        self.sub_millis_remainder = total_advanced
            .checked_sub(Duration::from_millis(advanced_millis))
            .expect("manual timer remainder underflow");
        let target_millis = if advanced_millis == 0 {
            self.current_millis()
        } else {
            self.advance_clock(advanced_millis)
        };
        let ticks_to_process = target_millis.saturating_sub(self.processed_millis);
        self.advance_ticks(ticks_to_process);
        self.prepare();
    }

    fn advance_to_next(&mut self) -> bool {
        if !self.running {
            return false;
        }

        self.prepare();
        loop {
            if !self.running {
                return false;
            }

            match self.core.can_skip() {
                Skip::Empty => return false,
                Skip::Millis(can_skip) if can_skip > 0 => {
                    self.skip_millis(u64::from(can_skip));
                    self.prepare();
                }
                Skip::Millis(_) | Skip::None => {
                    // Once the next entry sits in the primary wheel, `can_skip`
                    // can only promise that something may fire on the next tick,
                    // not which exact future millisecond will do it.
                    if self.advance_one_tick() > 0 {
                        return true;
                    }
                    self.prepare();
                }
            }
        }
    }

    fn prepare(&mut self) {
        if matches!(self.core.drain_messages(), QueueTimerControl::Stop) {
            self.running = false;
        }
    }

    fn current_millis(&self) -> u64 {
        self.elapsed_millis.load(Ordering::Acquire)
    }

    fn advance_clock(&self, advanced_millis: u64) -> u64 {
        let next_millis = self
            .current_millis()
            .checked_add(advanced_millis)
            .expect("manual timer duration overflow");
        self.elapsed_millis.store(next_millis, Ordering::Release);
        next_millis
    }

    fn advance_ticks(&mut self, mut remaining_ticks: u64) {
        while self.running && remaining_ticks > 0 {
            self.prepare();
            if !self.running {
                return;
            }

            match self.core.can_skip() {
                Skip::Empty => {
                    self.processed_millis += remaining_ticks;
                    return;
                }
                Skip::Millis(can_skip) if can_skip > 0 => {
                    let skipped_ticks = remaining_ticks.min(u64::from(can_skip));
                    self.core
                        .skip(u32::try_from(skipped_ticks).expect("skip count fits in u32"));
                    self.processed_millis += skipped_ticks;
                    remaining_ticks -= skipped_ticks;
                }
                Skip::Millis(_) | Skip::None => {
                    self.core.tick();
                    self.processed_millis += 1;
                    remaining_ticks -= 1;
                }
            }
        }
    }

    fn skip_millis(&mut self, skip_millis: u64) {
        if skip_millis == 0 {
            return;
        }

        self.advance_clock(skip_millis);
        self.core
            .skip(u32::try_from(skip_millis).expect("skip count fits in u32"));
        self.processed_millis += skip_millis;
    }

    fn advance_one_tick(&mut self) -> usize {
        debug_assert!(
            self.sub_millis_remainder < Duration::from_millis(1),
            "sub-millisecond remainder must stay below one millisecond"
        );
        self.sub_millis_remainder = Duration::ZERO;
        self.advance_clock(1);
        self.processed_millis += 1;
        self.core.tick()
    }
}

#[cfg(feature = "uuid-extras")]
impl ManualTimer<uuid::Uuid, OneShotClosureState<uuid::Uuid>, PeriodicClosureState<uuid::Uuid>> {
    /// Shorthand for creating a manual timer using `Uuid` identifiers and
    /// closure state.
    pub fn for_uuid_closures() -> Self {
        Self::new()
    }
}

#[cfg(feature = "uuid-extras")]
#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};
    use uuid::Uuid;

    #[test]
    fn oneshot_triggers_only_after_manual_advance() {
        let timer = ManualTimer::for_uuid_closures();
        let mut timer_ref = timer.timer_ref();
        let fired = Arc::new(Mutex::new(false));
        let fired_clone = Arc::clone(&fired);

        timer_ref.schedule_action_once(Uuid::new_v4(), Duration::from_millis(5), move |_| {
            let mut guard = fired_clone.lock().unwrap();
            *guard = true;
        });

        timer.advance_by(Duration::from_millis(4));
        assert!(!*fired.lock().unwrap());

        timer.advance_by(Duration::from_millis(1));
        assert!(*fired.lock().unwrap());
    }

    #[test]
    fn cancel_prevents_future_trigger() {
        let timer = ManualTimer::for_uuid_closures();
        let mut timer_ref = timer.timer_ref();
        let fired = Arc::new(Mutex::new(false));
        let fired_clone = Arc::clone(&fired);
        let id = Uuid::new_v4();

        timer_ref.schedule_action_once(id, Duration::from_millis(5), move |_| {
            let mut guard = fired_clone.lock().unwrap();
            *guard = true;
        });
        timer_ref.cancel(&id);

        timer.advance_by(Duration::from_millis(5));
        assert!(!*fired.lock().unwrap());
    }

    #[test]
    fn advance_to_next_triggers_periodic_once_per_call() {
        let timer = ManualTimer::for_uuid_closures();
        let mut timer_ref = timer.timer_ref();
        let fired = Arc::new(Mutex::new(Vec::new()));
        let fired_clone = Arc::clone(&fired);

        timer_ref.schedule_action_periodic(
            Uuid::new_v4(),
            Duration::from_millis(5),
            Duration::from_millis(7),
            move |_| {
                fired_clone.lock().unwrap().push(());
                if fired_clone.lock().unwrap().len() < 3 {
                    TimerReturn::Reschedule(())
                } else {
                    TimerReturn::Cancel
                }
            },
        );

        assert!(timer.advance_to_next());
        assert_eq!(fired.lock().unwrap().len(), 1);
        assert!(timer.advance_to_next());
        assert_eq!(fired.lock().unwrap().len(), 2);
        assert!(timer.advance_to_next());
        assert_eq!(fired.lock().unwrap().len(), 3);
        assert!(!timer.advance_to_next());
    }

    #[test]
    fn timer_ref_now_tracks_manual_advance_at_millisecond_resolution() {
        let timer = ManualTimer::for_uuid_closures();
        let timer_ref = timer.timer_ref();
        let base = timer_ref.now();

        timer.advance_by(Duration::from_micros(999));
        assert_eq!(timer.elapsed(), Duration::ZERO);
        assert_eq!(timer_ref.now(), base);

        timer.advance_by(Duration::from_micros(1));
        assert_eq!(timer.elapsed(), Duration::from_millis(1));
        assert_eq!(
            timer_ref.now().duration_since(base),
            Duration::from_millis(1)
        );
    }

    #[test]
    fn timer_ref_now_inside_handler_is_not_earlier_than_due_time() {
        let timer = ManualTimer::for_uuid_closures();
        let mut timer_ref = timer.timer_ref();
        let scheduled_from = timer_ref.now();
        let delay = Duration::from_millis(5);
        let handler_timer_ref = timer_ref.clone();
        let observed_now = Arc::new(Mutex::new(None));
        let observed_now_clone = Arc::clone(&observed_now);

        timer_ref.schedule_action_once(Uuid::new_v4(), delay, move |_| {
            let now = handler_timer_ref.now();
            *observed_now_clone.lock().unwrap() = Some(now);
        });

        timer.advance_by(Duration::from_millis(20));

        let observed_now = observed_now
            .lock()
            .unwrap()
            .expect("oneshot callback should record observed time");
        assert!(
            observed_now.duration_since(scheduled_from) >= delay,
            "handler observed {observed_now:?}, which is earlier than scheduled lower bound {:?}",
            scheduled_from + delay
        );
    }
}