2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-09-19 10:35:47 +08:00
golang多路复用器
Go to file
Soheil Hassas Yeganeh 6194168d4e Remove the bugos http2.Configure from tests
http2.Configure is not functional with cmux. Asked for
exporting http2.Server.HandleConn().
2015-07-29 17:23:53 -04:00
.gitignore Initial commit 2015-07-29 13:49:26 -04:00
.travis.yml Initial commit 2015-07-29 13:49:26 -04:00
cmux_test.go Remove the bugos http2.Configure from tests 2015-07-29 17:23:53 -04:00
cmux.go Initial commit 2015-07-29 13:49:26 -04:00
example_test.go Remove the bugos http2.Configure from tests 2015-07-29 17:23:53 -04:00
matchers.go Implement the matcher HTTP1 fields 2015-07-29 14:49:37 -04:00
patricia_test.go Initial commit 2015-07-29 13:49:26 -04:00
patricia.go Initial commit 2015-07-29 13:49:26 -04:00
README.md Nit in README 2015-07-29 14:53:29 -04: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, HTTP, and Go RPC on the same TCP listener to avoid having to use one port per protocol.

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.
grpcL := m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
httpL := m.Match(cmux.Any()) // Any means anything that is not yet matched.

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

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

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

// 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: Since cmux sits in between the actual listener and the mux'ed listeners, TLS handshake is not handled inside the actual servers. Because of that, when you handle HTTPS using cmux http.Request.TLS would not be set.