soheilhy/cmux

Name: cmux

Description: Connection multiplexer for GoLang: serve different services on the same port!

Created: 2015-07-29 17:46:44.0

Updated: 2018-01-19 08:26:43.0

Pushed: 2017-12-04 17:53:27.0

Homepage:

Size: 87

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

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:

reate the main listener.
rr := net.Listen("tcp", ":23456")
rr != nil {
log.Fatal(err)


reate a cmux.
 cmux.New(l)

atch connections in order:
irst grpc, then HTTP, and otherwise Go RPC/TCP.
L := m.Match(cmux.HTTP2HeaderField("content-type", "application/grpc"))
L := m.Match(cmux.HTTP1Fast())
L := m.Match(cmux.Any()) // Any means anything that is not yet matched.

reate your protocol servers.
S := grpc.NewServer()
hello.RegisterGreeterServer(grpcs, &server{})

S := &http.Server{
Handler: &helloHTTP1Handler{},


S := rpc.NewServer()
S.Register(&ExampleRPCRcvr{})

se the muxed listeners for your servers.
rpcS.Serve(grpcL)
ttpS.Serve(httpL)
rpcS.Accept(trpcL)

tart serving!
rve()

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

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.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.