smux
smux is a socket multiplexer. smux multiplexes one connection with a virtual channel called a stream. It behaves like a very simple HTTP/2 binary framing layer, but it reduces protocol overhead.
smux sends and receives multiple requests and responses in parallel using a single connection. Therefore, our application will be fast and simple.
Usage
smux provides simple server and client.
// smux server
server := smux.Server{
Network: "tcp", // or "unix"
Address: "localhost:3000", // or "sockfile"
Handler: smux.HandlerFunc(func(w io.Writer, r io.Reader) {
io.Copy(ioutil.Discard, r)
fmt.Fprint(w, "Hello, smux client!")
}),
}
server.ListenAndServe()
// smux client
client := smux.Client{
Network: "tcp", // or "unix"
Address: "localhost:3000", // or "sockfile"
}
body, _ := client.Post([]byte("Hello, smux server!"))
fmt.Printf("%s\n", body) // "Hello, smux client!"
And smux provides raw level interface (stream.Write and Read). You can learn from client and server code.
Performance
Benchmark for HTTP, smux, and similar implementation.
Benchmark script is here. It runs on MacBook Pro (15-inch, 2017), CPU 2.8 GHz Intel Core i7, memory 16 GB. Go version is go1.10.2 darwin/amd64.
Currently, xtaci/smux (ssmux) is fast. I am currently speeding up my smux !