MCPcopy
hub / github.com/soheilhy/cmux

github.com/soheilhy/cmux @v0.1.5 sqlite

repository ↗ · DeepWiki ↗ · release v0.1.5 ↗
139 symbols 504 edges 11 files 35 documented · 25%
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.

Extension points exported contracts — how you extend this code

CMux (Interface)
CMux is a multiplexer for network connections. [1 implementers]
cmux.go
Matcher (FuncType)
Matcher matches a connection based on its content.
cmux.go
MatchWriter (FuncType)
MatchWriter is a match that can also write response (say to do handshake).
cmux.go
ErrorHandler (FuncType)
ErrorHandler handles an error and returns whether the mux should continue serving the listener.
cmux.go

Core symbols most depended-on inside this repo

Match
called by 34
cmux.go
New
called by 19
cmux.go
Close
called by 15
cmux.go
Serve
called by 12
cmux.go
Read
called by 11
cmux.go
Any
called by 11
matchers.go
HTTP1Fast
called by 10
matchers.go
Error
called by 8
cmux.go

Shape

Function 75
Method 41
Struct 18
FuncType 3
Interface 1
TypeAlias 1

Languages

Go100%

Modules by API surface

cmux.go38 symbols
cmux_test.go34 symbols
matchers.go17 symbols
example/example_test.go12 symbols
patricia.go9 symbols
bench_test.go9 symbols
example/example_recursive_test.go8 symbols
example/example_tls_test.go5 symbols
patricia_test.go4 symbols
buffer.go3 symbols

Dependencies from manifests, versioned

github.com/soheilhy/cmuxv0.0.0-0001010100000 · 1×
golang.org/x/netv0.0.0-2020120216190 · 1×
google.golang.org/genprotov0.0.0-2020120715074 · 1×
google.golang.org/grpcv1.27.0 · 1×

For agents

$ claude mcp add cmux \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact