From 70d180552def143ed9a19a496e9024f98a79606b Mon Sep 17 00:00:00 2001 From: Jeremiah Senkpiel Date: Sat, 6 Mar 2021 19:56:25 -0800 Subject: [PATCH] refactor: improve the code for connection recycling This avoids unnecessarily registering with the waker when we aren't going to poll until Ready. Thanks to Kestrer on discord. --- src/h1/mod.rs | 2 -- src/h1/tcp.rs | 5 ++--- src/h1/tls.rs | 5 ++--- src/h1/utils.rs | 31 ------------------------------- 4 files changed, 4 insertions(+), 39 deletions(-) delete mode 100644 src/h1/utils.rs diff --git a/src/h1/mod.rs b/src/h1/mod.rs index f77c44b..f0a4df9 100644 --- a/src/h1/mod.rs +++ b/src/h1/mod.rs @@ -19,8 +19,6 @@ cfg_if::cfg_if! { use super::{async_trait, Error, HttpClient, Request, Response}; -pub(crate) mod utils; - mod tcp; #[cfg(any(feature = "native-tls", feature = "rustls"))] mod tls; diff --git a/src/h1/tcp.rs b/src/h1/tcp.rs index 76dae6a..d99e13d 100644 --- a/src/h1/tcp.rs +++ b/src/h1/tcp.rs @@ -8,8 +8,6 @@ use deadpool::managed::{Manager, Object, RecycleResult}; use futures::io::{AsyncRead, AsyncWrite}; use futures::task::{Context, Poll}; -use super::utils::PollRead; - #[derive(Clone, Debug)] pub(crate) struct TcpConnection { addr: SocketAddr, @@ -65,7 +63,8 @@ impl Manager for TcpConnection { async fn recycle(&self, conn: &mut TcpStream) -> RecycleResult { let mut buf = [0; 4]; - match PollRead::new(conn, &mut buf).await { + let mut cx = Context::from_waker(futures::task::noop_waker_ref()); + match Pin::new(conn).poll_read(&mut cx, &mut buf) { Poll::Ready(Err(error)) => Err(error), Poll::Ready(Ok(bytes)) if bytes == 0 => Err(std::io::Error::new( std::io::ErrorKind::UnexpectedEof, diff --git a/src/h1/tls.rs b/src/h1/tls.rs index 0d1fa4e..f7c714c 100644 --- a/src/h1/tls.rs +++ b/src/h1/tls.rs @@ -8,8 +8,6 @@ use deadpool::managed::{Manager, Object, RecycleResult}; use futures::io::{AsyncRead, AsyncWrite}; use futures::task::{Context, Poll}; -use super::utils::PollRead; - cfg_if::cfg_if! { if #[cfg(feature = "rustls")] { use async_tls::client::TlsStream; @@ -78,7 +76,8 @@ impl Manager, Error> for TlsConnection { async fn recycle(&self, conn: &mut TlsStream) -> RecycleResult { let mut buf = [0; 4]; - match PollRead::new(conn, &mut buf).await { + let mut cx = Context::from_waker(futures::task::noop_waker_ref()); + match Pin::new(conn).poll_read(&mut cx, &mut buf) { Poll::Ready(Err(error)) => Err(error), Poll::Ready(Ok(bytes)) if bytes == 0 => Err(std::io::Error::new( std::io::ErrorKind::UnexpectedEof, diff --git a/src/h1/utils.rs b/src/h1/utils.rs deleted file mode 100644 index 1af600f..0000000 --- a/src/h1/utils.rs +++ /dev/null @@ -1,31 +0,0 @@ -use std::io; -use std::pin::Pin; - -use futures::future::Future; -use futures::io::AsyncRead; -use futures::task::{Context, Poll}; - -/// Like the `futures::io::Read` future, but returning the underlying `futures::task::Poll`. -#[derive(Debug)] -#[must_use = "futures do nothing unless you `.await` or poll them"] -pub(crate) struct PollRead<'a, R: ?Sized> { - reader: &'a mut R, - buf: &'a mut [u8], -} - -impl Unpin for PollRead<'_, R> {} - -impl<'a, R: AsyncRead + ?Sized + Unpin> PollRead<'a, R> { - pub(super) fn new(reader: &'a mut R, buf: &'a mut [u8]) -> Self { - Self { reader, buf } - } -} - -impl Future for PollRead<'_, R> { - type Output = Poll>; - - fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = &mut *self; - Poll::Ready(Pin::new(&mut this.reader).poll_read(cx, this.buf)) - } -}