2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-09-19 18:45:48 +08:00
golang多路复用器
Go to file
Tamir Duberstein 02b84e9be9
Revert "Optimize Patricia tree"
This reverts commit 3bbbe9847675a1300cce193d9efe458b9f0bdd23.

Fixes race conditions in matchers. Impact on benchmarks:

name               old time/op    new time/op    delta
CMuxConnHTTP1-4       664ns ±19%     754ns ±59%     ~            (p=0.796 n=9+10)
CMuxConnHTTP2-4       745ns ±16%     822ns ±26%     ~            (p=0.252 n=9+10)
CMuxConnHTTP1n2-4     930ns ±28%     926ns ±19%     ~           (p=0.912 n=10+10)
CMuxConnHTTP2n1-4    1.18µs ±52%    0.92µs ±14%     ~           (p=0.469 n=10+10)

name               old alloc/op   new alloc/op   delta
CMuxConnHTTP1-4        256B ± 0%      260B ± 0%   +1.56%        (p=0.000 n=10+10)
CMuxConnHTTP2-4        288B ± 0%      288B ± 0%     ~     (all samples are equal)
CMuxConnHTTP1n2-4      288B ± 0%      290B ± 0%   +0.69%        (p=0.000 n=10+10)
CMuxConnHTTP2n1-4      288B ± 0%      292B ± 0%   +1.39%        (p=0.000 n=10+10)

name               old allocs/op  new allocs/op  delta
CMuxConnHTTP1-4        3.00 ± 0%      5.00 ± 0%  +66.67%        (p=0.000 n=10+10)
CMuxConnHTTP2-4        4.00 ± 0%      4.00 ± 0%     ~     (all samples are equal)
CMuxConnHTTP1n2-4      4.00 ± 0%      6.00 ± 0%  +50.00%        (p=0.000 n=10+10)
CMuxConnHTTP2n1-4      4.00 ± 0%      6.00 ± 0%  +50.00%        (p=0.000 n=10+10)
2016-07-12 12:32:15 -04:00
.gitignore Initial commit 2015-07-29 13:49:26 -04:00
.travis.yml Parallelize benchmarks 2016-07-12 12:32:15 -04:00
bench_test.go Parallelize benchmarks 2016-07-12 12:32:15 -04:00
buffer.go Add copyright notice headers on all Go files. 2016-07-09 13:56:02 -04:00
cmux_test.go Add copyright notice headers on all Go files. 2016-07-09 13:56:02 -04:00
cmux.go Add copyright notice headers on all Go files. 2016-07-09 13:56:02 -04:00
CONTRIBUTORS Add the list of contributors 2016-07-09 14:07:27 -04:00
doc.go Add package level docs 2016-07-09 14:01:24 -04:00
example_recursive_test.go Add copyright notice headers on all Go files. 2016-07-09 13:56:02 -04:00
example_test.go Add copyright notice headers on all Go files. 2016-07-09 13:56:02 -04:00
example_tls_test.go Add copyright notice headers on all Go files. 2016-07-09 13:56:02 -04:00
LICENSE Add the license file 2015-08-01 10:36:36 -04:00
matchers.go Add copyright notice headers on all Go files. 2016-07-09 13:56:02 -04:00
patricia_test.go Add copyright notice headers on all Go files. 2016-07-09 13:56:02 -04:00
patricia.go Revert "Optimize Patricia tree" 2016-07-12 12:32:15 -04:00
README.md Add the list of contributors 2016-07-09 14:07:27 -04: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.
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()
s.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.