#timer #wheel #algorithm #time

time_wheel

An implementation of timer uses time-wheel algorithm

3 releases

0.1.2 Aug 11, 2021
0.1.1 Aug 11, 2021
0.1.0 Aug 9, 2021

#25 in #wheel

MIT license

14KB
325 lines

timer

An implementation of timer uses time-wheel algorithm


1

use std::time::{Duration, SystemTime};
use time_wheel::FrontEnd;

fn main() {
    let mut fe = FrontEnd::new(Duration::from_millis(32), 60, 4);

    let time0 = SystemTime::now();
    let timer_id = fe.put_timer(Duration::from_millis(1500));
    println!("timer_id = {}", timer_id);
    let timer = fe.receiver.recv().unwrap();
    println!("Trigger {:?}", timer);
    println!("cost {:?}", time0.elapsed().unwrap());
}

output

timer_id = 1
Trigger Timer(1, 1628603696.593087s)
cost 1.507511s

2

use std::time::{Duration};
use time_wheel::{FrontEnd, unix_now_ms};
use std::thread::sleep;

fn main() {
    let mut fe = FrontEnd::new(Duration::from_millis(32), 60, 4);
    fe.after_func(Duration::from_secs(1), |t| {
        println!(
            " error={:?} function is executing", unix_now_ms() - t.when);
    });
    sleep(Duration::from_secs(3));
}

output

 error=27.973ms function is executing

3

use std::time::{Duration};
use time_wheel::{FrontEnd, unix_now_ms};

fn main() {
    let mut fe = FrontEnd::new(Duration::from_millis(32), 60, 4);
    let ticker = fe.ticker(Duration::from_millis(100));
    for i in 0..5 {
        let when = ticker.recv().unwrap();
        println!("on tick {} error={:?}", i, unix_now_ms() - when);
    }
}

output

on tick 0 error=32.028ms
on tick 1 error=24.275ms
on tick 2 error=23.229ms
on tick 3 error=20.323ms
on tick 4 error=16.779ms

Dependencies

~4–13MB
~129K SLoC