Files
tokio/src/reactor/timeout.rs
T

105 lines
3.8 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.
///
2017-09-22 11:01:34 -04:00
/// This function will return a Result with the actual timeout object or an
/// error. The timeout object itself is then a future which will be
2016-08-03 09:55:47 -07:00
/// 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`.
///
2017-09-22 11:01:34 -04:00
/// This function will return a Result with the actual timeout object or an
/// error. The timeout object itself is then a future which will be
2016-08-03 09:55:47 -07:00
/// 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
2017-09-14 16:19:45 -04:00
/// fired". That is, if this future has resolved, calling this method means
2017-06-27 09:55:36 -07:00
/// 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
2017-09-14 16:19:45 -04:00
/// has been called to ensure that a 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);
}
2017-09-12 22:54:13 -07:00
/// Polls this `Timeout` instance to see if it's elapsed, assuming the
/// current time is specified by `now`.
///
/// The `Future::poll` implementation for `Timeout` will call `Instant::now`
/// each time it's invoked, but in some contexts this can be a costly
/// operation. This method is provided to amortize the cost by avoiding
/// usage of `Instant::now`, assuming that it's been called elsewhere.
///
/// This function takes the assumed current time as the first parameter and
/// otherwise functions as this future's `poll` function. This will block a
/// task if one isn't already blocked or update a previous one if already
/// blocked.
pub fn poll_at(&mut self, now: Instant) -> Poll<(), io::Error> {
if self.when <= now {
Ok(Async::Ready(()))
} else {
self.token.update_timeout(&self.handle);
Ok(Async::NotReady)
}
}
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?
2017-09-12 22:54:13 -07:00
self.poll_at(Instant::now())
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
}
}