| 1 |
// muxado is an implementation of a general-purpose stream-multiplexing protocol. |
| 2 |
// |
| 3 |
// muxado allows clients applications to multiplex a single stream-oriented connection, |
| 4 |
// like a TCP connection, and communicate over many streams on top of it. muxado accomplishes |
| 5 |
// this by chunking data sent over each stream into frames and then reassembling the |
| 6 |
// frames and buffering the data before being passed up to the application |
| 7 |
// layer on the other side. |
| 8 |
// |
| 9 |
// muxado is very nearly an exact implementation of the HTTP2 framing layer while leaving out all |
| 10 |
// the HTTP-specific parts. It is heavily inspired by HTTP2/SPDY/WebMUX. |
| 11 |
// |
| 12 |
// muxado's documentation uses the following terms consistently for easier communication: |
| 13 |
// - "a transport" is an underlying stream (typically TCP) over which frames are sent between |
| 14 |
// endpoints |
| 15 |
// - "a stream" is any of the full-duplex byte-streams multiplexed over the transport |
| 16 |
// - "a session" refers to an instance of the muxado protocol running over a transport between |
| 17 |
// two endpoints |
| 18 |
// |
| 19 |
// Perhaps the best part of muxado is the interface exposed to client libraries. Since new |
| 20 |
// streams may be initiated by both sides at any time, a muxado.Session implements the net.Listener |
| 21 |
// interface (almost! Go unfortunately doesn't support covariant interface satisfaction so there's |
| 22 |
// a shim). Each muxado stream implements the net.Conn interface. This allows you to integrate |
| 23 |
// muxado into existing code which works with these interfaces (which is most Golang networking code) |
| 24 |
// with very little difficulty. Consider the following toy example. Here we'll initiate a new secure |
| 25 |
// connection to a server, and then ask it which application it wants via an HTTP request over a muxado stream |
| 26 |
// and then serve an entire HTTP application *to the server*. |
| 27 |
// |
| 28 |
// |
| 29 |
// sess, err := muxado.DialTLS("tcp", "example.com:1234", new(tls.Config)) |
| 30 |
// client := &http.Client{Transport: &http.Transport{Dial: sess.NetDial}} |
| 31 |
// resp, err := client.Get("http://example.com/appchoice") |
| 32 |
// switch getChoice(resp.Body) { |
| 33 |
// case "foo": |
| 34 |
// http.Serve(sess.NetListener(), fooHandler) |
| 35 |
// case "bar": |
| 36 |
// http.Serve(sess.NetListener(), barHandler) |
| 37 |
// } |
| 38 |
// |
| 39 |
// |
| 40 |
// In addition to enabling multiple streams over a single connection, muxado enables other |
| 41 |
// behaviors which can be useful to the application layer: |
| 42 |
// - Both sides of a muxado session may initiate new streams |
| 43 |
// - muxado can transparently run application-level heartbeats and timeout dead sessions |
| 44 |
// - When connections fail, muxado indicates to the application which streams may be safely retried |
| 45 |
// - muxado supports prioritizing streams to maximize useful throughput when bandwidth-constrained |
| 46 |
// |
| 47 |
// A few examples of what these capabilities might make muxado useful for: |
| 48 |
// - eliminating custom async/pipeling code for your protocols |
| 49 |
// - eliminating connection pools in your protocols |
| 50 |
// - eliminating custom NAT traversal logic for enabling server-initiated streams |
| 51 |
// |
| 52 |
// muxado has been tuned to be very performant within the limits of what you can expect of pure-Go code. |
| 53 |
// Some of muxado's code looks unidiomatic in the quest for better performance. (Locks over channels, never allocating |
| 54 |
// from the heap, etc). muxado will typically outperform TCP connections when rapidly initiating many new |
| 55 |
// streams with small payloads. When sending a large payload over a single stream, muxado's worst case, it can |
| 56 |
// be 2-3x slower and does not parallelize well. |
| 57 |
package muxado |