2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-09-20 02:55:46 +08:00
golang多路复用器
Go to file
Soheil Hassas Yeganeh d83a667cb2 Add Matchers that can write back on the channel
As reported in issue #22 reports that Java gRPC clients cannot
handshake with cmux'ed gRPC server, since the client does not
immediately send a header with the content-type field. The reason
is that the java client, block on receiving the first SETTING
frame.

Add MatchWriter that can match and write on the connection. Implement
a MatchWriter that writes a SETTING frame once it receives a SETTING
frame.
2016-04-24 14:47:08 -04:00
.gitignore Initial commit 2015-07-29 13:49:26 -04:00
.travis.yml Fix and optimize travis config 2016-04-24 14:10:41 -04:00
bench_test.go Tweak shutdown behaviour 2016-02-23 11:13:16 -05:00
buffer.go Fix a blocking issue in buffer reader 2016-04-24 12:55:13 -04:00
cmux_test.go Merge pull request #23 from soheilhy/bytes-buffer 2016-04-24 14:38:36 -04:00
cmux.go Add Matchers that can write back on the channel 2016-04-24 14:47:08 -04:00
example_recursive_test.go errcheck 2016-02-21 12:02:37 -05:00
example_test.go errcheck 2016-02-21 12:02:37 -05:00
example_tls_test.go errcheck 2016-02-21 12:02:37 -05:00
LICENSE Add the license file 2015-08-01 10:36:36 -04:00
matchers.go Add Matchers that can write back on the channel 2016-04-24 14:47:08 -04:00
patricia_test.go Initial commit 2015-07-29 13:49:26 -04:00
patricia.go Simplify formatting in patricia.go 2016-02-21 10:41:14 -05:00
README.md README: clarify why TLS is limited 2015-12-11 15:22:57 -05:00

cmux: Connection Mux Travis Build Status GoDoc

cmux is a generic Go library to multiplex connections based on their payload. Using cmux, you can serve gRPC, SSH, HTTPS, HTTP, Go RPC, and pretty much any other protocol on the same TCP listener.

How-To

Simply create your main listener, create a cmux for that listener, and then match connections:

// Create the main listener.
l, err := net.Listen("tcp", ":23456")
if err != nil {
	log.Fatal(err)
}

// Create a cmux.
m := cmux.New(l)

// Match connections in order:
// First grpc, then HTTP, and otherwise Go RPC/TCP.
grpcL := m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
httpL := m.Match(cmux.HTTP1Fast())
trpcL := m.Match(cmux.Any()) // Any means anything that is not yet matched.

// Create your protocol servers.
grpcS := grpc.NewServer()
grpchello.RegisterGreeterServer(grpcs, &server{})

httpS := &http.Server{
	Handler: &helloHTTP1Handler{},
}

trpcS := rpc.NewServer()
s.Register(&ExampleRPCRcvr{})

// Use the muxed listeners for your servers.
go grpcS.Serve(grpcL)
go httpS.Serve(httpL)
go trpcS.Accept(trpcL)

// Start serving!
m.Serve()

Take a look at other examples in the GoDoc.

Docs

Performance

There is room for improvment but, since we are only matching the very first bytes of a connection, the performance overheads on long-lived connections (i.e., RPCs and pipelined HTTP streams) is negligible.

TODO(soheil): Add benchmarks.

Limitations

  • TLS: net/http uses a type assertion to identify TLS connections; since cmux's lookahead-implementing connection wraps the underlying TLS connection, this type assertion fails. Because of that, you can serve HTTPS using cmux but http.Request.TLS would not be set in your handlers.

  • Different Protocols on The Same Connection: cmux matches the connection when it's accepted. For example, one connection can be either gRPC or REST, but not both. That is, we assume that a client connection is either used for gRPC or REST.