diff --git a/cmux.go b/cmux.go index 33055b6..cbc259b 100644 --- a/cmux.go +++ b/cmux.go @@ -7,11 +7,11 @@ import ( ) // Matcher matches a connection based on its content. -type Matcher func(r io.Reader) (ok bool) +type Matcher func(io.Reader) bool // ErrorHandler handles an error and returns whether // the mux should continue serving the listener. -type ErrorHandler func(err error) (ok bool) +type ErrorHandler func(error) bool var _ net.Error = ErrNotMatched{} @@ -47,7 +47,7 @@ func New(l net.Listener) CMux { return &cMux{ root: l, bufLen: 1024, - errh: func(err error) bool { return true }, + errh: func(_ error) bool { return true }, } } @@ -57,12 +57,12 @@ type CMux interface { // the connections matched by at least one of the matcher. // // The order used to call Match determines the priority of matchers. - Match(matchers ...Matcher) net.Listener + Match(...Matcher) net.Listener // Serve starts multiplexing the listener. Serve blocks and perhaps // should be invoked concurrently within a go routine. Serve() error // HandleError registers an error handler that handles listener errors. - HandleError(h ErrorHandler) + HandleError(ErrorHandler) } type matchersListener struct { @@ -77,7 +77,7 @@ type cMux struct { sls []matchersListener } -func (m *cMux) Match(matchers ...Matcher) (l net.Listener) { +func (m *cMux) Match(matchers ...Matcher) net.Listener { ml := muxListener{ Listener: m.root, connc: make(chan net.Conn, m.bufLen), @@ -153,9 +153,9 @@ type muxListener struct { donec chan struct{} } -func (l muxListener) Accept() (c net.Conn, err error) { +func (l muxListener) Accept() (net.Conn, error) { select { - case c = <-l.connc: + case c := <-l.connc: return c, nil case <-l.donec: return nil, ErrListenerClosed diff --git a/matchers.go b/matchers.go index 599f4b8..5f1024b 100644 --- a/matchers.go +++ b/matchers.go @@ -21,9 +21,7 @@ func Any() Matcher { // starts with any of the strings in strs. func PrefixMatcher(strs ...string) Matcher { pt := newPatriciaTreeString(strs...) - return func(r io.Reader) bool { - return pt.matchPrefix(r) - } + return pt.matchPrefix } var defaultHTTPMethods = []string{ @@ -84,9 +82,7 @@ var http2Preface = []byte(http2.ClientPreface) // HTTP2 parses the frame header of the first frame to detect whether the // connection is an HTTP2 connection. func HTTP2() Matcher { - return func(r io.Reader) bool { - return hasHTTP2Preface(r) - } + return hasHTTP2Preface } // HTTP1HeaderField returns a matcher matching the header fields of the first @@ -105,7 +101,7 @@ func HTTP2HeaderField(name, value string) Matcher { } } -func hasHTTP2Preface(r io.Reader) (ok bool) { +func hasHTTP2Preface(r io.Reader) bool { b := make([]byte, len(http2Preface)) n, err := r.Read(b) if err != nil {