2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-14 11:31:28 +08:00
go/src/github.com/soheilhy/cmux/cmux.go:127:13	c.Close()
go/src/github.com/soheilhy/cmux/cmux.go:134:9	c.Close()
go/src/github.com/soheilhy/cmux/cmux.go:137:15	m.root.Close()
go/src/github.com/soheilhy/cmux/cmux_test.go:43:9	s.Serve(l)
go/src/github.com/soheilhy/cmux/cmux_test.go:52:20	defer r.Body.Close()
go/src/github.com/soheilhy/cmux/cmux_test.go:72:12	s.Register(TestRPCRcvr{})
go/src/github.com/soheilhy/cmux/cmux_test.go:103:15	defer l.Close()
go/src/github.com/soheilhy/cmux/cmux_test.go:109:15	go muxl.Serve()
go/src/github.com/soheilhy/cmux/cmux_test.go:116:20	defer r.Body.Close()
go/src/github.com/soheilhy/cmux/cmux_test.go:125:15	defer l.Close()
go/src/github.com/soheilhy/cmux/cmux_test.go:133:15	go muxl.Serve()
go/src/github.com/soheilhy/cmux/cmux_test.go:141:15	defer l.Close()
go/src/github.com/soheilhy/cmux/cmux_test.go:147:15	go muxl.Serve()
go/src/github.com/soheilhy/cmux/example_recursive_test.go:27:9	s.Serve(l)
go/src/github.com/soheilhy/cmux/example_recursive_test.go:56:12	s.Register(&RecursiveRPCRcvr{})
go/src/github.com/soheilhy/cmux/example_recursive_test.go:88:15	go tlsm.Serve()
go/src/github.com/soheilhy/cmux/example_recursive_test.go:89:12	tcpm.Serve()
go/src/github.com/soheilhy/cmux/example_test.go:30:9	s.Serve(l)
go/src/github.com/soheilhy/cmux/example_test.go:34:9	io.Copy(ws, ws)
go/src/github.com/soheilhy/cmux/example_test.go:41:9	s.Serve(l)
go/src/github.com/soheilhy/cmux/example_test.go:53:12	s.Register(&ExampleRPCRcvr{})
go/src/github.com/soheilhy/cmux/example_test.go:68:13	grpcs.Serve(l)
go/src/github.com/soheilhy/cmux/example_test.go:97:9	m.Serve()
go/src/github.com/soheilhy/cmux/example_tls_test.go:24:9	s.Serve(l)
go/src/github.com/soheilhy/cmux/example_tls_test.go:69:9	m.Serve()
go/src/github.com/soheilhy/cmux/matchers.go:151:14	hdec.Write(f.HeaderBlockFragment())
This commit is contained in:
Tamir Duberstein 2016-01-07 10:15:03 -05:00 committed by Soheil Hassas Yeganeh
parent ec462e9c0e
commit 92a63c4fce
7 changed files with 127 additions and 37 deletions

View File

@ -6,7 +6,10 @@ go:
before_install: before_install:
- go get -u github.com/golang/lint/golint - go get -u github.com/golang/lint/golint
- if [[ $TRAVIS_GO_VERSION == 1.5* ]]; then go get -u github.com/kisielk/errcheck; fi
before_script: before_script:
- '! gofmt -s -l . | read' - '! gofmt -s -l . | read'
- golint ./... - golint ./...
- echo $TRAVIS_GO_VERSION
- if [[ $TRAVIS_GO_VERSION == 1.5* ]]; then errcheck ./...; fi

View File

@ -117,17 +117,17 @@ func (m *cMux) serve(c net.Conn) {
select { select {
case sl.l.connc <- muc: case sl.l.connc <- muc:
case <-sl.l.donec: case <-sl.l.donec:
c.Close() _ = c.Close()
} }
return return
} }
} }
} }
c.Close() _ = c.Close()
err := ErrNotMatched{c: c} err := ErrNotMatched{c: c}
if !m.handleErr(err) { if !m.handleErr(err) {
m.root.Close() _ = m.root.Close()
} }
} }

View File

