cockroachdb/cmux

Name: cmux

Owner: CockroachDB

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

Forked from: soheilhy/cmux

Created: 2016-02-28 18:40:05.0

Updated: 2018-05-11 07:27:30.0

Pushed: 2017-01-10 19:26:08.0

Homepage:

Size: 72

Language: Go

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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:

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()
gister(&ExampleRPCRcvr{})

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

tart serving!
rve()

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

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.