From 438f55a0a44880158cf37a7a4d6ae309dbe3cf13 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Wed, 19 Feb 2014 13:57:52 -0500 Subject: [PATCH] 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. --- sockets.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sockets.go b/sockets.go index 3c61b1d..0befa90 100644 --- a/sockets.go +++ b/sockets.go @@ -330,6 +330,8 @@ func WSSendRecv(ws *websocket.Conn) { } }() + const deadline time.Duration = 2 * time.Second + for { select { case m, ok := <-received: @@ -348,7 +350,12 @@ func WSSendRecv(ws *websocket.Conn) { }(m) 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. return }