Provide a timer implementation (#249)

This patch adds a new crate: tokio-timer. This crate provides an
efficient timer implemeentation designed for use in Tokio based
applications.

The timer users a hierarchical hashed timer wheel algorithm with six
levels, each having 64 slots. This allows the timer to have a resolution
of 1ms while maintaining O(1) complexity for insert, removal, and firing
of timeouts.

There already exists a tokio-timer crate. This is a complete rewrite
which solves the outstanding problems with the existing tokio-timer
library.

Closes #146.
This commit is contained in:
Carl Lerche
2018-03-28 22:26:47 -07:00
committed by GitHub
parent ad189826f4
commit 19500f7df8
24 changed files with 3398 additions and 0 deletions
+60
View File
@@ -0,0 +1,60 @@
use self::Kind::*;
use std::error;
use std::fmt;
/// Errors encountered by the timer implementation.
#[derive(Debug)]
pub struct Error(Kind);
#[derive(Debug)]
enum Kind {
Shutdown,
AtCapacity,
}
impl Error {
/// Create an error representing a shutdown timer.
pub fn shutdown() -> Error {
Error(Shutdown)
}
/// Returns `true` if the error was caused by the timer being shutdown.
pub fn is_shutdown(&self) -> bool {
match self.0 {
Kind::Shutdown => true,
_ => false,
}
}
/// Create an error representing a timer at capacity.
pub fn at_capacity() -> Error {
Error(AtCapacity)
}
/// Returns `true` if the error was caused by the timer being at capacity.
pub fn is_at_capacity(&self) -> bool {
match self.0 {
Kind::AtCapacity => true,
_ => false,
}
}
}
impl error::Error for Error {
fn description(&self) -> &str {
use self::Kind::*;
match self.0 {
Shutdown => "timer is shutdown",
AtCapacity => "timer is at capacity and cannot create a new entry",
}
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
use std::error::Error;
self.description().fmt(fmt)
}
}