Set deadline on client websocket sends.

It appears that the websocket package will occasionally enter a
Codec's Send function and block forever, never erroring (presumably
due to a closed connection).  This change adds a deadline for the send
of two seconds.  If the send cannot complete before the deadline is
reached, the send is aborted and the connection is assumed to be lost.

A buffer should be added here as well, so even waiting max two seconds
for the send to error out won't cause wallet code to block.
This commit is contained in:
Josh Rickmar 2014-02-19 13:57:52 -05:00
parent 3a23fdaf64
commit 438f55a0a4

View file

@ -330,6 +330,8 @@ func WSSendRecv(ws *websocket.Conn) {
} }
}() }()
const deadline time.Duration = 2 * time.Second
for { for {
select { select {
case m, ok := <-received: case m, ok := <-received:
@ -348,7 +350,12 @@ func WSSendRecv(ws *websocket.Conn) {
}(m) }(m)
case m := <-cc.send: case m := <-cc.send:
if err := websocket.Message.Send(ws, m); err != nil { err := ws.SetWriteDeadline(time.Now().Add(deadline))
if err != nil {
return
}
err = websocket.Message.Send(ws, m)
if err != nil {
// Frontend disconnected. // Frontend disconnected.
return return
} }