diff --git a/tokio/src/time/instant.rs b/tokio/src/time/instant.rs index faf62ee59e3..d3f46ab39b8 100644 --- a/tokio/src/time/instant.rs +++ b/tokio/src/time/instant.rs @@ -54,14 +54,6 @@ impl Instant { Instant { std } } - pub(crate) fn far_future() -> Instant { - // Roughly 30 years from now. - // API does not provide a way to obtain max `Instant` - // or convert specific date in the future to instant. - // 1000 years overflows on macOS, 100 years overflows on FreeBSD. - Self::now() + Duration::from_secs(86400 * 365 * 30) - } - /// Convert the value into a `std::time::Instant`. pub fn into_std(self) -> std::time::Instant { self.std diff --git a/tokio/src/time/interval.rs b/tokio/src/time/interval.rs index 02cecc6ec1a..d1515d12cb5 100644 --- a/tokio/src/time/interval.rs +++ b/tokio/src/time/interval.rs @@ -1,4 +1,4 @@ -use crate::time::{sleep_until, Duration, Instant, Sleep}; +use crate::time::{safe_delay, sleep_until, Duration, Instant, Sleep}; use crate::util::trace; use std::future::{poll_fn, Future}; @@ -139,7 +139,7 @@ fn internal_interval_at( Interval { delay, - period, + period: safe_delay(period), missed_tick_behavior: MissedTickBehavior::default(), #[cfg(all(tokio_unstable, feature = "tracing"))] resource_span, @@ -479,9 +479,7 @@ impl Interval { self.missed_tick_behavior .next_timeout(timeout, now, self.period) } else { - timeout - .checked_add(self.period) - .unwrap_or_else(Instant::far_future) + timeout + self.period }; // When we arrive here, the internal delay returned `Poll::Ready`. diff --git a/tokio/src/time/mod.rs b/tokio/src/time/mod.rs index 8627a7838aa..67f805a941d 100644 --- a/tokio/src/time/mod.rs +++ b/tokio/src/time/mod.rs @@ -84,6 +84,12 @@ //! [`interval`]: crate::time::interval() //! [`sleep`]: sleep() +fn safe_delay(duration: Duration) -> Duration { + // Roughly 30 years from now. + // 1000 years overflows on macOS, 100 years overflows on FreeBSD. + duration.min(Duration::from_secs(86400 * 365 * 30)) +} + mod clock; pub(crate) use self::clock::Clock; cfg_test_util! { diff --git a/tokio/src/time/sleep.rs b/tokio/src/time/sleep.rs index b2dcf5c59f0..42c4fa6688f 100644 --- a/tokio/src/time/sleep.rs +++ b/tokio/src/time/sleep.rs @@ -1,5 +1,5 @@ use crate::runtime::Timer; -use crate::time::{error::Error, Duration, Instant}; +use crate::time::{error::Error, safe_delay, Duration, Instant}; use crate::util::trace; use pin_project_lite::pin_project; @@ -121,12 +121,8 @@ pub fn sleep_until(deadline: Instant) -> Sleep { #[cfg_attr(docsrs, doc(alias = "wait"))] #[track_caller] pub fn sleep(duration: Duration) -> Sleep { - let location = trace::caller_location(); - - match Instant::now().checked_add(duration) { - Some(deadline) => Sleep::new_timeout(deadline, location), - None => Sleep::new_timeout(Instant::far_future(), location), - } + let deadline = Instant::now() + safe_delay(duration); + Sleep::new_timeout(deadline, trace::caller_location()) } pin_project! { @@ -303,10 +299,6 @@ impl Sleep { Sleep { inner, entry } } - pub(crate) fn far_future(location: Option<&'static Location<'static>>) -> Sleep { - Self::new_timeout(Instant::far_future(), location) - } - /// Returns the instant at which the future will complete. pub fn deadline(&self) -> Instant { self.entry.deadline() diff --git a/tokio/src/time/timeout.rs b/tokio/src/time/timeout.rs index fc2065c11f0..d8fb5f833da 100644 --- a/tokio/src/time/timeout.rs +++ b/tokio/src/time/timeout.rs @@ -6,7 +6,7 @@ use crate::{ task::coop, - time::{error::Elapsed, sleep_until, Duration, Instant, Sleep}, + time::{error::Elapsed, safe_delay, Duration, Instant, Sleep}, util::trace, }; @@ -87,13 +87,8 @@ pub fn timeout(duration: Duration, future: F) -> Timeout where F: IntoFuture, { - let location = trace::caller_location(); - - let deadline = Instant::now().checked_add(duration); - let delay = match deadline { - Some(deadline) => Sleep::new_timeout(deadline, location), - None => Sleep::far_future(location), - }; + let deadline = Instant::now() + safe_delay(duration); + let delay = Sleep::new_timeout(deadline, trace::caller_location()); Timeout::new_with_delay(future.into_future(), delay) } @@ -165,7 +160,7 @@ pub fn timeout_at(deadline: Instant, future: F) -> Timeout where F: IntoFuture, { - let delay = sleep_until(deadline); + let delay = Sleep::new_timeout(deadline, trace::caller_location()); Timeout::new_with_delay(future.into_future(), delay) }