-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e4ca77a
commit b76f9c5
Showing
6 changed files
with
102 additions
and
2 deletions.
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
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ pub mod config; | |
pub mod count_stream; | ||
pub mod logger; | ||
pub mod peekable_stream; | ||
pub mod timeout_stream; |
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,92 @@ | ||
use futures::ready; | ||
use pin_project_lite::pin_project; | ||
use std::{ | ||
future::Future, | ||
pin::Pin, | ||
task::{Context, Poll}, | ||
}; | ||
use tokio::{ | ||
io::{self, AsyncRead, AsyncWrite}, | ||
time::{sleep, Duration, Instant, Sleep}, | ||
}; | ||
|
||
pin_project! { | ||
#[derive(Debug)] | ||
pub struct TimeoutStream<S> { | ||
#[pin] | ||
s: S, | ||
duration: Option<Duration>, | ||
sleep: Pin<Box<Sleep>>, | ||
} | ||
} | ||
|
||
impl<S> TimeoutStream<S> { | ||
pub fn new(s: S, duration: Option<Duration>) -> TimeoutStream<S> | ||
where | ||
S: AsyncRead + AsyncWrite, | ||
{ | ||
TimeoutStream { | ||
s, | ||
duration, | ||
sleep: Box::pin(sleep(duration.unwrap_or(Duration::from_secs(1)))), | ||
} | ||
} | ||
} | ||
|
||
impl<S> AsyncRead for TimeoutStream<S> | ||
where | ||
S: AsyncRead + Unpin, | ||
{ | ||
fn poll_read( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &mut io::ReadBuf<'_>, | ||
) -> Poll<io::Result<()>> { | ||
let this = self.project(); | ||
match this.duration { | ||
Some(duration) => match this.s.poll_read(cx, buf) { | ||
Poll::Ready(r) => { | ||
this.sleep.as_mut().reset(Instant::now() + *duration); | ||
Poll::Ready(r) | ||
} | ||
Poll::Pending => { | ||
ready!(this.sleep.as_mut().poll(cx)); | ||
Poll::Ready(Err(io::ErrorKind::TimedOut.into())) | ||
} | ||
}, | ||
None => this.s.poll_read(cx, buf), | ||
} | ||
} | ||
} | ||
|
||
impl<S> AsyncWrite for TimeoutStream<S> | ||
where | ||
S: AsyncWrite + Unpin, | ||
{ | ||
fn poll_write( | ||
self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<Result<usize, io::Error>> { | ||
let this = self.project(); | ||
match this.duration { | ||
Some(duration) => match this.s.poll_write(cx, buf) { | ||
Poll::Ready(r) => { | ||
this.sleep.as_mut().reset(Instant::now() + *duration); | ||
Poll::Ready(r) | ||
} | ||
Poll::Pending => { | ||
ready!(this.sleep.as_mut().poll(cx)); | ||
Poll::Ready(Err(io::ErrorKind::TimedOut.into())) | ||
} | ||
}, | ||
None => this.s.poll_write(cx, buf), | ||
} | ||
} | ||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> { | ||
self.project().s.poll_flush(cx) | ||
} | ||
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), io::Error>> { | ||
self.project().s.poll_shutdown(cx) | ||
} | ||
} |