websocket
websocket copied to clipboard
SetReadDeadline (an alternative to deadline contexts)
Hey,
For the continuous reads, it's convenient to use something like Gorilla's SetReadDeadline, instead of creating a new context each time a Read is called. Though I know Gorilla's encapsulating the net.Conn, while this pkg isn't. How do you think is it possible to add something like that in this library?
Already implemented! Check out websocket.NetConn!
hmm but that creates a new conn instead of letting you cheaply set a deadline on the existing socket.
creating a new context in a loop like this seems like a terrible idea, since the defer isnt called on loop re-entry
for ;; {
ctx, cancel := context.WithTimeout(r.Context(), time.Second*10)
defer cancel()
var v interface{}
err = wsjson.Read(ctx, c, &v)
if err != nil {
// ...
}
}
@aep You don't have to create a new net.Conn on every read/write, just once. If the tiny allocation overhead is a real concern, using Go is likely not appropriate. You'd want your websocket server in C.
I'm going to reopen this as I might change the API to a tiered approach where contexts are only used if requested.
Will roll into #402