maxogden/multiplex — pull request #14 by jessetane (2014-10-14 21:09:35+00:00)
Title: Cleanup ended streams

I ran into a couple problems while using this module to multiplex a bunch of sub-streams over `websocket-stream`. The first was related to dealing with error or close events: if a server restart or network failure occurred and triggered one of these, I wanted some easy way to clean up all my sub-streams. I figured the right way to do this would be to call `end()` on the multiplexer itself, but this did not end the sub-streams or remove them from the multiplexer's internal lookup table.

The second problem I ran into was that on rare occasions (if the network happened to be slow at the right moment) It was possible for a remote stream to push data to a local stream that had already ended, resulting in "stream.push() after EOF" errors.

I tried to solve these problems by making the multiplex instance end all of its sub-streams if it had ended, and by keeping a reference around to local streams that had ended until an ack (chunk with length 0) came back from the other side.

I also thought removing the `destroyStream(id)` api method in favor of ending sub-streams directly would be nice - that way indicating that you're done with a sub-stream (or the multiplexer itself) would be accomplished in the same fashion: `{substream,multiplex}.end()`.