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) }