@ -36,11 +36,13 @@ func (h *testHTTP1Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, testHTTP1Resp) fmt.Fprintf(w, testHTTP1Resp)
} }
func runTestHTTPServer(l net.Listener) { func runTestHTTPServer(t *testing.T, l net.Listener) {
s := &http.Server{ s := &http.Server{
Handler: &testHTTP1Handler{}, Handler: &testHTTP1Handler{},
} }
s.Serve(l) if err := s.Serve(l); err != nil {
t.Log(err)
}
} }
func runTestHTTP1Client(t *testing.T, addr string) { func runTestHTTP1Client(t *testing.T, addr string) {
@ -49,7 +51,11 @@ func runTestHTTP1Client(t *testing.T, addr string) {
t.Fatal(err) t.Fatal(err)
} }
defer r.Body.Close() defer func() {
if err := r.Body.Close(); err != nil {
t.Log(err)
}
}()
b, err := ioutil.ReadAll(r.Body) b, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
t.Error(err) t.Error(err)
@ -67,16 +73,18 @@ func (r TestRPCRcvr) Test(i int, j *int) error {
return nil return nil
} }
func runTestRPCServer(l net.Listener) { func runTestRPCServer(t *testing.T, l net.Listener) {
s := rpc.NewServer() s := rpc.NewServer()
s.Register(TestRPCRcvr{}) if err := s.Register(TestRPCRcvr{}); err != nil {
t.Fatal(err)
}
for { for {
c, err := l.Accept() c, err := l.Accept()
if err != nil { if err != nil {
t.Log(err)
return return
} }
s.ServeConn(c) go s.ServeConn(c)
} }
} }
@ -100,21 +108,36 @@ func runTestRPCClient(t *testing.T, addr string) {
func TestAny(t *testing.T) { func TestAny(t *testing.T) {
l, addr := testListener(t) l, addr := testListener(t)
defer l.Close() defer func() {
if err := l.Close(); err != nil {
t.Log(err)
}
}()
muxl := New(l) muxl := New(l)
httpl := muxl.Match(Any()) httpl := muxl.Match(Any())
go runTestHTTPServer(httpl) go runTestHTTPServer(t, httpl)
go muxl.Serve() go func() {
if err := muxl.Serve(); err != nil {
t.Log(err)
}
}()
r, err := http.Get("http://" + addr) r, err := http.Get("http://" + addr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer r.Body.Close() defer func() {
if err := r.Body.Close(); err != nil {
t.Log(err)
}
}()
b, err := ioutil.ReadAll(r.Body) b, err := ioutil.ReadAll(r.Body)
if err != nil {
t.Error(err)
}
if string(b) != testHTTP1Resp { if string(b) != testHTTP1Resp {
t.Errorf("invalid response: want=%s got=%s", testHTTP1Resp, b) t.Errorf("invalid response: want=%s got=%s", testHTTP1Resp, b)
} }
@ -122,15 +145,23 @@ func TestAny(t *testing.T) {
func TestHTTPGoRPC(t *testing.T) { func TestHTTPGoRPC(t *testing.T) {
l, addr := testListener(t) l, addr := testListener(t)
defer l.Close() defer func() {
if err := l.Close(); err != nil {
t.Log(err)
}
}()
muxl := New(l) muxl := New(l)
httpl := muxl.Match(HTTP2(), HTTP1Fast()) httpl := muxl.Match(HTTP2(), HTTP1Fast())
rpcl := muxl.Match(Any()) rpcl := muxl.Match(Any())
go runTestHTTPServer(httpl) go runTestHTTPServer(t, httpl)
go runTestRPCServer(rpcl) go runTestRPCServer(t, rpcl)
go muxl.Serve() go func() {
if err := muxl.Serve(); err != nil {
t.Log(err)
}
}()
runTestHTTP1Client(t, addr) runTestHTTP1Client(t, addr)
runTestRPCClient(t, addr) runTestRPCClient(t, addr)
@ -138,13 +169,21 @@ func TestHTTPGoRPC(t *testing.T) {
func TestErrorHandler(t *testing.T) { func TestErrorHandler(t *testing.T) {
l, addr := testListener(t) l, addr := testListener(t)
defer l.Close() defer func() {
if err := l.Close(); err != nil {
t.Log(err)
}
}()
muxl := New(l) muxl := New(l)
httpl := muxl.Match(HTTP2(), HTTP1Fast()) httpl := muxl.Match(HTTP2(), HTTP1Fast())
go runTestHTTPServer(httpl) go runTestHTTPServer(t, httpl)
go muxl.Serve() go func() {
if err := muxl.Serve(); err != nil {
t.Log(err)
}
}()
firstErr := true firstErr := true
muxl.HandleError(func(err error) bool { muxl.HandleError(func(err error) bool {

View File

@ -8,6 +8,7 @@ import (
"net" "net"
"net/http" "net/http"
"net/rpc" "net/rpc"
"strings"
"github.com/soheilhy/cmux" "github.com/soheilhy/cmux"
) )
@ -24,7 +25,9 @@ func recursiveServeHTTP(l net.Listener) {
s := &http.Server{ s := &http.Server{
Handler: &recursiveHTTPHandler{}, Handler: &recursiveHTTPHandler{},
} }
s.Serve(l) if err := s.Serve(l); err != cmux.ErrListenerClosed {
panic(err)
}
} }
func tlsListener(l net.Listener) net.Listener { func tlsListener(l net.Listener) net.Listener {
@ -53,8 +56,19 @@ func (r *RecursiveRPCRcvr) Cube(i int, j *int) error {
func recursiveServeRPC(l net.Listener) { func recursiveServeRPC(l net.Listener) {
s := rpc.NewServer() s := rpc.NewServer()
s.Register(&RecursiveRPCRcvr{}) if err := s.Register(&RecursiveRPCRcvr{}); err != nil {
s.Accept(l) panic(err)
}
for {
conn, err := l.Accept()
if err != nil {
if err != cmux.ErrListenerClosed {
panic(err)
}
return
}
go s.ServeConn(conn)
}
} }
// This is an example for serving HTTP, HTTPS, and GoRPC/TLS on the same port. // This is an example for serving HTTP, HTTPS, and GoRPC/TLS on the same port.
@ -80,11 +94,16 @@ func Example_recursiveCmux() {
tlsm := cmux.New(tlsl) tlsm := cmux.New(tlsl)
httpsl := tlsm.Match(cmux.HTTP1Fast()) httpsl := tlsm.Match(cmux.HTTP1Fast())
gorpcl := tlsm.Match(cmux.Any()) gorpcl := tlsm.Match(cmux.Any())
go recursiveServeHTTP(httpl) go recursiveServeHTTP(httpl)
go recursiveServeHTTP(httpsl) go recursiveServeHTTP(httpsl)
go recursiveServeRPC(gorpcl) go recursiveServeRPC(gorpcl)
go tlsm.Serve() go func() {
tcpm.Serve() if err := tlsm.Serve(); err != cmux.ErrListenerClosed {
panic(err)
}
}()
if err := tcpm.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
panic(err)
}
} }

View File

@ -7,6 +7,7 @@ import (
"net" "net"
"net/http" "net/http"
"net/rpc" "net/rpc"
"strings"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -27,18 +28,24 @@ func serveHTTP(l net.Listener) {
s := &http.Server{ s := &http.Server{
Handler: &exampleHTTPHandler{}, Handler: &exampleHTTPHandler{},
} }
s.Serve(l) if err := s.Serve(l); err != cmux.ErrListenerClosed {
panic(err)
}
} }
func EchoServer(ws *websocket.Conn) { func EchoServer(ws *websocket.Conn) {
io.Copy(ws, ws) if _, err := io.Copy(ws, ws); err != nil {
panic(err)
}
} }
func serveWS(l net.Listener) { func serveWS(l net.Listener) {
s := &http.Server{ s := &http.Server{
Handler: websocket.Handler(EchoServer), Handler: websocket.Handler(EchoServer),
} }
s.Serve(l) if err := s.Serve(l); err != cmux.ErrListenerClosed {
panic(err)
}
} }
type ExampleRPCRcvr struct{} type ExampleRPCRcvr struct{}
@ -50,8 +57,19 @@ func (r *ExampleRPCRcvr) Cube(i int, j *int) error {
func serveRPC(l net.Listener) { func serveRPC(l net.Listener) {
s := rpc.NewServer() s := rpc.NewServer()
s.Register(&ExampleRPCRcvr{}) if err := s.Register(&ExampleRPCRcvr{}); err != nil {
s.Accept(l) panic(err)
}
for {
conn, err := l.Accept()
if err != nil {
if err != cmux.ErrListenerClosed {
panic(err)
}
return
}
go s.ServeConn(conn)
}
} }
type grpcServer struct{} type grpcServer struct{}
@ -65,7 +83,9 @@ func (s *grpcServer) SayHello(ctx context.Context, in *grpchello.HelloRequest) (
func serveGRPC(l net.Listener) { func serveGRPC(l net.Listener) {
grpcs := grpc.NewServer() grpcs := grpc.NewServer()
grpchello.RegisterGreeterServer(grpcs, &grpcServer{}) grpchello.RegisterGreeterServer(grpcs, &grpcServer{})
grpcs.Serve(l) if err := grpcs.Serve(l); err != cmux.ErrListenerClosed {
panic(err)
}
} }
func Example() { func Example() {
@ -94,5 +114,7 @@ func Example() {
go serveHTTP(httpl) go serveHTTP(httpl)
go serveRPC(rpcl) go serveRPC(rpcl)
m.Serve() if err := m.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
panic(err)
}
} }

View File

@ -7,6 +7,7 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"strings"
"github.com/soheilhy/cmux" "github.com/soheilhy/cmux"
) )
@ -21,7 +22,9 @@ func serveHTTP1(l net.Listener) {
s := &http.Server{ s := &http.Server{
Handler: &anotherHTTPHandler{}, Handler: &anotherHTTPHandler{},
} }
s.Serve(l) if err := s.Serve(l); err != cmux.ErrListenerClosed {
panic(err)
}
} }
func serveHTTPS(l net.Listener) { func serveHTTPS(l net.Listener) {
@ -66,5 +69,7 @@ func Example_bothHTTPAndHTTPS() {
go serveHTTP1(httpl) go serveHTTP1(httpl)
go serveHTTPS(tlsl) go serveHTTPS(tlsl)
m.Serve() if err := m.Serve(); !strings.Contains(err.Error(), "use of closed network connection") {
panic(err)
}
} }

View File

@ -144,7 +144,9 @@ func matchHTTP2Field(r io.Reader, name, value string) (matched bool) {
switch f := f.(type) { switch f := f.(type) {
case *http2.HeadersFrame: case *http2.HeadersFrame:
hdec.Write(f.HeaderBlockFragment()) if _, err := hdec.Write(f.HeaderBlockFragment()); err != nil {
return false
}
if matched { if matched {
return true return true
} }