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:
@@ -73,7 +73,16 @@ impl Read for DeadlineStream {
|
|||||||
socket.set_write_timeout(Some(timeout))?;
|
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> {
|
fn time_until_deadline(deadline: Instant) -> IoResult<Duration> {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
match deadline.checked_duration_since(now) {
|
match deadline.checked_duration_since(now) {
|
||||||
None => Err(IoError::new(
|
None => Err(io_error_timeout()),
|
||||||
ErrorKind::TimedOut,
|
|
||||||
"timed out reading response",
|
|
||||||
)),
|
|
||||||
Some(duration) => Ok(duration),
|
Some(duration) => Ok(duration),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn io_error_timeout() -> IoError {
|
||||||
|
IoError::new(ErrorKind::TimedOut, "timed out reading response")
|
||||||
|
}
|
||||||
|
|
||||||
impl ::std::fmt::Debug for Stream {
|
impl ::std::fmt::Debug for Stream {
|
||||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
|
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
|
||||||
write!(
|
write!(
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ fn get_and_expect_timeout(url: String) {
|
|||||||
|
|
||||||
match resp.into_string() {
|
match resp.into_string() {
|
||||||
Err(io_error) => match io_error.kind() {
|
Err(io_error) => match io_error.kind() {
|
||||||
io::ErrorKind::WouldBlock => Ok(()),
|
|
||||||
io::ErrorKind::TimedOut => Ok(()),
|
io::ErrorKind::TimedOut => Ok(()),
|
||||||
_ => Err(format!("{:?}", io_error)),
|
_ => Err(format!("{:?}", io_error)),
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user