https://github.com/s2-streamstore/tokio-muxt
Timer for a limited set of events that multiplexes over a single tokio Sleep instance.
https://github.com/s2-streamstore/tokio-muxt
async deadline multiplexing rust sleep timer tokio
Last synced: 6 months ago
JSON representation
Timer for a limited set of events that multiplexes over a single tokio Sleep instance.
- Host: GitHub
- URL: https://github.com/s2-streamstore/tokio-muxt
- Owner: s2-streamstore
- License: apache-2.0
- Created: 2024-07-11T17:52:28.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-09T21:21:13.000Z (over 1 year ago)
- Last Synced: 2025-04-01T10:17:44.071Z (over 1 year ago)
- Topics: async, deadline, multiplexing, rust, sleep, timer, tokio
- Language: Rust
- Homepage:
- Size: 30.3 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tokio-muxt
A tiny timer that multiplexes a small set of event deadlines over a single `tokio::time::Sleep`.
`MuxTimer` is useful when you have a fixed, small set of event types and want one async timer that wakes for the next event, returning the event's ordinal and its deadline.
## Features
- One `tokio::time::Sleep` instance for many events.
- Coalesce repeated deadlines for the same event to the earliest or latest one.
- Stack-allocated storage with a const-generic capacity.
- Simple `Future` interface that yields `(ordinal, deadline)`.
## Example
```rust
use std::time::Duration;
use tokio::pin;
use tokio::time::Instant;
use tokio_muxt::{CoalesceMode, MuxTimer};
const EVENT_A: usize = 0;
const EVENT_B: usize = 1;
#[tokio::main]
async fn main() {
let timer: MuxTimer<2> = MuxTimer::default();
pin!(timer);
let start = Instant::now();
timer
.as_mut()
.fire_after(EVENT_A, Duration::from_millis(50), CoalesceMode::Earliest);
timer
.as_mut()
.fire_after(EVENT_B, Duration::from_millis(20), CoalesceMode::Earliest);
let (event, deadline) = timer.as_mut().await;
println!("first event = {event}, at +{:?}", deadline.duration_since(start));
}
```
## License
[MIT](./LICENSE)