#timer #timeout #runtimes #wheel #future #hashed #base

pendulum

带多种运行时的哈希定时轮

5 个不稳定版本

使用旧的 Rust 2015

0.3.1 2017年11月29日
0.3.0 2017年11月13日
0.2.1 2017年11月11日
0.2.0 2017年11月10日
0.1.0 2017年11月10日

#4 in #hashed

Download history 10/week @ 2024-03-29 2/week @ 2024-04-05 53/week @ 2024-07-12

每月 53 次下载

MIT/Apache

67KB
972 行代码(不含注释)

Pendulum

Build Status Build status Documentation Crates.io

高效定时管理的数据结构和运行时

使用方法

Cargo.toml:

[dependencies]
pendulum = "0.3"

lib.rs/main.rs:

extern crate pendulum;

示例

使用 Timer 运行时的 futures 基础

extern crate pendulum;
extern crate futures;

use std::time::Duration;

use futures::Stream;
use futures::sync::mpsc;

use pendulum::HashedWheelBuilder;
use pendulum::future::{TimerBuilder, TimedOut};

#[derive(Debug, PartialEq, Eq)]
enum PeerMessage {
    KeepAlive,
    DoSomething
}

impl From<TimedOut> for PeerMessage {
    fn from(_: TimedOut) -> PeerMessage {
        PeerMessage::KeepAlive
    }
}

fn main() {
    // Create a timer with the default configuration
    let timer = TimerBuilder::default()
        .build(HashedWheelBuilder::default().build());

    // Assume some other part of the application sends messages to some peer
    let (send, recv) = mpsc::unbounded();

    // Application sent the peer a single message
    send.unbounded_send(PeerMessage::DoSomething)
        .unwrap();

    // Wrap the receiver portion (a `Stream`), in a `Heartbeat` stream
    let mut heartbeat = timer.heartbeat(Duration::from_millis(100), recv)
        .unwrap()
        .wait();

    // Should receive the applications message
    assert_eq!(PeerMessage::DoSomething, heartbeat.next().unwrap().unwrap());

    // Application only sent one message, timer will continuously send keep alives
    // if 100 ms goes by without the original receiver receiving any messages
    assert_eq!(PeerMessage::KeepAlive, heartbeat.next().unwrap().unwrap());
    assert_eq!(PeerMessage::KeepAlive, heartbeat.next().unwrap().unwrap());
}

使用 Pendulum 数据结构

extern crate pendulum;

use std::time::Duration;
use std::thread;

use pendulum::{Pendulum, HashedWheelBuilder};

#[derive(Debug, PartialEq, Eq)]
struct SomeData(usize);

fn main() {
    // Create a pendulum with mostly default configration
    let mut wheel = HashedWheelBuilder::default()
        // Tick duration defines the resolution for our timer (all timeouts will be a multiple of this)
        .with_tick_duration(Duration::from_millis(100))
        .build();

    // Insert a timeout and store the token, we can use this to cancel the timeout
    let token = wheel.insert_timeout(Duration::from_millis(50), SomeData(5)).unwrap();

    // Tick our wheel after the given duration (100 ms)
    thread::sleep(wheel.tick_duration());

    // Tell the wheel that it can perform a tick
    wheel.tick();

    // Retrieve any expired timeouts
    while let Some(timeout) = wheel.expired_timeout() {
        assert_eq!(SomeData(5), timeout);
    }
    
    // If we tried to remove the timeout using the token, we get None (already expired)
    assert_eq!(None, wheel.remove_timeout(token));
}

参考

许可证

根据您的选择,许可如下

贡献

除非您明确声明,否则根据 Apache-2.0 许可证定义的,您有意提交的任何贡献,都应如上所述双重许可,不附加任何额外条款或条件。

依赖项

~225KB