{"id":13563499,"url":"https://github.com/soheilhy/cmux","last_synced_at":"2025-05-13T00:26:18.455Z","repository":{"id":35633553,"uuid":"39907649","full_name":"soheilhy/cmux","owner":"soheilhy","description":"Connection multiplexer for GoLang: serve different services on the same port!","archived":false,"fork":false,"pushed_at":"2023-12-19T09:16:26.000Z","size":105,"stargazers_count":2625,"open_issues_count":34,"forks_count":203,"subscribers_count":40,"default_branch":"master","last_synced_at":"2025-04-22T10:02:53.142Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soheilhy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-07-29T17:46:44.000Z","updated_at":"2025-04-21T02:37:06.000Z","dependencies_parsed_at":"2024-06-18T10:58:34.444Z","dependency_job_id":"d3d25155-7ae0-4f29-8bb2-c0fa4c51005e","html_url":"https://github.com/soheilhy/cmux","commit_stats":{"total_commits":96,"total_committers":17,"mean_commits":5.647058823529412,"dds":0.6979166666666667,"last_synced_commit":"5ec6847320e53b5fee0ab9a4757b56625a946c85"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soheilhy%2Fcmux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soheilhy%2Fcmux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soheilhy%2Fcmux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soheilhy%2Fcmux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soheilhy","download_url":"https://codeload.github.com/soheilhy/cmux/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250493228,"owners_count":21439710,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T13:01:19.985Z","updated_at":"2025-04-23T18:41:44.894Z","avatar_url":"https://github.com/soheilhy.png","language":"Go","funding_links":[],"categories":["开源类库","Misc","Go","Open source library","Language-Specific"],"sub_categories":["网络","The Internet","Go"],"readme":"# cmux: Connection Mux ![Travis Build Status](https://api.travis-ci.org/soheilhy/args.svg?branch=master \"Travis Build Status\") [![GoDoc](https://godoc.org/github.com/soheilhy/cmux?status.svg)](http://godoc.org/github.com/soheilhy/cmux)\n\ncmux is a generic Go library to multiplex connections based on\ntheir payload. Using cmux, you can serve gRPC, SSH, HTTPS, HTTP,\nGo RPC, and pretty much any other protocol on the same TCP listener.\n\n## How-To\nSimply create your main listener, create a cmux for that listener,\nand then match connections:\n```go\n// Create the main listener.\nl, err := net.Listen(\"tcp\", \":23456\")\nif err != nil {\n\tlog.Fatal(err)\n}\n\n// Create a cmux.\nm := cmux.New(l)\n\n// Match connections in order:\n// First grpc, then HTTP, and otherwise Go RPC/TCP.\ngrpcL := m.Match(cmux.HTTP2HeaderField(\"content-type\", \"application/grpc\"))\nhttpL := m.Match(cmux.HTTP1Fast())\ntrpcL := m.Match(cmux.Any()) // Any means anything that is not yet matched.\n\n// Create your protocol servers.\ngrpcS := grpc.NewServer()\ngrpchello.RegisterGreeterServer(grpcS, \u0026server{})\n\nhttpS := \u0026http.Server{\n\tHandler: \u0026helloHTTP1Handler{},\n}\n\ntrpcS := rpc.NewServer()\ntrpcS.Register(\u0026ExampleRPCRcvr{})\n\n// Use the muxed listeners for your servers.\ngo grpcS.Serve(grpcL)\ngo httpS.Serve(httpL)\ngo trpcS.Accept(trpcL)\n\n// Start serving!\nm.Serve()\n```\n\nTake a look at [other examples in the GoDoc](http://godoc.org/github.com/soheilhy/cmux/#pkg-examples).\n\n## Docs\n* [GoDocs](https://godoc.org/github.com/soheilhy/cmux)\n\n## Performance\nThere is room for improvment but, since we are only matching\nthe very first bytes of a connection, the performance overheads on\nlong-lived connections (i.e., RPCs and pipelined HTTP streams)\nis negligible.\n\n*TODO(soheil)*: Add benchmarks.\n\n## Limitations\n* *TLS*: `net/http` uses a type assertion to identify TLS connections; since\ncmux's lookahead-implementing connection wraps the underlying TLS connection,\nthis type assertion fails.\nBecause of that, you can serve HTTPS using cmux but `http.Request.TLS`\nwould not be set in your handlers.\n\n* *Different Protocols on The Same Connection*: `cmux` matches the connection\nwhen it's accepted. For example, one connection can be either gRPC or REST, but\nnot both. That is, we assume that a client connection is either used for gRPC\nor REST.\n\n* *Java gRPC Clients*: Java gRPC client blocks until it receives a SETTINGS\nframe from the server. If you are using the Java client to connect to a cmux'ed\ngRPC server please match with writers:\n```go\ngrpcl := m.MatchWithWriters(cmux.HTTP2MatchHeaderFieldSendSettings(\"content-type\", \"application/grpc\"))\n```\n\n# Copyright and License\nCopyright 2016 The CMux Authors. All rights reserved.\n\nSee [CONTRIBUTORS](https://github.com/soheilhy/cmux/blob/master/CONTRIBUTORS)\nfor the CMux Authors. Code is released under\n[the Apache 2 license](https://github.com/soheilhy/cmux/blob/master/LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoheilhy%2Fcmux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoheilhy%2Fcmux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoheilhy%2Fcmux/lists"}