GithubHelp home page GithubHelp logo

drr's Introduction

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 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.

API Documentation

Documentation can be found here.

Example

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

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.

drr's People

Contributors

fossabot avatar gmichelo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.