Skip to content

Commit

Permalink
Merge pull request #77 from Fishrock123/fix-h1-connection-pooling
Browse files Browse the repository at this point in the history
fix: properly check if connections can be recycled
  • Loading branch information
Fishrock123 authored Mar 6, 2021
2 parents e62f1ef + 70d1805 commit cab5441
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/h1/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ impl Manager<TcpStream, std::io::Error> for TcpConnection {

async fn recycle(&self, conn: &mut TcpStream) -> RecycleResult<std::io::Error> {
let mut buf = [0; 4];
conn.peek(&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,
"connection appeared to be closed (EoF)",
)),
_ => Ok(()),
}?;
Ok(())
}
}
14 changes: 10 additions & 4 deletions src/h1/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ impl Manager<TlsStream<TcpStream>, Error> for TlsConnection {

async fn recycle(&self, conn: &mut TlsStream<TcpStream>) -> RecycleResult<Error> {
let mut buf = [0; 4];
conn.get_ref()
.peek(&mut buf[..])
.await
.map_err(Error::from)?;
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,
"connection appeared to be closed (EoF)",
)),
_ => Ok(()),
}
.map_err(Error::from)?;
Ok(())
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,17 @@ SOFTWARE.

Ok(())
}

#[atest]
async fn keep_alive() {
let _mock_guard = mockito::mock("GET", "/report")
.with_status(200)
.expect_at_least(2)
.create();

let client = DefaultClient::new();
let url: Url = format!("{}/report", mockito::server_url()).parse().unwrap();
let req = Request::new(http_types::Method::Get, url);
client.send(req.clone()).await.unwrap();
client.send(req.clone()).await.unwrap();
}

0 comments on commit cab5441

Please sign in to comment.