Rust

timer

Module

Keep-alive timers: TimingWheel (one-second buckets drained by a heartbeat) and NoTimer (never reaps).

Contents

use utopia_mqtt::NoTimer;
Rust
pub struct NoTimer

A timer that never reaps: scheduling and clearing do nothing.

use utopia_mqtt::TimingWheel;
Rust
pub struct TimingWheel

Keep-alive deadlines in one-second buckets. Scheduling an id puts it in the bucket of the second its keep-alive (times the multiplier) runs out; a heartbeat every interval seconds drains the buckets that are due and calls the tick hook with each id in them. Rescheduling (every packet a client sends) moves the id to a later bucket, so only silent clients are reaped, at most interval seconds late.

The heartbeat runs on the tokio runtime the first id is scheduled from (feature tokio); without one, call drain yourself.

Rust
use std::sync::{Arc, Mutex};

use utopia_mqtt::{Timer, TimingWheel};

let now = php_std::datetime::time();
let wheel = TimingWheel::new(20, 1.5, Some(now));
let reaped = Arc::new(Mutex::new(Vec::new()));
let seen = reaped.clone();
wheel.on_tick(Arc::new(move |id| seen.lock().unwrap().push(id)));

wheel.schedule(7, 10); // due in 15 seconds
wheel.drain(now + 5);
assert!(reaped.lock().unwrap().is_empty());
wheel.drain(now + 30);
assert_eq!(*reaped.lock().unwrap(), [7]);
Also implements Default.