dmcgowan / streams

Go interface for using multiplexed transports
MIT License
14 stars 2 forks source link

proposal: multiplexer abstraction #1

Open stevvooe opened 9 years ago

stevvooe commented 9 years ago

Originally proposed in https://github.com/docker/libchan/pull/93#issuecomment-75811135, the following is a possible multiplexing interface that would afford some flexibility:

func NewMultiplexer(io.ReadWriter) (Multiplexer, error)

type Multiplexer interface {
    // Channel returns a channel associated with the id, allocating 
    // the channel if it does not yet exist.
    Channel(id int) (io.ReadWriteCloser, error)
}

Then, streams, encoders and decoders pull channels off the multiplexed stream. The base NewMultiplexer can be a libchan specific framing protocol but other implementations could provide Multiplexers.

Optionally, io.ReadWriteCloser could be a net.Conn, if that makes more sense.

dmcgowan commented 9 years ago

Would be interesting to use this interface to expose the Simple Framer. The idea behind the simple framer is that it is the minimum protocol to implement multiplexing for this interface. The only additional concept missing is headers, which are needed to build an application on top of the multiplexer.

Have not started drafting it too much but @mcollina and I discussed having something on the Go and JS side so it could easily be used over websockets in the browser.

mcollina commented 9 years ago

Basically what I am using in jschan is a stream containing msgpack5 messages. The format of those messages is very simple, the channel/bytestream id, the data payload (what should be sent), and an optional parent field for creating new channels.

You can see it here: https://github.com/GraftJS/jschan/blob/master/lib/stream/channels.js#L21-L34