Expand description
tokio-osinterval provides OsInterval, an alternative to
tokio::time::Interval that drives its periodic ticks from the
operating system’s native async-capable timer facility instead of
tokio’s userspace timer wheel.
Backends:
| Platform | Backend |
|---|---|
| Linux / Android | timerfd_create(CLOCK_MONOTONIC, …) |
| macOS / iOS / *BSD | kqueue + EVFILT_TIMER |
| Windows | CreateThreadpoolTimer |
Other / --no-default-features | tokio::time::sleep_until fallback |
The public API mirrors tokio::time::Interval closely so swapping
is mostly an import change.
§Example
use std::time::Duration;
use tokio_osinterval::interval;
let mut ticker = interval(Duration::from_millis(100));
for _ in 0..5 {
ticker.tick().await;
// do periodic work
}§Differences from tokio::time::Interval
- Each
OsIntervalowns one kernel timer object (an fd orPTP_TIMER); creating it requires an active tokio runtime. - The native backends bypass
tokio::time::pause()— for tests that need a paused clock, build withdefault-features = false. - Sub-millisecond periods are honored on platforms whose timers
support them (kqueue with
NOTE_NSECONDS, timerfd, Windows high-resolution timers).
§Cargo features
interval(default) — enablesOsIntervaland theinterval/interval_atconstructors.os-native(default) — selects the platform-native backend forOsInterval. Disable to force the portabletokio::time::sleep_untilfallback. Has no effect unlessintervalis also enabled.periodic— enablesPeriodicInterval, a stripped-down ticker driven by a single kernel-side periodic timer (Linux/BSD only).
interval and periodic are independent: enable either, both, or
(with default-features = false) neither.
Structs§
- OsInterval
interval - Periodic timer driven by the operating system’s native async timer.
- Periodic
Interval periodic - A periodic ticker driven entirely by an OS timer.
Enums§
- Missed
Tick Behavior interval - Defines the behavior of an
OsIntervalwhen it misses a tick.
Functions§
- interval
interval - Creates an interval that yields its first tick immediately and then once
every
periodthereafter. - interval_
at interval - Creates an interval that yields its first tick at
startand then once everyperiodthereafter.
Type Aliases§
- Expirations
periodic - Number of times a
PeriodicIntervalhas expired since the previous successful tick.