-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore(volo-http): bump volo-http * fix(volo-grpc): add `timer` for hyper client and server Both server and client of `hyper` need to set a timer, otherwise they will **panic** when timeout occurs. But `hyper` does not provide an implementation of the trait `Timer`, and even if `hyper-util` does provide one, updates are not released in new versions. To solve this problem, I copied `TokioTimer` and `TokioSleep` from the latest repository of `hyper-util` and put them into `time.rs`. `timer.rs` will be removed once the `hyper-util` 0.1.2 released, and `hyper_util::rt::TokioTimer` will be used directly. Refer: hyperium/hyper-util#73 --------- Signed-off-by: Yu Li <wfly1998@sina.com>
- Loading branch information
1 parent
ce632c9
commit a651ca2
Showing
5 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,78 @@ | ||
// The `TokioTimer` and `TokioSleep` are copied from `hyper-util`. | ||
// | ||
// Since these are very important, but the new version of `hyper-util` containing these | ||
// has not been released yet, I copied it temporarily. | ||
// | ||
// This file will be removed once the `hyper-util` 0.1.2 released, and | ||
// `hyper_util::rt::TokioTimer` will be used directly. | ||
// | ||
// Refer: https://github.com/hyperium/hyper-util/pull/73 | ||
|
||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
use hyper::rt::{Sleep, Timer}; | ||
use pin_project::pin_project; | ||
|
||
/// A Timer that uses the tokio runtime. | ||
#[non_exhaustive] | ||
#[derive(Default, Clone, Debug)] | ||
pub struct TokioTimer; | ||
|
||
// Use TokioSleep to get tokio::time::Sleep to implement Unpin. | ||
// see https://docs.rs/tokio/latest/tokio/time/struct.Sleep.html | ||
#[pin_project] | ||
#[derive(Debug)] | ||
struct TokioSleep { | ||
#[pin] | ||
inner: tokio::time::Sleep, | ||
} | ||
|
||
// ==== impl TokioTimer ===== | ||
|
||
impl Timer for TokioTimer { | ||
fn sleep(&self, duration: Duration) -> Pin<Box<dyn Sleep>> { | ||
Box::pin(TokioSleep { | ||
inner: tokio::time::sleep(duration), | ||
}) | ||
} | ||
|
||
fn sleep_until(&self, deadline: Instant) -> Pin<Box<dyn Sleep>> { | ||
Box::pin(TokioSleep { | ||
inner: tokio::time::sleep_until(deadline.into()), | ||
}) | ||
} | ||
|
||
fn reset(&self, sleep: &mut Pin<Box<dyn Sleep>>, new_deadline: Instant) { | ||
if let Some(sleep) = sleep.as_mut().downcast_mut_pin::<TokioSleep>() { | ||
sleep.reset(new_deadline) | ||
} | ||
} | ||
} | ||
|
||
impl TokioTimer { | ||
/// Create a new TokioTimer | ||
pub fn new() -> Self { | ||
Self {} | ||
} | ||
} | ||
|
||
impl Future for TokioSleep { | ||
type Output = (); | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
self.project().inner.poll(cx) | ||
} | ||
} | ||
|
||
impl Sleep for TokioSleep {} | ||
|
||
impl TokioSleep { | ||
fn reset(self: Pin<&mut Self>, deadline: Instant) { | ||
self.project().inner.as_mut().reset(deadline.into()); | ||
} | ||
} |
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