A timer based on a multi-time wheel structure

Overview

wheel-timer2

A timer based on a multi-time wheel structure

docs.rs

This library uses a multi-layered time wheel structure.

When a task is added to the wheel, it will go to the wheel with the coarsest granularity first, then to the wheel with a higher granularity, until the specified time is reached.

If the task is too long to fit on all the wheels, the task will be run by adding round, if there are too many too long tasks, it will cause a lot of tasks to accumulate in the coarsest granularity wheel, so the number of layers, capacity and granularity of the wheel should be adjusted according to actual needs to avoid this kind of thing.

To be precise, this is a structure for managing and running timed tasks, not a complete timer, because the user himself needs to push (execute) it regularly.

Usage

  1. First, we create a MultiWheel:
let mut wheel = MultiWheel::new(3, 10, Duration::from_millis(100));

this means creating a 3-layer round with 10 slots in each layer. Each slot differs by 100ms:

  1. Then, we add a task that prints hello world after a delay of 300ms:
let add_handle = wheel.add_handle();

add_handle
    .add(
        || {
            println!("hello");
        },
        Duration::from_millis(300),
    )
    .unwrap();

tips: AddHandle is a handle used to add tasks to the time wheel while the time wheel is running.

  1. Finally, we run the time wheel:
let mut i = tokio::time::interval(Duration::from_millis(100));
loop {
    i.tick().await;
    wheel.tick();
}

The tick interval must be consistent with the granularity specified when the time wheel was created. Otherwise, the task will be executed at the wrong time.

Examples

look examples

Precision

Under normal circumstances, the error is mainly the error of each running tick plus the time consumed by the tick.

I recommend using tokio::time::interval instead of simply using loop+sleep, because you can adjust MissedTickBehavior to try to compensate for the precision.

If the time of the task is less than the granularity of the time wheel, it will be scheduled to run after the next tick or one granularity time, depending on whether the task time is closer to the granularity time or 0.

You might also like...
 Northstar is a horizontally scalable and multi-tenant Kubernetes cluster provisioner and orchestrator
Northstar is a horizontally scalable and multi-tenant Kubernetes cluster provisioner and orchestrator

Northstar Northstar is a horizontally scalable and multi-tenant Kubernetes cluster provisioner and orchestrator. Explore the docs » View Demo · Report

Pegasus: A Multi-Node Command Runner
Pegasus: A Multi-Node Command Runner

Run a list of commands on a set of SSH nodes. With a bit of optional parametrization.

Rust library to scan files and expand multi-file crates source code as a single tree

syn-file-expand This library allows you to load full source code of multi-file crates into a single syn::File. Features: Based on syn crate. Handling

An embedded-hal driver for the TT21100 multi-touch touchscreen controller

tt21100 An embedded-hal driver for the TT21100 multi-touch touchscreen controller. If there is a feature which has not yet been implemented and which

Check Have I Been Pwned and see if it's time for you to change passwords.

checkpwn Check Have I Been Pwned and see if it's time for you to change passwords. Getting started Install: cargo install checkpwn Update: cargo inst

Estimate the amount of time spent working on a Git repository

jikyuu (時給) A tool to estimate the amount of time spent working on a Git repository. It is a direct port of git-hours, written in Node.js, because the

Lightweight compile-time UUID parser.

compiled-uuid Anywhere you're building Uuids from a string literal, you should use uuid. Motivation If you want to use a fixed Uuid throughout your pr

Measure the execution time of an application

Execution Timer Drag an executable file on the binary or enter the path as an argument to measure the execution time of the program. Building cargo bu

Simple and efficient time representation in Rust.

timens-rs Simple and efficient timestamp representation. The main objective being interoperability with OCaml Core_kernel.Time_ns. A significant part

Owner
orange soeur
上课很忙
orange soeur
Simple pomodoro timer with notifications

omo Simple pomodoro timer with notifications Installation

Guy Edwards 22 Dec 21, 2022
RTIC monotonic implementation using the RP2040's Timer peripheral

rp2040-monotonic RTIC monotonic implementation using the RP2040's Timer peripheral. Documentation License Licensed under either of Apache License, Ver

Emil Fresk 6 Nov 24, 2022
Speedrun timer with autosplitter for fxpak/sd2snes

About This is a simple barebones re-imagining of LiveSplit + autosplitter for SNES (sd2snes/fxpak + qusb2snes or SNI). It's named for the phylum of se

Jason Dagit 9 Nov 14, 2022
A safe sync/async multi-producer, multi-consumer channel

Loole A safe async/sync multi-producer multi-consumer channel. Producers can send and consumers can receive messages asynchronously or synchronously:

Mahdi Shojaee 50 Oct 6, 2023
Automatically build a Rust module tree from the project directory structure

Hypermod An even lazier version of automod and supermod. Searches the src/ directory recursively for .rs files, then builds a module tree using the di

null 4 Aug 3, 2022
A naive buffered/sync channel implementation in Rust, using the queue data structure

buffered-queue-rs Introduction This is my attempt at a simple and very naive buffered/synced queue implementation in Rust. The base thread-safe queue

Dhruv 4 Jul 22, 2023
mdTranslation is a utility to prepare multi-lingual Markdown documents.

mdTranslation is a utility to prepare multi-lingual Markdown documents. There's also a mdBook preprocessor called mdbook-translation for

Charles Lew 15 Dec 26, 2022
Incremental, multi-version remote backup tool for block devices.

bsync Incremental, multi-version remote backup tool for block devices. The on-disk backup format is a SQLite database and I've been dogfooding this on

Heyang Zhou 7 Aug 21, 2022
A fast, multi-threaded line counting utility written in Rust.

xloc A fast, multi-threaded line counting utility written in Rust. What is xloc A drop in replacement for bash's wc -l. Your project has x lines of co

null 1 Nov 15, 2021
A working example of multi targets compilation for Rust using Github Actions.

A working example of multi targets compilation for Rust using Github Actions. Supports Windows, MacOSX, x86_64, ARM and Raspberry PI Linux.

Nicolas Vanhoren 41 Dec 17, 2022