2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-10 03:31:52 +08:00

remove named returns and useless closures

This commit is contained in:
Tamir Duberstein 2016-02-20 16:16:18 -05:00
parent a18f806553
commit 903d5c95f0
2 changed files with 11 additions and 15 deletions

16
cmux.go
View File

@ -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{}
@ -49,7 +49,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 },
}
}
@ -59,12 +59,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 {
@ -79,7 +79,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),
@ -155,9 +155,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

View File

@ -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{
@ -88,9 +86,7 @@ var (
// 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
@ -109,7 +105,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 {