Introduce Timeout and deprecate Deadline. (#558)

This patch introduces `Timeout`. This new type allows setting a timeout
both using a duration and an instant. Given this overlap with
`Deadline`, `Deadline` is deprecated.

In addition to supporting future timeouts, the `Timeout` combinator is
able to provide timeout functionality to streams. It does this by
applying a duration based timeout to each item being yielded.

The main reason for introducing `Timeout` is that a deadline approach
does not work with streams. Since `Timeout` needed to be introduced
anyway, keeping `Deadline` around does not make sense.
This commit is contained in:
Carl Lerche
2018-08-22 20:39:46 -07:00
committed by GitHub
parent cf184eb326
commit 8bf2e9aeb0
9 changed files with 521 additions and 44 deletions
+4 -2
View File
@@ -11,7 +11,7 @@ use std::ptr;
use std::sync::{Arc, Weak};
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::{SeqCst, Relaxed};
use std::time::Instant;
use std::time::{Instant, Duration};
use std::u64;
/// Internal state shared between a `Delay` instance and the timer.
@@ -95,6 +95,7 @@ pub(crate) struct Entry {
#[derive(Debug)]
pub(crate) struct Time {
pub(crate) deadline: Instant,
pub(crate) duration: Duration,
}
/// Flag indicating a timer entry has elapsed
@@ -106,10 +107,11 @@ const ERROR: u64 = u64::MAX;
// ===== impl Entry =====
impl Entry {
pub fn new(deadline: Instant) -> Entry {
pub fn new(deadline: Instant, duration: Duration) -> Entry {
Entry {
time: CachePadded::new(UnsafeCell::new(Time {
deadline,
duration,
})),
inner: None,
task: AtomicTask::new(),
+10 -3
View File
@@ -1,10 +1,11 @@
use Error;
use clock::now;
use timer::{HandlePriv, Entry};
use futures::Poll;
use std::sync::Arc;
use std::time::Instant;
use std::time::{Instant, Duration};
/// Registration with a timer.
///
@@ -16,11 +17,11 @@ pub(crate) struct Registration {
}
impl Registration {
pub fn new(deadline: Instant) -> Registration {
pub fn new(deadline: Instant, duration: Duration) -> Registration {
fn is_send<T: Send + Sync>() {}
is_send::<Registration>();
Registration { entry: Arc::new(Entry::new(deadline)) }
Registration { entry: Arc::new(Entry::new(deadline, duration)) }
}
pub fn deadline(&self) -> Instant {
@@ -42,6 +43,12 @@ impl Registration {
Entry::reset(&mut self.entry);
}
pub fn reset_timeout(&mut self) {
let deadline = now() + self.entry.time_ref().duration;
self.entry.time_mut().deadline = deadline;
Entry::reset(&mut self.entry);
}
pub fn is_elapsed(&self) -> bool {
self.entry.is_elapsed()
}