diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-02-10 12:07:28 +0100 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-02-10 12:07:28 +0100 |
| commit | 46d9a7df9d8b8c714d4767b540c86e6ecd3f44a9 (patch) | |
| tree | 62edcb40dcf346639b38beb945c84e3d377f3b42 | |
| parent | 93c29b500e0343eb7b22c320ce04b106fb119d52 (diff) | |
| download | muxer-46d9a7df9d8b8c714d4767b540c86e6ecd3f44a9.tar.gz muxer-46d9a7df9d8b8c714d4767b540c86e6ecd3f44a9.zip | |
Update README
| -rw-r--r-- | README.md | 151 |
1 files changed, 15 insertions, 136 deletions
@@ -4,136 +4,6 @@ Simple binary protocol with multiplexing support. [](https://pkg.go.dev/github.com/tetsuo/binproto) -## Install - -```sh -go get github.com/tetsuo/binproto -``` - -## Usage - -To start exchanging messages with binproto, simply wrap a `net.Conn` with a `binproto.Conn` instance. - -Then, once connected, use `ReadMessage()` to read incoming messages and `Send()` to send new ones over the network. - -#### Server - -```go -package main - -import ( - "fmt" - "log" - "net" - - "github.com/tetsuo/binproto" -) - -func main() { - s := &server{} - if err := s.serve("tcp", ":4242"); err != nil { - log.Fatal(err) - } -} - -type server struct { - listener net.Listener -} - -// handle manages an individual client connection. -// It reads each message, prints its content, and responds with a simple reply. -func (s *server) handle(conn net.Conn) { - defer conn.Close() - c := binproto.NewConn(conn) - msg := &binproto.Message{} - for { - err := c.ReadMessage(msg) - if err != nil { - fmt.Printf("error: %v", err) - return - } - fmt.Printf("%d %d %s\n", msg.ID, msg.Channel, msg.Data) - - _, err = c.Send(binproto.NewMessage(112, 5, []byte("hello"))) - if err != nil { - log.Fatal(err) - } - } -} - -// serve starts listening for incoming TCP connections. -// For each new connection, it launches handle in a separate goroutine. -func (s *server) serve(network, address string) error { - l, err := net.Listen(network, address) - if err != nil { - return err - } - s.listener = l - - for { - if s.listener == nil { - break - } - c, err := l.Accept() - if err != nil { - continue - } - go s.handle(c) - } - return nil -} - -// close cleanly shuts down the server listener. -func (s *server) close() error { - err := s.listener.Close() - s.listener = nil - return err -} -``` - -#### Client - -```go -package main - -import ( - "fmt" - "log" - - "github.com/tetsuo/binproto" -) - -func main() { - // Connect to the server on localhost:4242 using binproto - c, err := binproto.Dial("tcp", ":4242") - if err != nil { - log.Fatal(err) - } - - // Start a goroutine to read and print incoming messages from the server - go func() { - msg := &binproto.Message{} - for { - err := c.ReadMessage(msg) - if err != nil { - log.Fatal(err) - return - } - - fmt.Printf("%d %d %s\n", msg.ID, msg.Channel, msg.Data) - } - }() - - // Send a message to the server: ID=42, Channel=3, Data="hi" - _, err = c.Send(binproto.NewMessage(42, 3, []byte("hi"))) - if err != nil { - log.Fatal(err) - } - - select {} // Keeps main from exiting immediately -} -``` - ## Message structure Every message is encoded with a 64-bit header: a length prefix followed by a channel ID and type. @@ -148,12 +18,21 @@ Every message is encoded with a 64-bit header: a length prefix followed by a cha * **Channel ID (first 60 bits)**: Identifies the specific channel for the message. * **Channel Type (last 4 bits)**: Specifies the type of data in the message. -## Encryption +## Benchmarks -binproto itself doesn't provide encryption. However, encryption can be added by using Go libraries that act as drop-in replacements for `net`, for example by wrapping your connections with an implementation of the [NOISE protocol](http://www.noiseprotocol.org/). +The benchmarks measure the performance for different payload sizes (64 bytes, 4 KB, and 1 MB). -## Configure buffer size +With the default 4kB buffers, binproto achieves a throughput of approximately 50 GB/s for both writing and reading operations. -binproto operates with a default internal buffer size of 4096 bytes, meaning data is processed as long as it meets or exceeds this buffer size, which is an effective default for many applications. This value can be adjusted to better suit protocols that use larger or smaller data chunks, optimizing performance as needed. - -> 📄 **For more details, see the API documentation at [pkg.go.dev](https://pkg.go.dev/github.com/tetsuo/binproto).** +``` +goos: darwin +goarch: arm64 +pkg: github.com/tetsuo/binproto +cpu: Apple M4 Pro +BenchmarkWriter_64B-12 100000000 12.15 ns/op 5266.44 MB/s 0 B/op 0 allocs/op +BenchmarkWriter_4KB-12 14390127 81.01 ns/op 50564.20 MB/s 0 B/op 0 allocs/op +BenchmarkWriter_1MB-12 75183 15905 ns/op 65925.85 MB/s 14 B/op 0 allocs/op +BenchmarkReader_64B-12 70587026 16.55 ns/op 3868.22 MB/s 0 B/op 0 allocs/op +BenchmarkReader_4KB-12 15426055 78.05 ns/op 52477.75 MB/s 0 B/op 0 allocs/op +BenchmarkReader_1MB-12 38109 31472 ns/op 33317.35 MB/s 27 B/op 0 allocs/op +``` |
