timerwheel
timerwheel provides delayed task scheduling with a hierarchical timer wheel.
The scheduler owns time advancement and dispatches expired tasks to an executor,
so long-running work does not block later timeout scheduling.
Install
[]
= "0.1"
Enable the optional Tokio adapter with:
[]
= { = "0.1", = ["tokio"] }
Quick Start
use mpsc;
use Duration;
use Timer;
Configuration
Timer::builder() configures the scheduler:
tick: timer resolution.bucket_count: buckets per timing-wheel level.command_capacity: bounded schedule-command capacity.max_pending: maximum accepted pending timeouts.backpressure_policy: reject or wait when the command path is full.expired_task_policy: reject or retry when executor dispatch is saturated.metric_sink: observe timer metric snapshots after metric changes.
executor::Pool::builder() configures the worker executor:
workers: fixed worker count.queue_capacity: bounded task queue capacity.reject_policy: reject or wait when the worker queue is full.metric_sink: observe executor metric snapshots after metric changes.
Prelude
Applications that use timer, executor, policy, and metric sink APIs together can import the prelude:
use *;
The prelude keeps timer and executor metric sinks distinct with aliases:
TimerMetricSink, NoopTimerMetricSink, ExecutorMetricSink, and
NoopExecutorMetricSink.
Executor Boundary
The scheduler advances time and dispatches expired tasks through
executor::Executor::try_execute. The default pool and Tokio executor keep
worker execution from blocking timer-wheel advancement. Custom executors must
return from try_execute immediately.
use ;
use ;
use ;
Cancellation
Timeout::cancel() is logical. A cancelled entry may stay in a bucket until the
scheduler reaches that bucket, but it will not execute.
Metrics
Timer::metrics() and executor::Pool::metrics() return immutable snapshots.
The counters cover accepted, expired, cancelled, rejected, panicked, pending,
and queue-depth states. Builders can also attach metric sinks for push-style
observation.
Shutdown
Timer::shutdown() stops accepting new schedules, wakes the scheduler, drains
accepted commands, and shuts down timer-owned executors. Calling shutdown more
than once is safe.
Optional Tokio Adapter
With the tokio feature:
use Duration;
async