Path: redb-route/redb.Route/Processors/ScatterGatherProcessor.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/ScatterGatherProcessor.cs ---- using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using redb.Route.Abstractions; using redb.Route.Core; namespace redb.Route.Processors; /// /// Processor for the Scatter-Gather EIP pattern. /// Sends the exchange to multiple endpoints (scatter phase), collects all responses, /// and aggregates them using a mandatory pair-wise strategy (gather phase). /// Supports static and dynamic recipient lists, parallel/sequential processing, /// timeout with partial-result aggregation, and configurable error handling. /// public sealed class ScatterGatherProcessor : IProcessor, IAsyncDisposable { private readonly IRouteContext _context; private readonly IReadOnlyList? _staticRecipients; private readonly Func>? _dynamicRecipients; private readonly Func _aggregationStrategy; private readonly TimeSpan _timeout; private readonly bool _parallelProcessing; private readonly bool _stopOnException; private readonly int _maxDegreeOfParallelism; private readonly ILogger? _logger; private readonly ConcurrentDictionary> _producerCache = new(StringComparer.Ordinal); /// /// Creates a scatter-gather processor with static recipients. /// /// Route context for endpoint resolution. /// Fixed list of endpoint URIs to scatter to. /// Pair-wise aggregation: (accumulated, current) → merged. /// Whether to send to all recipients in parallel. /// Max concurrent sends (0 = processor count). /// Whether to stop on the first error (true) or aggregate partial results (false). /// Timeout for the entire operation (TimeSpan.Zero = no timeout). /// Optional logger. public ScatterGatherProcessor( IRouteContext context, IReadOnlyList recipients, Func aggregationStrategy, bool parallelProcessing = true, int maxDegreeOfParallelism = 0, bool stopOnException = false, TimeSpan timeout = default, ILogger? logger = null) { _context = context ?? throw new ArgumentNullException(nameof(context)); ArgumentNullException.ThrowIfNull(recipients); if (recipients.Count == 0) throw new ArgumentException("At least one recipient is required.", nameof(recipients)); _staticRecipients = recipients; foreach (var recipient in recipients) (context as RouteContext)?.NoteStaticEndpoint(recipient); _dynamicRecipients = null; _aggregationStrategy = aggregationStrategy ?? throw new ArgumentNullException(nameof(aggregationStrategy)); _parallelProcessing = parallelProcessing; _maxDegreeOfParallelism = maxDegreeOfParallelism; _stopOnException = stopOnException; _timeout = timeout >= TimeSpan.Zero ? timeout : throw new ArgumentOutOfRangeException(nameof(timeout)); _logger = logger; } /// /// Creates a scatter-gather processor with dynamic recipients resolved at runtime. /// /// Route context for endpoint resolution. /// Function that returns target URIs from the exchange. /// Pair-wise aggregation: (accumulated, current) → merged. /// Whether to send to all recipients in parallel. /// Max concurrent sends (0 = processor count). /// Whether to stop on the first error (true) or aggregate partial results (false). /// Timeout for the entire operation (TimeSpan.Zero = no timeout). /// Optional logger. public ScatterGatherProcessor( IRouteContext context, Func> recipientFactory, Func aggregationStrategy, bool parallelProcessing = true, int maxDegreeOfParallelism = 0, bool stopOnException = false, TimeSpan timeout = default, ILogger? logger = null) { _context = context ?? throw new ArgumentNullException(nameof(context)); _staticRecipients = null; _dynamicRecipients = recipientFactory ?? throw new ArgumentNullException(nameof(recipientFactory)); _aggregationStrategy = aggregationStrategy ?? throw new ArgumentNullException(nameof(aggregationStrategy)); _parallelProcessing = parallelProcessing; _maxDegreeOfParallelism = maxDegreeOfParallelism; _stopOnException = stopOnException; _timeout = timeout >= TimeSpan.Zero ? timeout : throw new ArgumentOutOfRangeException(nameof(timeout)); _logger = logger; } /// public async Task Process(IExchange exchange, CancellationToken ct = default) { ArgumentNullException.ThrowIfNull(exchange); IReadOnlyList recipients = _staticRecipients ?? (IReadOnlyList)_dynamicRecipients!(exchange).ToList(); if (recipients.Count == 0) throw new InvalidOperationException("Scatter-gather requires at least one recipient."); _logger?.LogDebug("Scatter-gather: dispatching to {Count} recipients", recipients.Count); using var timeoutCts = _timeout > TimeSpan.Zero ? CancellationTokenSource.CreateLinkedTokenSource(ct) : null; timeoutCts?.CancelAfter(_timeout); var effectiveCt = timeoutCts?.Token ?? ct; try { if (_parallelProcessing) await ProcessParallel(exchange, recipients, effectiveCt, ct).ConfigureAwait(false); else await ProcessSequential(exchange, recipients, effectiveCt, ct).ConfigureAwait(false); } // Wraps timeout-only OCE into TimeoutException. // In best-effort mode, timeout is handled internally (partial results aggregated) // so this catch only fires in stopOnException mode. catch (OperationCanceledException) when (_timeout > TimeSpan.Zero && !ct.IsCancellationRequested) { // Timeout reached with stopOnException — wrap in TimeoutException throw new TimeoutException($"Scatter-gather timed out after {_timeout}."); } } private async Task ProcessSequential( IExchange exchange, IReadOnlyList recipients, CancellationToken ct, CancellationToken callerCt) { IExchange? aggregated = null; var clones = new List(recipients.Count); try { foreach (var uri in recipients) { ct.ThrowIfCancellationRequested(); var clone = exchange.CorrelatedCopyLinked(); clones.Add(clone); try { var producer = GetOrCreateProducer(uri); await producer.Process(clone, ct).ConfigureAwait(false); aggregated = aggregated is null ? clone : _aggregationStrategy(aggregated, clone); _logger?.LogDebug("Scatter-gather: received response from {Endpoint}", EndpointUri.Sanitize(uri)); } catch (OperationCanceledException) when (!callerCt.IsCancellationRequested && !_stopOnException) { // Timeout in best-effort mode — aggregate partial results collected so far _logger?.LogWarning( "Scatter-gather: timed out, aggregating partial results"); break; } catch (OperationCanceledException) { throw; } catch (Exception ex) { if (_stopOnException) throw; clone.Exception = ex; _logger?.LogWarning(ex, "Scatter-gather: endpoint {Endpoint} failed", EndpointUri.Sanitize(uri)); aggregated = aggregated is null ? clone : _aggregationStrategy(aggregated, clone); } } ApplyAggregation(exchange, aggregated); } finally { foreach (var clone in clones) if (clone is IAsyncDisposable d) await d.DisposeAsync().ConfigureAwait(false); } } private async Task ProcessParallel( IExchange exchange, IReadOnlyList recipients, CancellationToken ct, CancellationToken callerCt) { var clones = new IExchange?[recipients.Count]; try { var maxDop = _maxDegreeOfParallelism > 0 ? _maxDegreeOfParallelism : Environment.ProcessorCount; using var semaphore = new SemaphoreSlim(maxDop); async Task SendToRecipient(int index) { await semaphore.WaitAsync(ct).ConfigureAwait(false); try { var clone = exchange.CorrelatedCopy(); clones[index] = clone; var producer = GetOrCreateProducer(recipients[index]); // Concurrent branches never share one Transaction.Current: each runs under a dependent clone of the // route's transaction, as a parallel Multicast or Split does (see DependentTransactionBranch). await Transactions.DependentTransactionBranch.RunAsync(() => producer.Process(clone, ct)).ConfigureAwait(false); _logger?.LogDebug("Scatter-gather: received response from {Endpoint}", EndpointUri.Sanitize(recipients[index])); return clone; } finally { // Release DI scopes early; body/headers stay alive for aggregation if (clones[index] != null) await clones[index]!.ReleaseScopes().ConfigureAwait(false); semaphore.Release(); } } var tasks = Enumerable.Range(0, recipients.Count) .Select(SendToRecipient) .ToList(); IExchange?[] results; if (_stopOnException) { results = await Task.WhenAll(tasks).ConfigureAwait(false); } else { // Best-effort: collect completed results, handle failures/timeouts gracefully results = await Task.WhenAll(tasks.Select(async (t, index) => { try { return await t.ConfigureAwait(false); } catch (OperationCanceledException) when (!callerCt.IsCancellationRequested) { // Timeout — skip this result _logger?.LogWarning("Scatter-gather: endpoint {Endpoint} timed out", EndpointUri.Sanitize(recipients[index])); return null; } catch (OperationCanceledException) { throw; } catch (Exception ex) when (ex is not OutOfMemoryException) { var clone = clones[index] ?? exchange.CorrelatedCopy(); clones[index] = clone; clone.Exception = ex; _logger?.LogWarning(ex, "Scatter-gather: endpoint {Endpoint} failed", EndpointUri.Sanitize(recipients[index])); return clone; } })).ConfigureAwait(false); } // Aggregate in deterministic index order IExchange? aggregated = null; foreach (var result in results) { if (result == null) continue; aggregated = aggregated is null ? result : _aggregationStrategy(aggregated, result); } ApplyAggregation(exchange, aggregated); } finally { foreach (var clone in clones) if (clone is IAsyncDisposable d) await d.DisposeAsync().ConfigureAwait(false); } } /// /// Copies aggregated body, headers, and properties back to the original exchange. /// private static void ApplyAggregation(IExchange original, IExchange? aggregated) { if (aggregated == null) return; original.In.Body = aggregated.In.Body; foreach (var header in aggregated.In.Headers) original.In.Headers[header.Key] = header.Value; foreach (var prop in aggregated.Properties) { // The clone's own bookkeeping (cached DI scopes, registered resources) stays with the clone, which // releases it right after this merge; copying it here would leave the original a disposed scope. // The branch's correlation id points at the original; the original keeps its own, as in Camel. if (ExchangeResources.IsOwnedByExchange(prop.Key) || prop.Key == ExchangePropertyKeys.CorrelationId) continue; original.Properties[prop.Key] = prop.Value; } } 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() { List? errors = null; foreach (var lazy in _producerCache.Values) { if (lazy.IsValueCreated) { try { await lazy.Value.StopProducerAsync().ConfigureAwait(false); } catch (Exception ex) { errors ??= []; errors.Add(ex); } } } _producerCache.Clear(); if (errors != null) throw new AggregateException("One or more producers failed to stop.", errors); } }