Check for server closed connections.

This builds on 753d61b. Before we send a request, we can do a 1-byte
nonblocking peek on the connection. If the server has closed the
connection, this will give us an EOF, and we can take the connection out
of the pool before sending any request on it. This will reduce the
likelihood that we send a non-retryable POST on an already-closed
connection.

The server could still potentially close the connection between when we
make this check and when we finish sending the request, but this should
handle the majority of cases.
This commit is contained in:
Jacob Hoffman-Andrews
2020-06-12 20:55:19 -07:00
committed by Martin Algesten
parent 0b6323df44
commit b4c15eef2c
2 changed files with 35 additions and 2 deletions

View File

@@ -274,8 +274,14 @@ fn connect_socket(unit: &Unit, use_pooled: bool) -> Result<(Stream, bool), Error
if use_pooled {
let state = &mut unit.agent.lock().unwrap();
if let Some(agent) = state.as_mut() {
if let Some(stream) = agent.pool.try_get_connection(&unit.url) {
return Ok((stream, true));
// The connection may have been closed by the server
// due to idle timeout while it was sitting in the pool.
// Loop until we find one that is still good or run out of connections.
while let Some(stream) = agent.pool.try_get_connection(&unit.url) {
let server_closed = stream.server_closed()?;
if !server_closed {
return Ok((stream, true));
}
}
}
}