From d01756d78354ae7f81337346c1adf5a576d10421 Mon Sep 17 00:00:00 2001 From: Matt Kline Date: Fri, 5 Jan 2024 21:33:44 -0800 Subject: [PATCH] response: We *are* blocking! We're not using some event-driven async I/O, that's the whole point of this crate. The sockets are not set up as non-blocking and it would be very unexpected for the OS to return EWOULDBLOCK from recv(). --- examples/iterator.rs | 14 ++------------ src/response.rs | 8 ++++---- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/examples/iterator.rs b/examples/iterator.rs index 34b589d..60b07d9 100644 --- a/examples/iterator.rs +++ b/examples/iterator.rs @@ -5,18 +5,8 @@ fn main() -> Result<(), minreq::Error> { let mut buffer = Vec::new(); for byte in minreq::get("http://example.com").send_lazy()? { // The connection could have a problem at any point during the - // download, so each byte needs to be unwrapped. An IO error - // of the WouldBlock kind may also be returned, but it is not - // a fatal error, it just means that we're still waiting for - // more bytes. Some operating systems just block while waiting - // for more bytes, others return a WouldBlock error. - let (byte, len) = match byte { - Ok((byte, len)) => (byte, len), - Err(minreq::Error::IoError(err)) if err.kind() == std::io::ErrorKind::WouldBlock => { - continue - } - Err(err) => return Err(err), - }; + // download, so each byte needs to be unwrapped. + let (byte, len) = byte?; // The `byte` is the current u8 of data we're iterating // through. diff --git a/src/response.rs b/src/response.rs index b3e6951..1df8643 100644 --- a/src/response.rs +++ b/src/response.rs @@ -47,8 +47,8 @@ impl Response { body.push(byte); } Err(Error::IoError(err)) if err.kind() == ErrorKind::WouldBlock => { - // Busy waiting isn't ideal, but waiting for N milliseconds would be worse. - std::thread::yield_now(); + // We're a blocking socket, so EWOULDBLOCK indicates a timeout + return Err(Error::IoError(io::Error::from(ErrorKind::TimedOut))); } Err(err) => return Err(err), } @@ -547,8 +547,8 @@ fn read_line( } } Err(err) if err.kind() == ErrorKind::WouldBlock => { - // Busy waiting isn't ideal, but waiting for N milliseconds would be worse. - std::thread::yield_now(); + // We're a blocking socket, so EWOULDBLOCK indicates a timeout + return Err(Error::IoError(io::Error::from(ErrorKind::TimedOut))); } Err(err) => return Err(Error::IoError(err)), }