network: use net.ErrClosed to check network connection was closed

Close #1765.
This commit is contained in:
Anna Shaleva 2022-02-22 19:48:59 +03:00 committed by AnnaShaleva
parent 2096ad6e81
commit 753d604784

View file

@ -1,8 +1,8 @@
package network package network
import ( import (
"errors"
"net" "net"
"regexp"
"sync" "sync"
"time" "time"
@ -19,8 +19,6 @@ type TCPTransport struct {
quit bool quit bool
} }
var reClosedNetwork = regexp.MustCompile(".* use of closed network connection")
// NewTCPTransport returns a new TCPTransport that will listen for // NewTCPTransport returns a new TCPTransport that will listen for
// new incoming peer connections. // new incoming peer connections.
func NewTCPTransport(s *Server, bindAddr string, log *zap.Logger) *TCPTransport { func NewTCPTransport(s *Server, bindAddr string, log *zap.Logger) *TCPTransport {
@ -65,7 +63,7 @@ func (t *TCPTransport) Accept() {
t.lock.Lock() t.lock.Lock()
quit := t.quit quit := t.quit
t.lock.Unlock() t.lock.Unlock()
if t.isCloseError(err) && quit { if errors.Is(err, net.ErrClosed) && quit {
break break
} }
t.log.Warn("TCP accept error", zap.Error(err)) t.log.Warn("TCP accept error", zap.Error(err))
@ -76,16 +74,6 @@ func (t *TCPTransport) Accept() {
} }
} }
func (t *TCPTransport) isCloseError(err error) bool {
if opErr, ok := err.(*net.OpError); ok {
if reClosedNetwork.Match([]byte(opErr.Error())) {
return true
}
}
return false
}
// Close implements the Transporter interface. // Close implements the Transporter interface.
func (t *TCPTransport) Close() { func (t *TCPTransport) Close() {
t.lock.Lock() t.lock.Lock()