1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
//! Allows a future or stream to execute for a maximum amount of time. //! //! See [`Timeout`] documentation for more details. //! //! [`Timeout`]: struct.Timeout.html use crate::clock::now; use crate::Delay; #[cfg(feature = "async-traits")] use futures_core::ready; use std::fmt; use std::future::Future; use std::pin::Pin; use std::task::{self, Poll}; use std::time::{Duration, Instant}; /// Allows a `Future` or `Stream` to execute for a limited amount of time. /// /// If the future or stream completes before the timeout has expired, then /// `Timeout` returns the completed value. Otherwise, `Timeout` returns an /// [`Error`]. /// /// # Futures and Streams /// /// The exact behavor depends on if the inner value is a `Future` or a `Stream`. /// In the case of a `Future`, `Timeout` will require the future to complete by /// a fixed deadline. In the case of a `Stream`, `Timeout` will allow each item /// to take the entire timeout before returning an error. /// /// In order to set an upper bound on the processing of the *entire* stream, /// then a timeout should be set on the future that processes the stream. For /// example: /// /// ```rust,no_run /// use tokio::prelude::*; /// use tokio::sync::mpsc; /// /// use std::thread; /// use std::time::Duration; /// /// # async fn dox() { /// let (mut tx, rx) = mpsc::unbounded_channel(); /// /// thread::spawn(move || { /// tx.try_send(()).unwrap(); /// thread::sleep(Duration::from_millis(10)); /// tx.try_send(()).unwrap(); /// }); /// /// let process = rx.for_each(|item| { /// // do something with `item` /// # drop(item); /// # tokio::future::ready(()) /// }); /// /// // Wrap the future with a `Timeout` set to expire in 10 milliseconds. /// process.timeout(Duration::from_millis(10)).await; /// # } /// ``` /// /// # Cancelation /// /// Cancelling a `Timeout` is done by dropping the value. No additional cleanup /// or other work is required. /// /// The original future or stream may be obtained by calling [`Timeout::into_inner`]. This /// consumes the `Timeout`. /// /// [`Error`]: struct.Error.html /// [`Timeout::into_inner`]: struct.Timeout.html#method.into_iter #[must_use = "futures do nothing unless you `.await` or poll them"] #[derive(Debug)] pub struct Timeout<T> { value: T, delay: Delay, } /// Error returned by `Timeout`. #[derive(Debug)] pub struct Elapsed(()); impl<T> Timeout<T> { /// Create a new `Timeout` that allows `value` to execute for a duration of /// at most `timeout`. /// /// The exact behavior depends on if `value` is a `Future` or a `Stream`. /// /// See [type] level documentation for more details. /// /// [type]: # /// /// # Examples /// /// Create a new `Timeout` set to expire in 10 milliseconds. /// /// ```rust /// use tokio::timer::Timeout; /// use tokio::sync::oneshot; /// /// use std::time::Duration; /// /// # async fn dox() { /// let (tx, rx) = oneshot::channel(); /// # tx.send(()).unwrap(); /// /// // Wrap the future with a `Timeout` set to expire in 10 milliseconds. /// Timeout::new(rx, Duration::from_millis(10)).await; /// } /// ``` pub fn new(value: T, timeout: Duration) -> Timeout<T> { let delay = Delay::new_timeout(now() + timeout, timeout); Timeout::new_with_delay(value, delay) } pub(crate) fn new_with_delay(value: T, delay: Delay) -> Timeout<T> { Timeout { value, delay } } /// Gets a reference to the underlying value in this timeout. pub fn get_ref(&self) -> &T { &self.value } /// Gets a mutable reference to the underlying value in this timeout. pub fn get_mut(&mut self) -> &mut T { &mut self.value } /// Consumes this timeout, returning the underlying value. pub fn into_inner(self) -> T { self.value } } impl<T: Future> Timeout<T> { /// Create a new `Timeout` that completes when `future` completes or when /// `deadline` is reached. /// /// This function differs from `new` in that: /// /// * It only accepts `Future` arguments. /// * It sets an explicit `Instant` at which the timeout expires. pub fn new_at(future: T, deadline: Instant) -> Timeout<T> { let delay = Delay::new(deadline); Timeout { value: future, delay, } } } impl<T> Future for Timeout<T> where T: Future, { type Output = Result<T::Output, Elapsed>; fn poll(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> { // First, try polling the future // Safety: we never move `self.value` unsafe { let p = self.as_mut().map_unchecked_mut(|me| &mut me.value); if let Poll::Ready(v) = p.poll(cx) { return Poll::Ready(Ok(v)); } } // Now check the timer // Safety: X_X! unsafe { match self.map_unchecked_mut(|me| &mut me.delay).poll(cx) { Poll::Ready(()) => Poll::Ready(Err(Elapsed(()))), Poll::Pending => Poll::Pending, } } } } #[cfg(feature = "async-traits")] impl<T> futures_core::Stream for Timeout<T> where T: futures_core::Stream, { type Item = Result<T::Item, Elapsed>; fn poll_next(mut self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Option<Self::Item>> { // Safety: T might be !Unpin, but we never move neither `value` // nor `delay`. // // ... X_X unsafe { // First, try polling the future let v = self .as_mut() .map_unchecked_mut(|me| &mut me.value) .poll_next(cx); if let Poll::Ready(v) = v { if v.is_some() { self.as_mut().get_unchecked_mut().delay.reset_timeout(); } return Poll::Ready(v.map(Ok)); } // Now check the timer ready!(self.as_mut().map_unchecked_mut(|me| &mut me.delay).poll(cx)); // if delay was ready, timeout elapsed! self.as_mut().get_unchecked_mut().delay.reset_timeout(); Poll::Ready(Some(Err(Elapsed(())))) } } } // ===== impl Elapsed ===== impl fmt::Display for Elapsed { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { "deadline has elapsed".fmt(fmt) } } impl std::error::Error for Elapsed {} impl From<Elapsed> for std::io::Error { fn from(_err: Elapsed) -> std::io::Error { std::io::ErrorKind::TimedOut.into() } }