The library already compiled and passed tests, but tooling and dependencies were stale. This brings everything up to current standards without changing public behavior. Library (go 1.23.0): - cmux_test.go: fix 8 `go vet` failures by sending writer-goroutine errors to the existing errCh (now buffered) instead of t.Fatal; ioutil.ReadAll -> io.ReadAll - matchers.go: io/ioutil -> io (io.Discard); gofmt -s doc comment - go.mod: go 1.11 -> 1.23.0; golang.org/x/net -> v0.42.0 (newest release that still permits a 1.23 directive) Example submodule: - golang.org/x/net/context -> stdlib context - grpc v1.27.0 -> v1.80, modern protobuf; helloworld now comes from the separate google.golang.org/grpc/examples module (requires go 1.25, isolated to this demo-only module) CI: - remove ancient .travis.yml (Go 1.6-1.8) - add .github/workflows/ci.yml: gofmt/vet/build/test -race for the library (Go 1.23 + 1.24 matrix) and the example Temporary() methods and TLS version defaults are intentionally unchanged: net.Error still requires Temporary(), and the TLS defaults are public-API behavior. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
cmux: Connection Mux

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()
trpcS.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/httpuses 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 buthttp.Request.TLSwould not be set in your handlers. -
Different Protocols on The Same Connection:
cmuxmatches 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. -
Java gRPC Clients: Java gRPC client blocks until it receives a SETTINGS frame from the server. If you are using the Java client to connect to a cmux'ed gRPC server please match with writers:
grpcl := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("content-type", "application/grpc"))
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.