From 753d6047843c23d51382922fbf4ed4f0e3ecfa27 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Tue, 22 Feb 2022 19:48:59 +0300 Subject: [PATCH] network: use net.ErrClosed to check network connection was closed Close #1765. --- pkg/network/tcp_transport.go | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/pkg/network/tcp_transport.go b/pkg/network/tcp_transport.go index e9c4efea7..dbce2ef53 100644 --- a/pkg/network/tcp_transport.go +++ b/pkg/network/tcp_transport.go @@ -1,8 +1,8 @@ package network import ( + "errors" "net" - "regexp" "sync" "time" @@ -19,8 +19,6 @@ type TCPTransport struct { quit bool } -var reClosedNetwork = regexp.MustCompile(".* use of closed network connection") - // NewTCPTransport returns a new TCPTransport that will listen for // new incoming peer connections. func NewTCPTransport(s *Server, bindAddr string, log *zap.Logger) *TCPTransport { @@ -65,7 +63,7 @@ func (t *TCPTransport) Accept() { t.lock.Lock() quit := t.quit t.lock.Unlock() - if t.isCloseError(err) && quit { + if errors.Is(err, net.ErrClosed) && quit { break } 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. func (t *TCPTransport) Close() { t.lock.Lock()