From e6e3b66040e809966ce190411bdd6e121c6e38f9 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 10 Mar 2016 22:21:59 -0600 Subject: [PATCH] Improve error log avoidance on client disconnect. The existing check to only log errors from websocket connections when the error is not because the connection is being shutdown only detects the single case where the remote client was shutdown. This commit modifies that logic to include when the connection is being forcibly shutdown via closing the quit channel and when it has been shutdown from a client side close. --- infrastructure.go | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/infrastructure.go b/infrastructure.go index 5edf5a3a..098edcc5 100644 --- a/infrastructure.go +++ b/infrastructure.go @@ -13,6 +13,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "io/ioutil" "net" "net/http" @@ -362,6 +363,28 @@ func (c *Client) handleMessage(msg []byte) { request.responseChan <- &response{result: result, err: err} } +// shouldLogReadError returns whether or not the passed error, which is expected +// to have come from reading from the websocket connection in wsInHandler, +// should be logged. +func (c *Client) shouldLogReadError(err error) bool { + // No logging when the connetion is being forcibly disconnected. + select { + case <-c.shutdown: + return false + default: + } + + // No logging when the connection has been disconnected. + if err == io.EOF { + return false + } + if opErr, ok := err.(*net.OpError); ok && !opErr.Temporary() { + return false + } + + return true +} + // wsInHandler handles all incoming messages for the websocket connection // associated with the client. It must be run as a goroutine. func (c *Client) wsInHandler() { @@ -379,7 +402,7 @@ out: _, msg, err := c.wsConn.ReadMessage() if err != nil { // Log the error if it's not due to disconnecting. - if _, ok := err.(*net.OpError); !ok { + if c.shouldLogReadError(err) { log.Errorf("Websocket receive error from "+ "%s: %v", c.config.Host, err) }