#watchdog #tokio #timer #duration #pet #handle #dur

tokio-watchdog

为Tokio的看门狗定时器

1个不稳定版本

使用旧的Rust 2015

0.1.0 2018年4月5日

#9 in #pet

MIT/Apache

8KB
114

tokio-watchdog的早期实现,以供进一步讨论。

许可证 = Apache2.0/MIT,类似于Rust。

API摘录

pub struct Watchdog(H);
impl Watchdog {
    pub fn new(dur: Duration) -> Self;
    pub fn duration(&self) -> Duration;
    pub fn set_duration(&mut self, dur: Duration);
    pub fn handle(&self) -> Pet;
}

impl Future for Watchdog {
    type Item = Rearm;
    type Error = tokio_timer::Error;
}

pub struct Pet(H);
impl Pet {
    pub fn pet(&self);
    pub fn get_remaining_time(&self) -> Option<Duration>;
}

pub struct Rearm(H);
impl Rearm {
    pub fn rearm(self) -> Watchdog;
    pub fn rearm_with_duration(self, dur: Duration) -> Watchdog;
}

示例

extern crate tokio;
extern crate tokio_watchdog;

use tokio::runtime;
use tokio::prelude::{Future, Stream, future};
use tokio::timer::Interval;

use std::time::{Instant, Duration};

fn main() {
    
    let mut r = runtime::Builder::new().build().unwrap();
    
    let watch = tokio_watchdog::Watchdog::new(Duration::from_secs(1));
    
    let p = watch.handle();
    
    let task1 = watch.and_then(|_| {
        println!("BLAM!");
        ::std::process::exit(0);
        #[allow(unreachable_code)]
        future::ok(())
    });
    
    
    
    let intvl = Interval::new(Instant::now(), Duration::new(0, 100_000_000));
    let lazy_time = Instant::now() + Duration::from_secs(2);
    let task2 = intvl.for_each(move |i| {
        if i < lazy_time {
            println!("pet");
            p.pet();
        } else {
            println!("no pet");
        }
        future::ok(())
    });
    
    r.spawn(task1.map_err(|_|()));
    r.spawn(task2.map_err(|_|()));
    
    r.shutdown_on_idle().wait().unwrap();
}

依赖项

~620KB