2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-10 03:31:52 +08:00
cmux/README.md

59 lines
1.8 KiB
Markdown
Raw Normal View History

# cmux: Connection Mux ![Travis Build Status](https://api.travis-ci.org/soheilhy/args.svg?branch=master "Travis Build Status") [![GoDoc](https://godoc.org/github.com/soheilhy/cmux?status.svg)](http://godoc.org/github.com/soheilhy/cmux)
2015-07-30 01:45:57 +08:00
cmux is a generic Go library to multiplex connections based on
2015-07-30 02:53:29 +08:00
their payload. Using cmux, you can serve gRPC, HTTP, and Go RPC
2015-07-30 07:39:19 +08:00
on the same TCP listener to avoid using one port per
2015-07-30 01:45:57 +08:00
protocol.
## How-To
Simply create your main listener, create a cmux for that listener,
and then match connections:
```go
// 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.
2015-07-30 01:45:57 +08:00
// Create your protocol servers.
grpcS := grpc.NewServer()
2015-07-30 05:35:55 +08:00
grpchello.RegisterGreeterServer(grpcs, &server{})
2015-07-30 01:45:57 +08:00
httpS := &http.Server{
2015-07-30 05:35:55 +08:00
Handler: &helloHTTP1Handler{},
2015-07-30 01:45:57 +08:00
}
// Use the muxed listeners for your servers.
go grpcS.Serve(grpcL)
go httpS.Serve(httpL)
2015-07-30 01:45:57 +08:00
// Start serving!
m.Serve()
```
Take a look at [other examples in the GoDoc](http://godoc.org/github.com/soheilhy/cmux/#pkg-examples).
2015-07-30 01:45:57 +08:00
## Docs
* [GoDocs](https://godoc.org/github.com/soheilhy/cmux)
## Performance
2015-07-30 02:53:29 +08:00
There is room for improvment but, since we are only matching
2015-07-30 01:45:57 +08:00
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.