Skip to content

Commit

Permalink
response: We *are* blocking!
Browse files Browse the repository at this point in the history
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().
  • Loading branch information
mrkline committed Jan 7, 2024
1 parent 4d40862 commit d01756d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
14 changes: 2 additions & 12 deletions examples/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand Down Expand Up @@ -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)),
}
Expand Down

0 comments on commit d01756d

Please sign in to comment.