Path: redb-route/redb.Route/Processors/LoadBalancer/LoadBalancerProcessor.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/LoadBalancerProcessor.cs
----
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Processors.LoadBalancer;
///
/// Processor for the Load Balancer EIP pattern.
/// Selects an endpoint using a pluggable ,
/// then delegates the exchange to the selected endpoint's producer.
/// Producers are lazily created and cached per endpoint URI (thread-safe).
/// For , automatically retries with the next healthy endpoint on failure.
///
public sealed class LoadBalancerProcessor : IProcessor, IAsyncDisposable
{
private readonly IRouteContext _context;
private readonly IReadOnlyList _endpoints;
private readonly ILoadBalancerStrategy _strategy;
private readonly ILogger? _logger;
private readonly ConcurrentDictionary> _producerCache = new(StringComparer.Ordinal);
///
/// Creates a new load balancer processor.
///
/// Route context for endpoint resolution.
/// List of endpoint URIs to balance across.
/// Strategy for selecting the next endpoint.
/// Optional logger.
public LoadBalancerProcessor(
IRouteContext context,
IReadOnlyList endpoints,
ILoadBalancerStrategy strategy,
ILogger? logger = null)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_endpoints = endpoints ?? throw new ArgumentNullException(nameof(endpoints));
foreach (var endpoint in endpoints)
(context as RouteContext)?.NoteStaticEndpoint(endpoint);
_strategy = strategy ?? throw new ArgumentNullException(nameof(strategy));
_logger = logger;
if (endpoints.Count == 0)
throw new ArgumentException("At least one endpoint is required.", nameof(endpoints));
}
///
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(exchange);
if (_strategy is FailoverStrategy)
{
await ProcessWithFailover(exchange, ct).ConfigureAwait(false);
return;
}
var selected = _strategy.Select(exchange, _endpoints);
await SendToEndpoint(selected, exchange, ct).ConfigureAwait(false);
}
private async Task ProcessWithFailover(IExchange exchange, CancellationToken ct)
{
// Try up to N endpoints (one per attempt)
for (var attempt = 0; attempt < _endpoints.Count; attempt++)
{
var selected = _strategy.Select(exchange, _endpoints);
try
{
var producer = GetOrCreateProducer(selected);
await producer.Process(exchange, ct).ConfigureAwait(false);
_strategy.ReportSuccess(selected);
_logger?.LogDebug("Load balancer: sent to {Endpoint} (failover attempt {Attempt})",
EndpointUri.Sanitize(selected), attempt + 1);
return;
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception ex)
{
_strategy.ReportFailure(selected, ex);
_logger?.LogWarning(ex,
"Load balancer: endpoint {Endpoint} failed (attempt {Attempt}/{Total})",
selected, attempt + 1, _endpoints.Count);
// Last attempt — re-throw
if (attempt == _endpoints.Count - 1)
throw;
}
}
}
private async Task SendToEndpoint(string endpoint, IExchange exchange, CancellationToken ct)
{
try
{
var producer = GetOrCreateProducer(endpoint);
await producer.Process(exchange, ct).ConfigureAwait(false);
_strategy.ReportSuccess(endpoint);
_logger?.LogDebug("Load balancer: sent to {Endpoint}", EndpointUri.Sanitize(endpoint));
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
_strategy.ReportFailure(endpoint, ex);
throw;
}
}
private ToProcessor GetOrCreateProducer(string endpoint)
{
return _producerCache.GetOrAdd(endpoint,
ep => new Lazy(() => new ToProcessor(ep, _context))).Value;
}
///
/// Stops all cached producers and clears the cache.
///
public async ValueTask DisposeAsync()
{
foreach (var lazy in _producerCache.Values)
{
if (lazy.IsValueCreated)
await lazy.Value.StopProducerAsync().ConfigureAwait(false);
}
_producerCache.Clear();
}
}