{"id":16092303,"url":"https://github.com/alecthomas/multiplex","last_synced_at":"2025-03-17T17:30:58.369Z","repository":{"id":13955077,"uuid":"16655271","full_name":"alecthomas/multiplex","owner":"alecthomas","description":"This Go package multiplexes streams over a single underlying transport io.ReadWriteCloser.","archived":false,"fork":false,"pushed_at":"2024-03-02T14:13:57.000Z","size":9,"stargazers_count":25,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-28T02:11:49.882Z","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":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alecthomas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"COPYING","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":"2014-02-08T23:06:37.000Z","updated_at":"2023-08-02T05:10:03.000Z","dependencies_parsed_at":"2024-06-19T01:55:06.081Z","dependency_job_id":null,"html_url":"https://github.com/alecthomas/multiplex","commit_stats":{"total_commits":4,"total_committers":1,"mean_commits":4.0,"dds":0.0,"last_synced_commit":"e565e6fcbcd0f7f3aa83820717e47abd194483a6"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fmultiplex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fmultiplex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fmultiplex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alecthomas%2Fmultiplex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alecthomas","download_url":"https://codeload.github.com/alecthomas/multiplex/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243871911,"owners_count":20361380,"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-10-09T16:06:56.147Z","updated_at":"2025-03-17T17:30:58.039Z","avatar_url":"https://github.com/alecthomas.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Multiplexed streams for Go [![Build Status](https://travis-ci.org/alecthomas/multiplex.png)](https://travis-ci.org/alecthomas/multiplex)\n\nPackage multiplex provides multiplexed streams over a single underlying\ntransport `io.ReadWriteCloser`.\n\nAny system that requires a large number of independent TCP connections could\nbenefit from this package, by instead having each client maintain a single\nmultiplexed connection. There is essentially very little cost to creating new\nchannels, or maintaining a large number of open channels. Ideal for long term\nwaiting.\n\nAn interesting side-effect of this multiplexing is that once the underlying\nconnection has been established, each end of the connection can both `Accept()`\nand `Dial()`. This allows for elegant push notifications and other interesting\napproaches.\n\n\n### Documentation\n\nCan be found on [godoc.org](http://godoc.org/github.com/alecthomas/multiplex) or\nbelow.\n\n### Example Server\n\n    ln, err := net.Listen(\"tcp\", \":1234\")\n    for {\n        conn, err := ln.Accept()\n        go func(conn net.Conn) {\n            mx := multiplex.MultiplexedServer(conn)\n            for {\n                c, err := mx.Accept()\n                go handleConnection(c)\n            }\n        }()\n    }\n\n\n### Example Client\n\nConnect to a server with a single TCP connection, then create 10K channels over\nit and write \"hello\" to each.\n\n    conn, err := net.Dial(\"tcp\", \"127.0.0.1:1234\")\n    mx := multiplex.MultiplexedClient(conn)\n\n    for i := 0; i \u003c 10000; i++ {\n        go func() {\n            c, err := mx.Dial()\n            n, err := c.Write([]byte(\"hello\"))\n            c.Close()\n        }()\n    }\n\n## Usage\n\n```go\nconst (\n\tSYN = 1 \u003c\u003c iota\n\tRST = 1 \u003c\u003c iota\n)\n```\nPacket flags.\n\n```go\nconst (\n\t// FragmentSize (in bytes) of packet fragments.\n\tFragmentSize = 1024\n)\n```\n\n```go\nvar (\n\t// ErrInvalidChannel is returned when an attempt is made to write to an invalid channel.\n\tErrInvalidChannel = errors.New(\"invalid channel\")\n)\n```\n\n#### type Channel\n\n```go\ntype Channel struct {\n}\n```\n\nA Channel managed by the multiplexer.\n\n#### func (*Channel) Close\n\n```go\nfunc (c *Channel) Close() error\n```\nClose a multiplexed channel.\n\n#### func (*Channel) Read\n\n```go\nfunc (c *Channel) Read(b []byte) (int, error)\n```\nRead bytes from a multiplexed channel.\n\n#### func (*Channel) Write\n\n```go\nfunc (c *Channel) Write(b []byte) (int, error)\n```\nWrite bytes to a multiplexed channel. The underlying implementation will\nfragment the payload into FragmentSize chunks to prevent starvation of other\nchannels.\n\n#### type MultiplexedStream\n\n```go\ntype MultiplexedStream struct {\n}\n```\n\n\n#### func  MultiplexedClient\n\n```go\nfunc MultiplexedClient(conn io.ReadWriteCloser) *MultiplexedStream\n```\nMultiplexedClient creates a new multiplexed client-side stream.\n\n#### func  MultiplexedServer\n\n```go\nfunc MultiplexedServer(conn io.ReadWriteCloser) *MultiplexedStream\n```\nMultiplexedServer creates a new multiplexed server-side stream.\n\n#### func (*MultiplexedStream) Accept\n\n```go\nfunc (m *MultiplexedStream) Accept() (*Channel, error)\n```\n\n#### func (*MultiplexedStream) Close\n\n```go\nfunc (m *MultiplexedStream) Close() error\n```\n\n#### func (*MultiplexedStream) Dial\n\n```go\nfunc (m *MultiplexedStream) Dial() (*Channel, error)\n```\nDial the remote end, creating a new multiplexed channel.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Fmultiplex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falecthomas%2Fmultiplex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falecthomas%2Fmultiplex/lists"}