Skip to content

Commit 70d1805

Browse files
committed
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.
1 parent d129f07 commit 70d1805

File tree

4 files changed

+4
-39
lines changed

4 files changed

+4
-39
lines changed

src/h1/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ cfg_if::cfg_if! {
1919

2020
use super::{async_trait, Error, HttpClient, Request, Response};
2121

22-
pub(crate) mod utils;
23-
2422
mod tcp;
2523
#[cfg(any(feature = "native-tls", feature = "rustls"))]
2624
mod tls;

src/h1/tcp.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use deadpool::managed::{Manager, Object, RecycleResult};
88
use futures::io::{AsyncRead, AsyncWrite};
99
use futures::task::{Context, Poll};
1010

11-
use super::utils::PollRead;
12-
1311
#[derive(Clone, Debug)]
1412
pub(crate) struct TcpConnection {
1513
addr: SocketAddr,
@@ -65,7 +63,8 @@ impl Manager<TcpStream, std::io::Error> for TcpConnection {
6563

6664
async fn recycle(&self, conn: &mut TcpStream) -> RecycleResult<std::io::Error> {
6765
let mut buf = [0; 4];
68-
match PollRead::new(conn, &mut buf).await {
66+
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
67+
match Pin::new(conn).poll_read(&mut cx, &mut buf) {
6968
Poll::Ready(Err(error)) => Err(error),
7069
Poll::Ready(Ok(bytes)) if bytes == 0 => Err(std::io::Error::new(
7170
std::io::ErrorKind::UnexpectedEof,

src/h1/tls.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use deadpool::managed::{Manager, Object, RecycleResult};
88
use futures::io::{AsyncRead, AsyncWrite};
99
use futures::task::{Context, Poll};
1010

11-
use super::utils::PollRead;
12-
1311
cfg_if::cfg_if! {
1412
if #[cfg(feature = "rustls")] {
1513
use async_tls::client::TlsStream;
@@ -78,7 +76,8 @@ impl Manager<TlsStream<TcpStream>, Error> for TlsConnection {
7876

7977
async fn recycle(&self, conn: &mut TlsStream<TcpStream>) -> RecycleResult<Error> {
8078
let mut buf = [0; 4];
81-
match PollRead::new(conn, &mut buf).await {
79+
let mut cx = Context::from_waker(futures::task::noop_waker_ref());
80+
match Pin::new(conn).poll_read(&mut cx, &mut buf) {
8281
Poll::Ready(Err(error)) => Err(error),
8382
Poll::Ready(Ok(bytes)) if bytes == 0 => Err(std::io::Error::new(
8483
std::io::ErrorKind::UnexpectedEof,

src/h1/utils.rs

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)