indutny/node-spdy — pull request #82 by molnarg (2013-03-09 19:08:35+00:00)
Title: Make scheduler wait for drain if connection.write() returns false.

This should enforce stream priorities better than the original scheduler in some cases.

I'll try to explain this. The current scheduler collects chunks for a short time, and then writes them to the connection stream starting with the highest priority chunk. This time frame is very short, because the scheduler uses `process.nextTick()`, so the time frame is exactly one JavaScript 'turn', or 'tick'. This makes the scheduler almost useless if the incoming chunk are coming asynchronously (imagine two streams with different priorities emitting data asynchronously: the higher priority stream won't have higher bandwidth allocated).

This patch makes the scheduler behave exactly the same until there's no congestion (there's enough bandwidth for every stream), but when there is, then it uses a different strategy. When the connection stream signals that its buffer is full (`connection.write()` returns `false`) then we don't write any more until the there's free buffer space in the connection stream. In this time frame, there may be high or low priority incoming chunks, those are queued up. When the connection stream is ready again, the we write the higher priority chunks first etc. until we can.

Unfortunately, this is not completely stable yet. I am building a forwarding proxy (spdy on one side, http on the other) using node-spdy, and I use that for testing. I tested the patch with the latest Chrome, and the connection sometimes hangs, but could not figure out why. I am not very familiar with the node-spdy code base, so if someone could take a look at this that would be great. I would be glad to improve this patch further if you find problems.