timerwheel 0.1.0

Hierarchical timer wheel for delayed task scheduling with pluggable executors.
Documentation
// Copyright © 2026-present The Timerwheel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::sync::{
    Arc,
    atomic::{AtomicUsize, Ordering},
    mpsc,
};
use std::time::Duration;

use timerwheel::timer::MetricSink;
use timerwheel::{Timer, TimerMetrics};

struct CountingMetricSink {
    observations: Arc<AtomicUsize>,
}

impl MetricSink for CountingMetricSink {
    fn observe_metrics(&self, _metrics: TimerMetrics) {
        self.observations.fetch_add(1, Ordering::Relaxed);
    }
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let observations = Arc::new(AtomicUsize::new(0));
    let timer = Timer::builder()
        .metric_sink(CountingMetricSink {
            observations: Arc::clone(&observations),
        })
        .build()?;
    let (tx, rx) = mpsc::channel();

    timer.schedule(Duration::from_millis(10), move || {
        tx.send(()).expect("send succeeds");
    })?;
    rx.recv_timeout(Duration::from_secs(1))?;

    let metrics = timer.metrics();
    println!(
        "scheduled={} expired={} pending={} observations={}",
        metrics.scheduled_timeouts,
        metrics.expired_timeouts,
        metrics.pending_timeouts,
        observations.load(Ordering::Relaxed)
    );

    timer.shutdown()?;
    Ok(())
}