Silence net.ErrClosed errors

This commit is contained in:
Simon Ser 2021-05-20 00:07:44 +02:00
parent 7e6ce23dac
commit c8a54235d0
3 changed files with 27 additions and 2 deletions

View File

@ -194,7 +194,7 @@ func newConn(srv *Server, ic ircConn, options *connOptions) *conn {
break break
} }
} }
if err := c.conn.Close(); err != nil { if err := c.conn.Close(); err != nil && !isErrClosed(err) {
c.logger.Printf("failed to close connection: %v", err) c.logger.Printf("failed to close connection: %v", err)
} else { } else {
c.logger.Printf("connection closed") c.logger.Printf("connection closed")
@ -232,7 +232,9 @@ func (c *conn) Close() error {
func (c *conn) ReadMessage() (*irc.Message, error) { func (c *conn) ReadMessage() (*irc.Message, error) {
msg, err := c.conn.ReadMessage() msg, err := c.conn.ReadMessage()
if err != nil { if isErrClosed(err) {
return nil, io.EOF
} else if err != nil {
return nil, err return nil, err
} }

11
net_go113.go Normal file
View File

@ -0,0 +1,11 @@
// +build !go1.16
package soju
import (
"strings"
)
func isErrClosed(err error) bool {
return err != nil && strings.Contains(err.Error(), "use of closed network connection")
}

12
net_go116.go Normal file
View File

@ -0,0 +1,12 @@
// +build go1.16
package soju
import (
"errors"
"net"
)
func isErrClosed(err error) bool {
return errors.Is(err, net.ErrClosed)
}