Normalize timeout ErrorKind::WouldBlock to be TimedOut (#131)

`set_read_timeout` and `set_write_timeout` can cause `ErrorKind::WouldBlock` on unix-y platforms.

This PR normalizes those cases to `ErrorKind::TimedOut`. This will make it simpler higher up in the
stack to deal with timeouts.
This commit is contained in:
Martin Algesten
2020-08-07 19:06:56 +02:00
committed by GitHub
parent 22d815e5e7
commit db4cc90956
2 changed files with 15 additions and 6 deletions

View File

@@ -73,7 +73,16 @@ impl Read for DeadlineStream {
socket.set_write_timeout(Some(timeout))?;
}
}
self.stream.read(buf)
self.stream.read(buf).map_err(|e| {
// On unix-y platforms set_read_timeout and set_write_timeout
// causes ErrorKind::WouldBlock instead of ErrorKind::TimedOut.
// Since the socket most definitely not set_nonblocking(true),
// we can safely normalize WouldBlock to TimedOut
if e.kind() == ErrorKind::WouldBlock {
return io_error_timeout();
}
e
})
}
}
@@ -82,14 +91,15 @@ impl Read for DeadlineStream {
fn time_until_deadline(deadline: Instant) -> IoResult<Duration> {
let now = Instant::now();
match deadline.checked_duration_since(now) {
None => Err(IoError::new(
ErrorKind::TimedOut,
"timed out reading response",
)),
None => Err(io_error_timeout()),
Some(duration) => Ok(duration),
}
}
fn io_error_timeout() -> IoError {
IoError::new(ErrorKind::TimedOut, "timed out reading response")
}
impl ::std::fmt::Debug for Stream {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
write!(