Categorygithub.com/soheilhy/cmux
modulepackage
0.1.5
Repository: https://github.com/soheilhy/cmux.git
Documentation: pkg.go.dev

# README

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()
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/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.

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

# Functions

Any is a Matcher that matches any connection.
HTTP1 parses the first line or upto 4096 bytes of the request to see if the conection contains an HTTP request.
HTTP1Fast only matches the methods in the HTTP request.
HTTP1HeaderField returns a matcher matching the header fields of the first request of an HTTP 1 connection.
HTTP1HeaderFieldPrefix returns a matcher matching the header fields of the first request of an HTTP 1 connection.
HTTP2 parses the frame header of the first frame to detect whether the connection is an HTTP2 connection.
HTTP2HeaderField returns a matcher matching the header fields of the first headers frame.
HTTP2HeaderFieldPrefix returns a matcher matching the header fields of the first headers frame.
HTTP2MatchHeaderFieldPrefixSendSettings matches the header field prefix and writes the settings to the server.
HTTP2MatchHeaderFieldSendSettings matches the header field and writes the settings to the server.
New instantiates a new connection multiplexer.
PrefixMatcher returns a matcher that matches a connection if it starts with any of the strings in strs.
TLS matches HTTPS requests.

# Variables

ErrListenerClosed is returned from muxListener.Accept when the underlying listener is closed.
ErrServerClosed is returned from muxListener.Accept when mux server is closed.

# Structs

ErrNotMatched is returned whenever a connection is not matched by any of the matchers registered in the multiplexer.
MuxConn wraps a net.Conn and provides transparent sniffing of connection data.

# Interfaces

CMux is a multiplexer for network connections.

# Type aliases

ErrorHandler handles an error and returns whether the mux should continue serving the listener.
Matcher matches a connection based on its content.
MatchWriter is a match that can also write response (say to do handshake).