Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need to check if all data sent #305

Closed
nskforward opened this issue May 28, 2021 · 3 comments
Closed

Need to check if all data sent #305

nskforward opened this issue May 28, 2021 · 3 comments

Comments

@nskforward
Copy link

In write.go file function Write does not return the number of actually sent bytes.
So currently I cannot do the second assertion in code below:

n, err := conn.Write(ctx, websocket.MessageBinary, data)
if err != nil {
...
}
if len(data) != n {
// send remaining bytes in the next Write call or throw an error
}
@qcha0
Copy link

qcha0 commented Jun 11, 2021

in my opinion, you no need to care about the number of actually sent bytes
function conn.Write write all the number of your send bytes done if the err is nil, alse means n == len(data)

@nhooyr
Copy link
Contributor

nhooyr commented Aug 6, 2021

Hello @nskforward!

So there are generally no write errors on which it makes sense to retry in Go.

First, you might get an error because you cannot write to the socket anymore. The peer may have explicitly disconnected, the peer may not be responding or some operating system limit may have been exceeded. It doesn't make sense to retry here as the connection is effectively unsable in some way and your application should release the associated resources.

Second, a deadline may have been triggered. Here you could retry but it doesn't make much sense. If a deadline was triggered, then the peer is too slow to receive your messages. Rather than retrying, why not just make your write deadline longer? If you're using a small deadline to do some sort of polling in your write goroutine then you'd be misusing Go as the purpose of goroutines and IO built into the runtime is to avoid you having to do that kind of stuff manually.

It might make sense to write an error in a multistream protocol where you could use a different stream to let the peer know they're being disconnected for being too slow but WebSockets is unfortunately not such a protocol.

But regardless, with this library you currently don't have the choice to retry as the API exposed to us by *http.Client for upgrading connections does not allow interrupting in progress writes without closing the entire connection. So any error returned from a write means you cannot write again as either the connection is broken or a write deadline was triggered and so the connection was closed to free any goroutine currently stuck in a Write.

See #242

@nhooyr nhooyr closed this as completed Aug 6, 2021
@nhooyr
Copy link
Contributor

nhooyr commented Aug 6, 2021

Apologies for the delayed response!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants