Pendulum
Data structures and runtimes for efficient timer management.
Usage
Cargo.toml
:
[dependencies]
pendulum = "0.3"
lib.rs/main.rs
:
extern crate pendulum;
Examples
Usage of the futures base Timer
runtime:
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() {
let timer = TimerBuilder::default()
.build(HashedWheelBuilder::default().build());
let (send, recv) = mpsc::unbounded();
send.unbounded_send(PeerMessage::DoSomething)
.unwrap();
let mut heartbeat = timer.heartbeat(Duration::from_millis(100), recv)
.unwrap()
.wait();
assert_eq!(PeerMessage::DoSomething, heartbeat.next().unwrap().unwrap());
assert_eq!(PeerMessage::KeepAlive, heartbeat.next().unwrap().unwrap());
assert_eq!(PeerMessage::KeepAlive, heartbeat.next().unwrap().unwrap());
}
Usage of the Pendulum
data structure:
extern crate pendulum;
use std::time::Duration;
use std::thread;
use pendulum::{Pendulum, HashedWheelBuilder};
#[derive(Debug, PartialEq, Eq)]
struct SomeData(usize);
fn main() {
let mut wheel = HashedWheelBuilder::default()
.with_tick_duration(Duration::from_millis(100))
.build();
let token = wheel.insert_timeout(Duration::from_millis(50), SomeData(5)).unwrap();
thread::sleep(wheel.tick_duration());
wheel.tick();
while let Some(timeout) = wheel.expired_timeout() {
assert_eq!(SomeData(5), timeout);
}
assert_eq!(None, wheel.remove_timeout(token));
}
References
License
Licensed under either of
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any
additional terms or conditions.