Skip to content

Commit

Permalink
fix(client): EofReader by nature means the connection is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Sep 2, 2015
1 parent b833f67 commit 32e09a0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
23 changes: 22 additions & 1 deletion src/client/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,16 @@ struct PooledStreamInner<S> {
impl<S: NetworkStream> Read for PooledStream<S> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.as_mut().unwrap().stream.read(buf)
match self.inner.as_mut().unwrap().stream.read(buf) {
Ok(0) => {
// if the wrapped stream returns EOF (Ok(0)), that means the
// server has closed the stream. we must be sure this stream
// is dropped and not put back into the pool.
self.is_closed = true;
Ok(0)
},
r => r
}
}
}

Expand Down Expand Up @@ -216,6 +225,7 @@ impl<S> Drop for PooledStream<S> {
#[cfg(test)]
mod tests {
use std::net::Shutdown;
use std::io::Read;
use mock::{MockConnector};
use net::{NetworkConnector, NetworkStream};

Expand Down Expand Up @@ -254,4 +264,15 @@ mod tests {
let locked = pool.inner.lock().unwrap();
assert_eq!(locked.conns.len(), 0);
}

#[test]
fn test_eof_closes() {
let pool = mocked!();

let mut stream = pool.connect("127.0.0.1", 3000, "http").unwrap();
assert_eq!(stream.read(&mut [0]).unwrap(), 0);
drop(stream);
let locked = pool.inner.lock().unwrap();
assert_eq!(locked.conns.len(), 0);
}
}
11 changes: 3 additions & 8 deletions src/client/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub struct Response {
pub url: Url,
status_raw: RawStatus,
message: Box<HttpMessage>,
is_drained: bool,
}

impl Response {
Expand Down Expand Up @@ -54,7 +53,6 @@ impl Response {
headers: headers,
url: url,
status_raw: raw_status,
is_drained: !message.has_body(),
message: message,
})
}
Expand All @@ -70,10 +68,6 @@ impl Read for Response {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
match self.message.read(buf) {
Ok(0) => {
self.is_drained = true;
Ok(0)
},
Err(e) => {
let _ = self.message.close_connection();
Err(e)
Expand All @@ -90,8 +84,9 @@ impl Drop for Response {
//
// otherwise, the response has been drained. we should check that the
// server has agreed to keep the connection open
trace!("Response.drop is_drained={}", self.is_drained);
if !(self.is_drained && http::should_keep_alive(self.version, &self.headers)) {
let is_drained = !self.message.has_body();
trace!("Response.drop is_drained={}", is_drained);
if !(is_drained && http::should_keep_alive(self.version, &self.headers)) {
trace!("Response.drop closing connection");
if let Err(e) = self.message.close_connection() {
error!("Response.drop error closing connection: {}", e);
Expand Down
1 change: 1 addition & 0 deletions src/http/h1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ impl HttpMessage for Http11Message {
Some(EmptyReader(..)) |
Some(SizedReader(_, 0)) |
Some(ChunkedReader(_, Some(0))) => false,
// specifically EofReader is always true
_ => true
}
}
Expand Down

0 comments on commit 32e09a0

Please sign in to comment.