Hacker News new | ask | show | jobs
by brickbrd 1468 days ago
What does "stream.write_all(&number_as_bytes).unwrap();" do if the socket buffer is full? Does it block this virtual thread running this function? Or does the stream keep buffering? or is it sending the message to some other process which is accumulating those messages. What if I don't wait this thread to block and instead do something else?

I believe all of these are handled. I just cannot find sufficient documentation to understand the details of how this works.

1 comments

Same as the synchronous version: It will block until more data can be written, and then go on and write as much as possible using another async .write() call. It's the same as:

    let mut offset = 0;
    while offset != number_as_bytes.length() {
        let written = stream.write(&number_as_bytes[offset..]).await.unwrap();
        offset += written;
    }
The synchronous version would be the same without the .await, and offers stronger guarantees that either all bytes are written to the socket or the socket errored and is dead. The async version could be cancelled in the middle of the invocation after some segments have already been written.