Path: redb-route/redb.Route/Processors/LoadBalancer/RoundRobinStrategy.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Processors/LoadBalancer/RoundRobinStrategy.cs
----
using System;
using System.Collections.Generic;
using System.Threading;
using redb.Route.Abstractions;
namespace redb.Route.Processors.LoadBalancer;
///
/// Round-robin load balancer strategy.
/// Cycles through endpoints evenly using a lock-free rotating index.
///
public sealed class RoundRobinStrategy : ILoadBalancerStrategy
{
private int _index = -1;
///
public string Select(IExchange exchange, IReadOnlyList endpoints)
{
if (endpoints.Count == 0)
throw new InvalidOperationException("No endpoints configured for load balancer.");
var idx = Interlocked.Increment(ref _index);
// Handle int overflow: ensure non-negative modulo
return endpoints[((idx % endpoints.Count) + endpoints.Count) % endpoints.Count];
}
}