Skip to content

gmichelo/drr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

37 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Deficit Round Robin channels scheduler

GoDoc License FOSSA Status Build Status Coverage Status Go Report Card

Introduction

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.

Quick overview on DRR theory

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 P1/(P1+P2) fraction of output capacity. While flow In_2 uses the remaining fraction, P2/(P1+P2).

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.

API Documentation

Documentation can be found here.

Example

import (
	"context"
	"fmt"

	"github.com/bigmikes/drr"
)

func sourceRequests(s string) <-chan string {
	inChan := make(chan string, 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 string, 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

License

The drr package is licensed under the MIT License. Please see the LICENSE file for details.

Contributing and bug reports

This package surely needs your help and feedbacks. You are welcome to open a new issue here on GitHub.