#watchdog #tokio #actor #signal #duration #expiration #bulletproof

simple-tokio-watchdog

Pretty simple but bulletproof watchdog actor

2 unstable releases

0.2.0 Aug 24, 2023
0.1.0 Aug 2, 2023

#882 in Asynchronous

Download history 3/week @ 2024-02-18 28/week @ 2024-02-25 1/week @ 2024-03-03 97/week @ 2024-03-10 2/week @ 2024-03-17 21/week @ 2024-03-31

120 downloads per month

Apache-2.0

8KB
100 lines

Watchdog

Pretty simple but bulletproof watchdog actor.

Send reset signals on the mpsc sender at a fast enough rate, or else the expiration oneshot channel will trigger.

use tokio::select;
use simple_tokio_watchdog::{Signal, Watchdog};
use std::time::Duration;

#[tokio::main]
async fn main() {
    let watchdog = Watchdog::with_timeout(Duration::from_millis(100));
    let (reset_tx, mut expired_rx) = watchdog.run();

    let mut duration = Duration::from_millis(4);
    loop {
        let sleep = tokio::time::sleep(duration);
        tokio::pin!(sleep);
        tokio::select! {
            _ = &mut expired_rx => {
                break;
            }
            () = sleep.as_mut() => {
                reset_tx.send(Signal::Reset).await.unwrap();
                duration *= 2;
                continue;
            }
        }
    }
    println!("{duration:?}");
}

Dependencies

~2.4–4MB
~70K SLoC