You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
clair/vendor/github.com/cockroachdb/cmux
Sida Chen 1533dd1d51
vendor: updated vendor dir for grpc v2 api
7 years ago
..
.gitignore vendor: updated vendor dir for grpc v2 api 7 years ago
.travis.yml vendor: updated vendor dir for grpc v2 api 7 years ago
CONTRIBUTORS vendor: updated vendor dir for grpc v2 api 7 years ago
LICENSE vendor: updated vendor dir for grpc v2 api 7 years ago
README.md vendor: updated vendor dir for grpc v2 api 7 years ago
bench_test.go vendor: updated vendor dir for grpc v2 api 7 years ago
buffer.go vendor: updated vendor dir for grpc v2 api 7 years ago
cmux.go vendor: updated vendor dir for grpc v2 api 7 years ago
cmux_test.go vendor: updated vendor dir for grpc v2 api 7 years ago
doc.go vendor: updated vendor dir for grpc v2 api 7 years ago
example_recursive_test.go vendor: updated vendor dir for grpc v2 api 7 years ago
example_test.go vendor: updated vendor dir for grpc v2 api 7 years ago
example_tls_test.go vendor: updated vendor dir for grpc v2 api 7 years ago
matchers.go vendor: updated vendor dir for grpc v2 api 7 years ago
patricia.go vendor: updated vendor dir for grpc v2 api 7 years ago
patricia_test.go vendor: updated vendor dir for grpc v2 api 7 years ago

README.md

cmux: Connection Mux 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()

There are more examples on GoDoc.

Performance

Since we are only matching the very first bytes of a connection, the performance overhead on long-lived connections (i.e., RPCs and pipelined HTTP streams) is negligible.

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. This means you can serve HTTPS using cmux but http.Request.TLS will not be set in your handlers. If you are able to wrap TLS around cmux, you can work around this limitation. See https://github.com/cockroachdb/cockroach/commit/83caba2 for an example of this approach.

  • 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.

Copyright and License

Copyright 2016 The CMux Authors. All rights reserved.

See CONTRIBUTORS for the CMux Authors. Code is released under the Apache 2 license.