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

remove named returns and useless closures

This commit is contained in:
Tamir Duberstein 2016-02-20 16:16:18 -05:00 committed by Soheil Hassas Yeganeh
parent ecc37f82cd
commit b5e73ea381
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. // 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 // ErrorHandler handles an error and returns whether
// the mux should continue serving the listener. // the mux should continue serving the listener.
type ErrorHandler func(err error) (ok bool) type ErrorHandler func(error) bool
var _ net.Error = ErrNotMatched{} var _ net.Error = ErrNotMatched{}
@ -47,7 +47,7 @@ func New(l net.Listener) CMux {
return &cMux{ return &cMux{
root: l, root: l,
bufLen: 1024, 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 connections matched by at least one of the matcher.
// //
// The order used to call Match determines the priority of matchers. // 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 // Serve starts multiplexing the listener. Serve blocks and perhaps
// should be invoked concurrently within a go routine. // should be invoked concurrently within a go routine.
Serve() error Serve() error
// HandleError registers an error handler that handles listener errors. // HandleError registers an error handler that handles listener errors.
HandleError(h ErrorHandler) HandleError(ErrorHandler)
} }
type matchersListener struct { type matchersListener struct {
@ -77,7 +77,7 @@ type cMux struct {
sls []matchersListener sls []matchersListener
} }
func (m *cMux) Match(matchers ...Matcher) (l net.Listener) { func (m *cMux) Match(matchers ...Matcher) net.Listener {
ml := muxListener{ ml := muxListener{
Listener: m.root, Listener: m.root,
connc: make(chan net.Conn, m.bufLen), connc: make(chan net.Conn, m.bufLen),
@ -153,9 +153,9 @@ type muxListener struct {
donec chan struct{} donec chan struct{}
} }
func (l muxListener) Accept() (c net.Conn, err error) { func (l muxListener) Accept() (net.Conn, error) {
select { select {
case c = <-l.connc: case c := <-l.connc:
return c, nil return c, nil
case <-l.donec: case <-l.donec:
return nil, ErrListenerClosed return nil, ErrListenerClosed

View File

@ -21,9 +21,7 @@ func Any() Matcher {
// starts with any of the strings in strs. // starts with any of the strings in strs.
func PrefixMatcher(strs ...string) Matcher { func PrefixMatcher(strs ...string) Matcher {
pt := newPatriciaTreeString(strs...) pt := newPatriciaTreeString(strs...)
return func(r io.Reader) bool { return pt.matchPrefix
return pt.matchPrefix(r)
}
} }
var defaultHTTPMethods = []string{ 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 // HTTP2 parses the frame header of the first frame to detect whether the
// connection is an HTTP2 connection. // connection is an HTTP2 connection.
func HTTP2() Matcher { func HTTP2() Matcher {
return func(r io.Reader) bool { return hasHTTP2Preface
return hasHTTP2Preface(r)
}
} }
// HTTP1HeaderField returns a matcher matching the header fields of the first // 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)) b := make([]byte, len(http2Preface))
n, err := r.Read(b) n, err := r.Read(b)
if err != nil { if err != nil {