Expand description
Periodic task scheduler built on tokio::time::interval.
Timer-wheel maintenance is delegated to the tokio runtime:
callers register a periodic callback through task_register
and receive a TaskHandle that cancels the underlying tokio
task when dropped or explicitly cancelled.
A one-shot variant, task_schedule_once, fires a single
delayed callback. The runtime drives both APIs, so there is no
per-iteration “time to next task” / “execute expired tasks”
bookkeeping; tokio’s reactor performs that work transparently.
§Examples
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use std::time::Duration;
use dynomite::core::task::task_register;
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let counter = Arc::new(AtomicUsize::new(0));
let c = counter.clone();
let handle = task_register(Duration::from_millis(5), Arc::new(move || {
c.fetch_add(1, Ordering::Relaxed);
}));
tokio::time::sleep(Duration::from_millis(40)).await;
handle.cancel();
assert!(counter.load(Ordering::Relaxed) >= 1);
});Structs§
- Task
Handle - A handle that cancels a registered task.
Functions§
- task_
register - Register a periodic task that fires its callback every
period. - task_
schedule_ once - Register a one-shot task that fires once after
delayelapses.