2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-09 19:21:52 +08:00
golang多路复用器
Go to file
Johan Brandhorst-Satzkorn e885ea5a5a
Update README.md
gRPC-Go has been waiting for the settings frame for a while now (https://github.com/soheilhy/cmux/issues/64),
and having the old handler in the example is confusing users (https://github.com/soheilhy/cmux/issues/67).
Use the new handler in the example and remove the "Limitations" from the README.
2022-01-10 16:26:07 -05:00
example Adopt go modules 2021-02-05 14:11:34 -05:00
.gitignore Initial commit 2015-07-29 13:49:26 -04:00
.travis.yml Fix golint import path 2018-10-25 10:41:06 -04:00
bench_test.go SetReadDeadline for Matching 2016-09-05 19:56:50 +02:00
buffer.go Reset the sniffing buffer when not needed 2016-09-25 20:56:09 -04:00
cmux_test.go Add TestClose 2021-01-14 18:06:57 -05:00
cmux.go Add mutex around channel close 2021-01-14 18:06:57 -05:00
CONTRIBUTORS update the list of contributors. 2016-09-25 01:03:35 -04:00
doc.go Add package level docs 2016-07-09 14:01:24 -04:00
go.mod Adopt go modules 2021-02-05 14:11:34 -05:00
go.sum Adopt go modules 2021-02-05 14:11:34 -05:00
LICENSE Add the license file 2015-08-01 10:36:36 -04:00
matchers.go Do not write SETTINGS in response to ACKs. 2018-01-29 10:50:01 -05:00
patricia_test.go Fix index out of range in patricia tree 2016-07-15 10:01:37 -07:00
patricia.go Fix index out of range in patricia tree 2016-07-15 10:01:37 -07:00
README.md Update README.md 2022-01-10 16:26:07 -05:00

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.
// cmux.HTTP2MatchHeaderFieldSendSettings ensures we can handle any gRPC client.
grpcL := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings("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.

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.