Expand description
Hierarchical timer wheel backing ntex timers.
TimerHandle and CallbackHandle register timers with the wheel of
the current thread, the timer futures of ntex-util are built on top of
them.
The design follows the Linux kernel timer wheel (kernel/time/timer.c).
§Clock
The wheel counts time in units of 1 << units milliseconds. The unit
is 16 milliseconds by default, see
Builder::timer_resolution. Each
thread reads the resolution of its system when the first timer is armed
and keeps it until the wheel is stopped. The wheel clock elapsed is the
unit of the last processed expiry, and elapsed_time is the Instant
that corresponds to it. Timer deadlines are converted to units relative to
this pair.
§Levels
The wheel has LVL_DEPTH (8) levels of LVL_SIZE (64) buckets. Each level
is 8 times coarser than the previous one, a timer is placed on the first
level that can represent its delay. With the default 16 ms unit:
| Level | Granularity | Range |
|---|---|---|
| 0 | 16 ms | 0 .. ~1 s |
| 1 | 128 ms | ~1 s .. ~8 s |
| 2 | ~1 s | ~8 s .. ~64 s |
| 3 | ~8 s | ~64 s .. ~8.6 m |
| 4 | ~65 s | ~8.6 m .. ~69 m |
| 5 | ~8.7 m | ~69 m .. ~9.2 h |
| 6 | ~70 m | ~9.2 h .. ~3 d |
| 7 | ~9.3 h | ~3 d .. ~24.5 d |
A smaller unit scales the granularity and the range of every level by the same factor, with a 2 ms unit level 0 covers ~126 ms and the wheel ~3 d.
Longer delays are clamped to the capacity of the wheel. The expiry is
rounded up to the granularity of the level and the delay is measured from
Instant::now(), so a timer never fires early but may fire up to one
granularity late. Timers are not cascaded to finer
levels, which keeps insertion and removal O(1).
A bitmap per level tracks occupied buckets, the next expiry is found by scanning the bitmaps instead of the buckets.
§Native runtime
Each native Runtime owns a wheel. Runtime::poll
expires due buckets before running tasks and reports the time until the
next occupied bucket in PollResult::Pending, so the driver waits for
I/O and timers in a single call. No driver tasks are spawned. The cached
system_time() is refreshed when it is older than 150 milliseconds.
The resolution is read from the current system when the first timer is
armed, or set with RuntimeBuilder::timer_resolution.
Handles carry the id of their wheel. A handle used outside the runtime that created it, or after that runtime is dropped, is stale: it reports elapsed, and reset and drop do nothing. Dropping the runtime stops its wheel and wakes all pending timers.
§Drivers
With the tokio and compio runtimes the wheel is per thread and two
tasks are spawned lazily on the current thread:
TimerDriversleeps until the next occupied bucket expires and wakes the timers stored in it. The clock advances to the scheduled time of the bucket, not to the time the driver woke up, so a late wakeup does not delay later timers. Buckets that are overdue are processed at once. Wakers are invoked in place while the wheel is held; timer operations are not reentrant. Callback timers are queued separately and invoked after releasing the wheel.LowresTimerDriverinvalidates the cachedsystem_time()value every 150 milliseconds.
A native runtime expires its timers when it polls and needs no driver
task. Otherwise the driver tasks sleep with the Tokio timer on the tokio
runtime, so the Tokio runtime must have timers enabled, with the compio
timer on compio, and with futures-timer without either feature.
Dropping the timer driver, i.e. when the runtime stops, stops the wheel and marks all timers as elapsed. Dropping the lowres driver invalidates the cached system time.
A runtime does not always drop its pending tasks when it stops, the timer is also reset once the arbiter storage of the stopped system is cleared, so that the next runtime on the thread starts its own drivers. Drivers of an older generation exit without touching the state.
Registrations retain owned crate::LocalWaker handles, specializing
native owner-thread wakes without allocating wrappers or growing entries.
Standard wakers are still accepted at polling boundaries.
Structs§
- Callback
Handle - A reusable, local one-shot callback timer.
- Timer
Handle - Handle to a timer registered with ntex’s local timer wheel.
Functions§
- system_
time - Returns a cached approximation of the current system time.