pkg/transport: add 'IsClosedConnError'

Signed-off-by: Gyu-Ho Lee <gyuhox@gmail.com>
This commit is contained in:
Gyu-Ho Lee 2017-04-13 10:48:06 -07:00
parent 2dd361aba5
commit 8ce579aac9
2 changed files with 22 additions and 0 deletions

View File

@ -27,6 +27,7 @@ import (
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/coreos/etcd/pkg/tlsutil"
@ -269,3 +270,12 @@ func ShallowCopyTLSConfig(cfg *tls.Config) *tls.Config {
}
return &ncfg
}
// IsClosedConnError returns true if the error is from closing listener, cmux.
// copied from golang.org/x/net/http2/http2.go
func IsClosedConnError(err error) bool {
// 'use of closed network connection' (Go <=1.8)
// 'use of closed file or network connection' (Go >1.8, internal/poll.ErrClosing)
// 'mux: listener closed' (cmux.ErrListenerClosed)
return err != nil && strings.Contains(err.Error(), "closed")
}

View File

@ -274,3 +274,15 @@ func TestNewListenerTLSInfoSelfCert(t *testing.T) {
}
testNewListenerTLSInfoAccept(t, tlsinfo)
}
func TestIsClosedConnError(t *testing.T) {
l, err := NewListener("testsocket", "unix", nil)
if err != nil {
t.Errorf("error listening on unix socket (%v)", err)
}
l.Close()
_, err = l.Accept()
if !IsClosedConnError(err) {
t.Fatalf("expect true, got false (%v)", err)
}
}