Skip to main content

Crate tokio_osinterval

Crate tokio_osinterval 

Source
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:

PlatformBackend
Linux / Androidtimerfd_create(CLOCK_MONOTONIC, …)
macOS / iOS / *BSDkqueue + EVFILT_TIMER
WindowsCreateThreadpoolTimer
Other / --no-default-featurestokio::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 OsInterval owns one kernel timer object (an fd or PTP_TIMER); creating it requires an active tokio runtime.
  • The native backends bypass tokio::time::pause() — for tests that need a paused clock, build with default-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) — enables OsInterval and the interval / interval_at constructors.
  • os-native (default) — selects the platform-native backend for OsInterval. Disable to force the portable tokio::time::sleep_until fallback. Has no effect unless interval is also enabled.
  • periodic — enables PeriodicInterval, 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§

OsIntervalinterval
Periodic timer driven by the operating system’s native async timer.
PeriodicIntervalperiodic
A periodic ticker driven entirely by an OS timer.

Enums§

MissedTickBehaviorinterval
Defines the behavior of an OsInterval when it misses a tick.

Functions§

intervalinterval
Creates an interval that yields its first tick immediately and then once every period thereafter.
interval_atinterval
Creates an interval that yields its first tick at start and then once every period thereafter.

Type Aliases§

Expirationsperiodic
Number of times a PeriodicInterval has expired since the previous successful tick.