From cc851dbb3f4725562729f12663e246690a61c620 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Petazzoni?= Date: Wed, 16 Oct 2013 13:40:52 -0700 Subject: [PATCH] Catch errClosing error when TCP and UDP proxies are terminated. --- network_proxy.go | 12 ++++++++++-- utils/utils.go | 10 ++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/network_proxy.go b/network_proxy.go index 1bb849538b..86545801b5 100644 --- a/network_proxy.go +++ b/network_proxy.go @@ -103,7 +103,11 @@ func (proxy *TCPProxy) Run() { for { client, err := proxy.listener.Accept() if err != nil { - utils.Errorf("Stopping proxy on tcp/%v for tcp/%v (%v)", proxy.frontendAddr, proxy.backendAddr, err.Error()) + if utils.IsClosedError(err) { + utils.Debugf("Stopping proxy on tcp/%v for tcp/%v (socket was closed)", proxy.frontendAddr, proxy.backendAddr) + } else { + utils.Errorf("Stopping proxy on tcp/%v for tcp/%v (%v)", proxy.frontendAddr, proxy.backendAddr, err.Error()) + } return } go proxy.clientLoop(client.(*net.TCPConn), quit) @@ -205,7 +209,11 @@ func (proxy *UDPProxy) Run() { // NOTE: Apparently ReadFrom doesn't return // ECONNREFUSED like Read do (see comment in // UDPProxy.replyLoop) - utils.Errorf("Stopping proxy on udp/%v for udp/%v (%v)", proxy.frontendAddr, proxy.backendAddr, err.Error()) + if utils.IsClosedError(err) { + utils.Debugf("Stopping proxy on udp/%v for udp/%v (socket was closed)", proxy.frontendAddr, proxy.backendAddr) + } else { + utils.Errorf("Stopping proxy on udp/%v for udp/%v (%v)", proxy.frontendAddr, proxy.backendAddr, err.Error()) + } break } diff --git a/utils/utils.go b/utils/utils.go index c78e61d801..b0327dd40c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1028,3 +1028,13 @@ type StatusError struct { func (e *StatusError) Error() string { return fmt.Sprintf("Status: %d", e.Status) } + +func IsClosedError(err error) bool { + /* This comparison is ugly, but unfortunately, net.go doesn't export errClosing. + * See: + * http://golang.org/src/pkg/net/net.go + * https://code.google.com/p/go/issues/detail?id=4337 + * https://groups.google.com/forum/#!msg/golang-nuts/0_aaCvBmOcM/SptmDyX1XJMJ + */ + return strings.HasSuffix(err.Error(), "use of closed network connection") +}