Round Robin implementation in C#
Ali Alp

Ali Alp @alialp

About: “Take it easy” is nonsense , take it as hard as you can and don’t let it go :)

Location:
Germany
Joined:
May 28, 2018

Round Robin implementation in C#

Publish Date: Jan 12 '20
20 5

Round Robin is a very simple but in the same time very useful algorithm,
but there is no native implementation in C# . So here is a simple but powerful and thread-safe implementation of the Round Robin algorithm in C#.

Source Code

Nuget

Usage

//installation
dotnet add package RoundRobin
Install-Package RoundRobin
Enter fullscreen mode Exit fullscreen mode
var roundRobinList = new RoundRobinList<int>(
    new List<int>{
        1,2,3,4,5
    }
);

for (var i = 0; i < 8; i++)
{
    Write($"{roundRobinList.Next()},");
}

//result
//1,2,3,4,5,1,2,3,

Enter fullscreen mode Exit fullscreen mode

Also you can increase/decrease the weights, by doing so you will be able to increase/decrease the priority of an element in the Round Robin list

var roundRobinList = new RoundRobinList<int>(
    new List<int>{
        1,2,3,4,5
    }
);

//the weight of the element 1 will be increase by 2 units
roundRobinList.IncreaseWeight(element:1, amount:2);

for (var i = 0; i < 10; i++)
{
    Write($"{roundRobinList.Next()},");
}

//result
//1,1,1,2,3,4,5,1,1,1
Enter fullscreen mode Exit fullscreen mode

Enjoy coding :)

Comments 5 total

  • Nishchal Gautam
    Nishchal GautamJan 13, 2020

    I like this implementation of round robin, it just does one thing and that's round robin (I've seen implementations of RR where assumption is always load balancing and http is baked right in :D )

    I do however suggest a few features:

    • Weighted Round Robin (you can already do that by adding multiple instance of data), but it'd be good to maintain this internally
    • Ability to reset a instance's weight and decrement the weight

    The second one would be good when let's say I have 5 endpoints and I've 5 clients with weight of 10 each (and maybe one of the machine is more reliable and has more resources, so maybe I want to set weight of 15)

    Now when my app is running, if a machine goes down or fails somehow, I'd like to decrement the weight by 1, and if a machine goes down for a long time, it'd be taken off of our round robin, but before it reaches 0 and it comes back online, then we could reset it back to original value.

    Basically requirement would be weighted round robin with the ability to change the weight of current instance.

    • Ali Alp
      Ali AlpJan 13, 2020

      hi there , happy to hear that you liked my algorithm :) , I have added the feature that you have requested. I hope it will be useful for you :)

  • virender verma
    virender vermaFeb 2, 2021

    You using List inside roundRobinList object . Have you used function reference in roundrobinlist.

    For example , We have 10 function . But we want call each function 1 by 1 and pass some parameters to functions .

    Like 10 scraper but each scraper have different logic inside it.

    So, I want pass URL to scraper functions 1 by 1.

    • Ali Alp
      Ali AlpOct 9, 2024

      sorry for the late response, Nope I am using T which gives you the freedom to do whatever type you prefer .
      for your use case : create the list of the functions with and then pass the list to the roundrobin then you can achieve your goal .
      let me know if you needed help on that .

  • Farid Bekran
    Farid BekranFeb 14, 2022

    Hi Ali, Thanks for the greate package.
    I have got a problem with the collection when the input data was empty.
    In an empty round roubin list the Next() method call would throw a NullReferenceException but I thinks it is better to throw more accurate exception to indicate that an empty list is not a normal state of round robin collection.
    I have also created a pull request for the change.
    But if you prefer that an empty round roubin collection does not need to be an invalid state of collection, in that case I can come up with the solution to deal with emty input data gracefully and not throwing exceptions.

    Pull request:
    github.com/alicommit-malp/roundrob...

Add comment