diff options
author | Sunil Nimmagadda <sunil@nimmagadda.net> | 2022-11-28 08:49:32 +0530 |
---|---|---|
committer | Sunil Nimmagadda <sunil@nimmagadda.net> | 2022-11-28 08:49:32 +0530 |
commit | 07463c6301172a6f7af1272a25c2bb84ab4c48d2 (patch) | |
tree | ef884ea182db5c1eb0b3c989071f2fb2a51b03e5 /src |
Let's begin.
A skeletal async structure that provides two distinct timers and a
signal handler using tokio.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..59f4286 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,43 @@ +use tokio::signal::unix::{signal, SignalKind}; +use tokio::time; + +const ADV_INTERVAL: u64 = 3; +const SKEW_INTERVAL: u64 = 1; + +async fn advert_handler() { + println!("3 second timer fired"); +} + +async fn skew_handler() { + println!("1 second timer fired"); +} + +async fn sighup_handler() { + println!("got SIGHUP"); +} + +#[tokio::main(flavor = "current_thread")] +async fn main() { + let advert_handle = tokio::spawn(async move { + let mut advert_interval = time::interval(time::Duration::from_secs(ADV_INTERVAL)); + loop { + advert_interval.tick().await; + advert_handler().await; + } + }); + let skew_handle = tokio::spawn(async move { + let mut skew_interval = time::interval(time::Duration::from_secs(SKEW_INTERVAL)); + loop { + skew_interval.tick().await; + skew_handler().await; + } + }); + let sighup_handle = tokio::spawn(async move { + let mut stream = signal(SignalKind::hangup()).unwrap(); + loop { + stream.recv().await; + sighup_handler().await; + } + }); + let (_, _, _) = tokio::join!(advert_handle, skew_handle, sighup_handle); +} |