C#C
C#4y ago
Ben

How to locally loadbalance threadsafe [Answered]

Hola,
I got an instance of a class that holds a list of end-points.
The class performs and awaits an http request to one of the end-points in the list.

The same instance of the class will be accessed async from multiple threads (about 300 times a second)
I need to round-robin load-balance the endpoints in the list.

Initially I thought of using a concurrentQueue and to dequeue/enqueue before and after every request. but the locking will hurt my performance as I have no need to wait for each request to finish before using the same end-point again..
I want that eventually within the 1 second, the 300 requests will be balanced between all the endpoints without delaying them.

I thought of using counter field as an index accessor, that I keep adjusting its value in a circle according to the end-points length.
what do you think? is there a better approach?

Simplified example -
public class MyClass
{
  List<string> _endpoints;
  int _indexCounter = 0;
  public MyClass(List<string> endpoints)
  {
    _endpoints = endpoints;    
  }
  
  public Task SendRequest()
  {
    var endpoint = _endpoints[_indexCounter];
    indexCounter = _indexCounter < _endpoints.Length-1 ? _indexCounter+1 : 0;
    
    await client.Request(endpoint);
  }
}
Was this page helpful?