Struct reactor::TimeoutManager

source ·
pub struct TimeoutManager<K = ()> { /* private fields */ }
Expand description

Manages timers and triggers timeouts.

Implementations§

Create a new timeout manager.

Takes a threshold below which two timeouts cannot overlap.

Return the number of timeouts being tracked.

Check whether there are timeouts being tracked.

Register a new timeout with an associated key and wake-up time from a UNIX time epoch.

use std::time::Duration;

use reactor::TimeoutManager;

let mut tm = TimeoutManager::new(Duration::from_secs(1));

// Sets timeout from Instant::now();
let registered = tm.register(0xA, Duration::from_secs(8));
assert!(registered);

let registered = tm.register(0xB, Duration::from_secs(9));
assert!(registered);
assert_eq!(tm.len(), 2);

let registered = tm.register(0xC, Duration::from_millis(9541));
assert!(!registered);

let registered = tm.register(0xC, Duration::from_millis(9999));
assert!(!registered);
assert_eq!(tm.len(), 2);

Get the minimum time duration we should wait for at least one timeout to be reached. Returns None if there are no timeouts.

use reactor::TimeoutManager;

let mut tm = TimeoutManager::new(Duration::from_secs(0));

tm.register(0xA, Duration::from_millis(16));
tm.register(0xB, Duration::from_millis(8));
tm.register(0xC, Duration::from_millis(64));

let mut now = Duration::from_millis(0);
// We need to wait 8 millis to trigger the next timeout (1).
assert!(tm.next(now) <= Some(Duration::from_millis(8)));

// ... sleep for a millisecond ...
now += Duration::from_millis(1);

// Now we don't need to wait as long!
assert!(tm.next(now).unwrap() <= Duration::from_millis(7));

Given a specific time, add to the input vector keys that have timed out by that time. Returns the number of keys that timed out.

Given the current time, add to the input vector keys that have timed out. Returns the number of keys that timed out.

use std::time::Duration;

use reactor::TimeoutManager;

let mut tm = TimeoutManager::new(Duration::from_secs(0));

tm.register(0xA, Duration::from_millis(8));
tm.register(0xB, Duration::from_millis(16));
tm.register(0xC, Duration::from_millis(64));
tm.register(0xD, Duration::from_millis(72));

let mut timeouts = Vec::new();

assert_eq!(tm.check(Duration::from_millis(21), &mut timeouts), 2);
assert_eq!(timeouts, vec![0xA, 0xB]);
assert_eq!(tm.len(), 2);

Trait Implementations§

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.