So I need to mux data over a single connection and wanted to prevent one flow from hogging the link. The weighted fair queue algorithm seems like a good solution.
I've implemented the algorithm in this package:
I welcome comments and suggestions.
One potential comment I'll address up front.
I chose not to use channels for two reasons:
1. Entry into the queue cannot be assessed pre-consumption when using channels.
2. While go routines are cheep, they are not free. The stack cost of 8K per goroutine adds up when you get in the 10's/100's of thousands of goroutines.
I find it easy to reason about conditionals so that's what I chose to use. I'm not opposed to channel based implementation suggestions as long as those suggestions would not lead to a loss of performance or an increase increase in complexity.
My primary concern is making sure that the queue actually behaves like a weighted fair queue. I'm fairly certain that it does in the case where all flows have the same weight. But, the one test that uses mixed weights only goes so far as to test that the higher priority flow finishes first. I'm also not entirely happy with TestMultiFlow because there is an element of probability to it seems unavoidable.
-Tad