Files
tokio/src/reactor/timeout.rs
T

90 lines
3.1 KiB
Rust
Raw Normal View History

2016-09-02 11:07:52 -07:00
//! Support for creating futures that represent timeouts.
//!
//! This module contains the `Timeout` type which is a future that will resolve
//! at a particular point in the future.
2016-08-03 09:55:47 -07:00
use std::io;
use std::time::{Duration, Instant};
2016-09-01 16:42:48 -07:00
use futures::{Future, Poll, Async};
2016-08-03 09:55:47 -07:00
2016-09-07 16:11:19 -07:00
use reactor::{Remote, Handle};
2016-09-02 11:07:52 -07:00
use reactor::timeout_token::TimeoutToken;
2016-08-03 09:55:47 -07:00
/// A future representing the notification that a timeout has occurred.
///
2016-10-06 21:11:46 +03:00
/// Timeouts are created through the `Timeout::new` or
/// `Timeout::new_at` methods indicating when a timeout should fire at.
2016-08-03 09:55:47 -07:00
/// Note that timeouts are not intended for high resolution timers, but rather
/// they will likely fire some granularity after the exact instant that they're
/// otherwise indicated to fire at.
pub struct Timeout {
token: TimeoutToken,
2016-10-06 21:11:46 +03:00
when: Instant,
2016-09-07 16:11:19 -07:00
handle: Remote,
2016-08-03 09:55:47 -07:00
}
2016-09-02 11:07:52 -07:00
impl Timeout {
2016-08-03 09:55:47 -07:00
/// Creates a new timeout which will fire at `dur` time into the future.
///
/// This function will return a future that will resolve to the actual
/// timeout object. The timeout object itself is then a future which will be
/// set to fire at the specified point in the future.
2016-09-07 16:11:19 -07:00
pub fn new(dur: Duration, handle: &Handle) -> io::Result<Timeout> {
2016-09-02 11:07:52 -07:00
Timeout::new_at(Instant::now() + dur, handle)
2016-08-03 09:55:47 -07:00
}
/// Creates a new timeout which will fire at the time specified by `at`.
///
/// This function will return a future that will resolve to the actual
/// timeout object. The timeout object itself is then a future which will be
/// set to fire at the specified point in the future.
2016-09-07 16:11:19 -07:00
pub fn new_at(at: Instant, handle: &Handle) -> io::Result<Timeout> {
Ok(Timeout {
token: try!(TimeoutToken::new(at, &handle)),
2016-10-06 21:11:46 +03:00
when: at,
2016-09-07 16:11:19 -07:00
handle: handle.remote().clone(),
})
2016-08-03 09:55:47 -07:00
}
2017-06-27 11:34:56 +08:00
/// Resets this timeout to an new timeout which will fire at the time
/// specified by `at`.
2017-06-27 09:55:36 -07:00
///
/// This method is usable even of this instance of `Timeout` has "already
/// fired". That is, if this future has resovled, calling this method means
/// that the future will still re-resolve at the specified instant.
///
/// If `at` is in the past then this future will immediately be resolved
/// (when `poll` is called).
///
/// Note that if any task is currently blocked on this future then that task
/// will be dropped. It is required to call `poll` again after this method
/// has been called to ensure tha ta task is blocked on this future.
2017-06-27 11:34:56 +08:00
pub fn reset(&mut self, at: Instant) {
self.when = at;
self.token.reset_timeout(self.when, &self.handle);
}
2016-08-03 09:55:47 -07:00
}
impl Future for Timeout {
type Item = ();
type Error = io::Error;
2016-08-17 09:29:05 -07:00
fn poll(&mut self) -> Poll<(), io::Error> {
2016-08-03 09:55:47 -07:00
// TODO: is this fast enough?
2016-08-17 18:14:32 -07:00
let now = Instant::now();
2016-10-06 21:11:46 +03:00
if self.when <= now {
2016-09-01 16:42:48 -07:00
Ok(Async::Ready(()))
2016-08-03 09:55:47 -07:00
} else {
2016-09-02 11:07:52 -07:00
self.token.update_timeout(&self.handle);
2016-09-01 16:42:48 -07:00
Ok(Async::NotReady)
2016-08-03 09:55:47 -07:00
}
}
}
impl Drop for Timeout {
fn drop(&mut self) {
2016-09-02 11:07:52 -07:00
self.token.cancel_timeout(&self.handle);
2016-08-03 09:55:47 -07:00
}
}