-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a `hyper::rt::Timer` trait, and it is used in connection builders to configure a custom timer source for timeouts. Co-authored-by: Robert Cunningham <[email protected]>
- Loading branch information
1 parent
2988baa
commit fae97ce
Showing
22 changed files
with
470 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
mod tokiort; | ||
pub use tokiort::TokioTimer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#![allow(dead_code)] | ||
//! Various runtimes for hyper | ||
use std::{ | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
use futures_util::Future; | ||
use hyper::rt::{Sleep, Timer}; | ||
|
||
/// An Executor that uses the tokio runtime. | ||
pub struct TokioExecutor; | ||
|
||
/// A Timer that uses the tokio runtime. | ||
#[derive(Clone, Debug)] | ||
pub struct TokioTimer; | ||
|
||
impl Timer for TokioTimer { | ||
fn sleep(&self, duration: Duration) -> Box<dyn Sleep + Unpin> { | ||
let s = tokio::time::sleep(duration); | ||
let hs = TokioSleep { inner: Box::pin(s) }; | ||
return Box::new(hs); | ||
} | ||
|
||
fn sleep_until(&self, deadline: Instant) -> Box<dyn Sleep + Unpin> { | ||
return Box::new(TokioSleep { | ||
inner: Box::pin(tokio::time::sleep_until(deadline.into())), | ||
}); | ||
} | ||
} | ||
|
||
struct TokioTimeout<T> { | ||
inner: Pin<Box<tokio::time::Timeout<T>>>, | ||
} | ||
|
||
impl<T> Future for TokioTimeout<T> | ||
where | ||
T: Future, | ||
{ | ||
type Output = Result<T::Output, tokio::time::error::Elapsed>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> { | ||
self.inner.as_mut().poll(context) | ||
} | ||
} | ||
|
||
// Use TokioSleep to get tokio::time::Sleep to implement Unpin. | ||
// see https://docs.rs/tokio/latest/tokio/time/struct.Sleep.html | ||
pub(crate) struct TokioSleep { | ||
pub(crate) inner: Pin<Box<tokio::time::Sleep>>, | ||
} | ||
|
||
impl Future for TokioSleep { | ||
type Output = (); | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
self.inner.as_mut().poll(cx) | ||
} | ||
} | ||
|
||
// Use HasSleep to get tokio::time::Sleep to implement Unpin. | ||
// see https://docs.rs/tokio/latest/tokio/time/struct.Sleep.html | ||
|
||
impl Sleep for TokioSleep {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use std::{fmt, sync::Arc}; | ||
#[cfg(all(feature = "server", feature = "runtime"))] | ||
use std::{ | ||
pin::Pin, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
#[cfg(all(feature = "server", feature = "runtime"))] | ||
use crate::rt::Sleep; | ||
use crate::rt::Timer; | ||
|
||
/// A user-provided timer to time background tasks. | ||
#[derive(Clone)] | ||
pub(crate) enum Time { | ||
Timer(Arc<dyn Timer + Send + Sync>), | ||
Empty, | ||
} | ||
|
||
impl fmt::Debug for Time { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("Time").finish() | ||
} | ||
} | ||
|
||
/* | ||
pub(crate) fn timeout<F>(tim: Tim, future: F, duration: Duration) -> HyperTimeout<F> { | ||
HyperTimeout { sleep: tim.sleep(duration), future: future } | ||
} | ||
pin_project_lite::pin_project! { | ||
pub(crate) struct HyperTimeout<F> { | ||
sleep: Box<dyn Sleep>, | ||
#[pin] | ||
future: F | ||
} | ||
} | ||
pub(crate) struct Timeout; | ||
impl<F> Future for HyperTimeout<F> where F: Future { | ||
type Output = Result<F::Output, Timeout>; | ||
fn poll(self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Self::Output>{ | ||
let mut this = self.project(); | ||
if let Poll::Ready(v) = this.future.poll(ctx) { | ||
return Poll::Ready(Ok(v)); | ||
} | ||
if let Poll::Ready(_) = Pin::new(&mut this.sleep).poll(ctx) { | ||
return Poll::Ready(Err(Timeout)); | ||
} | ||
return Poll::Pending; | ||
} | ||
} | ||
*/ | ||
|
||
#[cfg(all(feature = "server", feature = "runtime"))] | ||
impl Time { | ||
pub(crate) fn sleep(&self, duration: Duration) -> Box<dyn Sleep + Unpin> { | ||
match *self { | ||
Time::Empty => { | ||
panic!("You must supply a timer.") | ||
} | ||
Time::Timer(ref t) => t.sleep(duration), | ||
} | ||
} | ||
|
||
pub(crate) fn sleep_until(&self, deadline: Instant) -> Box<dyn Sleep + Unpin> { | ||
match *self { | ||
Time::Empty => { | ||
panic!("You must supply a timer.") | ||
} | ||
Time::Timer(ref t) => t.sleep_until(deadline), | ||
} | ||
} | ||
|
||
pub(crate) fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) { | ||
match *self { | ||
Time::Empty => { | ||
panic!("You must supply a timer.") | ||
} | ||
Time::Timer(ref t) => t.reset(sleep, new_deadline), | ||
} | ||
} | ||
} |
Oops, something went wrong.