Make DeadlineStream Read use the BufRead.

This commit is contained in:
Jacob Hoffman-Andrews
2020-11-28 23:33:28 -08:00
parent 6a22c54ba2
commit a286a7a22d

View File

@@ -82,23 +82,16 @@ impl BufRead for DeadlineStream {
impl Read for DeadlineStream { impl Read for DeadlineStream {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if let Some(deadline) = self.deadline { // All reads on a DeadlineStream use the BufRead impl. This ensures
let timeout = time_until_deadline(deadline)?; // that we have a chance to set the correct timeout before each recv
if let Some(socket) = self.stream.socket() { // syscall.
socket.set_read_timeout(Some(timeout))?; // Copied from the BufReader implementation of `read()`.
socket.set_write_timeout(Some(timeout))?; let nread = {
} let mut rem = self.fill_buf()?;
} rem.read(buf)?
self.stream.read(buf).map_err(|e| { };
// On unix-y platforms set_read_timeout and set_write_timeout self.consume(nread);
// causes ErrorKind::WouldBlock instead of ErrorKind::TimedOut. Ok(nread)
// Since the socket most definitely not set_nonblocking(true),
// we can safely normalize WouldBlock to TimedOut
if e.kind() == io::ErrorKind::WouldBlock {
return io_err_timeout("timed out reading response".to_string());
}
e
})
} }
} }