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.
This commit is contained in:
Dave Collins 2016-03-10 22:21:59 -06:00
parent 73a9dd628d
commit e6e3b66040

View file

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