Sometimes, certain messages are more important than others. The drr package provides a generic implementation of Deficit Round Robin scheduler for Go channels. Through this package, developer can merge multiple input channels into a single output one by enforcing different input rates.
Let's assume you have one single worker goroutine that must handle all the incoming requests. Let's also assume that there are two sources of those requests implemented through a couple of channels. Channel In_1 carries the requests with higher priority P1, while channel In_2 carries the requests with lower priority P2.
What we observe from channel Out is that input flows In_1 and In_2 share the output's capacity according to their priorities. That is, flow In_1 takes fraction of output capacity. While flow In_2 uses the remaining fraction,
.
DRR scheduling algorithm does not take into account empty flows (i.e. those that do not have anything to transmit). Therefore, the output capacity is shared among all the non-empty input flows.
Documentation can be found here.
import (
"context"
"fmt"
"github.com/bigmikes/drr"
)
func sourceRequests(s string) <-chan interface{} {
inChan := make(chan interface{}, 5)
go func() {
defer close(inChan)
for i := 0; i < 5; i++ {
inChan <- s
}
}()
return inChan
}
func main() {
// Set output channel and create DRR scheduler.
outputChan := make(chan interface{}, 5)
drr, err := drr.NewDRR(outputChan)
if err != nil {
panic(err)
}
// Register two input channels with priority 3 and 2 respectively.
sourceChan1 := sourceRequests("req1")
drr.Input(3, sourceChan1)
sourceChan2 := sourceRequests("req2")
drr.Input(2, sourceChan2)
// Start DRR
drr.Start(context.Background())
// Consume values from output channels.
// Expected rates are 3/5 for channel with priority 3
// and 2/5 for channel with priority 2.
for out := range outputChan {
fmt.Println(out)
}
}
// Output:
// req1
// req1
// req1
// req2
// req2
// req1
// req1
// req2
// req2
// req2
The drr package is licensed under the MIT License. Please see the LICENSE file for details.
This package surely needs your help and feedbacks. You are welcome to open a new issue here on GitHub.