Skip to content

Commit

Permalink
refactor: improve the code for connection recycling
Browse files Browse the repository at this point in the history
This avoids unnecessarily registering with the waker when we aren't going to poll until Ready.

Thanks to Kestrer on discord.
  • Loading branch information
Fishrock123 committed Mar 7, 2021
1 parent d129f07 commit 70d1805
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 39 deletions.
2 changes: 0 additions & 2 deletions src/h1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/h1/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -65,7 +63,8 @@ impl Manager<TcpStream, std::io::Error> for TcpConnection {

async fn recycle(&self, conn: &mut TcpStream) -> RecycleResult<std::io::Error> {
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,
Expand Down
5 changes: 2 additions & 3 deletions src/h1/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,7 +76,8 @@ impl Manager<TlsStream<TcpStream>, Error> for TlsConnection {

async fn recycle(&self, conn: &mut TlsStream<TcpStream>) -> RecycleResult<Error> {
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,
Expand Down
31 changes: 0 additions & 31 deletions src/h1/utils.rs

This file was deleted.

0 comments on commit 70d1805

Please sign in to comment.