Path: redb-route/redb.Route/Definitions/ScatterGatherDefinition.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/Definitions/ScatterGatherDefinition.cs
----
using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Processors;
namespace redb.Route.Definitions;
///
/// Definition for a Scatter-Gather step. Collects configuration and creates a
/// at route-build time.
///
public sealed class ScatterGatherDefinition : ProcessorDefinition, IScatterGatherDefinition
{
/// Fixed recipient URIs (null when dynamic recipients are configured).
public string[]? StaticRecipients { get; private set; }
/// Runtime recipient factory (null when static recipients are configured).
public Func>? DynamicRecipients { get; private set; }
/// Mandatory pair-wise aggregation: (accumulated, current) → merged.
public Func? AggregationStrategy { get; private set; }
/// Maximum duration to wait for all responses (default: zero = no timeout).
public TimeSpan Timeout { get; private set; }
/// Whether recipients are processed in parallel (default: true).
public bool ParallelProcessing { get; private set; } = true;
/// Maximum degree of parallelism (0 = processor count).
public int MaxDegreeOfParallelism { get; private set; }
/// Whether to stop immediately on first exception (default: false).
public bool StopOnException { get; private set; }
// ── IScatterGatherDefinition — explicit to avoid name conflicts with properties ──
IScatterGatherDefinition IScatterGatherDefinition.Recipients(params string[] uris)
{
StaticRecipients = uris ?? throw new ArgumentNullException(nameof(uris));
DynamicRecipients = null;
return this;
}
IScatterGatherDefinition IScatterGatherDefinition.Recipients(Func> factory)
{
DynamicRecipients = factory ?? throw new ArgumentNullException(nameof(factory));
StaticRecipients = null;
return this;
}
IScatterGatherDefinition IScatterGatherDefinition.AggregationStrategy(Func strategy)
{
AggregationStrategy = strategy ?? throw new ArgumentNullException(nameof(strategy));
return this;
}
IScatterGatherDefinition IScatterGatherDefinition.Timeout(TimeSpan timeout)
{
Timeout = timeout >= TimeSpan.Zero
? timeout
: throw new ArgumentOutOfRangeException(nameof(timeout), "Timeout must be non-negative.");
return this;
}
IScatterGatherDefinition IScatterGatherDefinition.ParallelProcessing(bool parallel)
{
ParallelProcessing = parallel;
return this;
}
IScatterGatherDefinition IScatterGatherDefinition.MaxDegreeOfParallelism(int maxDop)
{
MaxDegreeOfParallelism = maxDop >= 0
? maxDop
: throw new ArgumentOutOfRangeException(nameof(maxDop), "Max DOP must be non-negative.");
return this;
}
IScatterGatherDefinition IScatterGatherDefinition.StopOnException(bool stop)
{
StopOnException = stop;
return this;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
{
if (AggregationStrategy is null)
throw new InvalidOperationException("AggregationStrategy is required for ScatterGather.");
if (StaticRecipients is null && DynamicRecipients is null)
throw new InvalidOperationException("Recipients are required for ScatterGather.");
var logger = context.GetService()?.CreateLogger();
return StaticRecipients is not null
? new ScatterGatherProcessor(context, StaticRecipients, AggregationStrategy,
ParallelProcessing, MaxDegreeOfParallelism, StopOnException, Timeout, logger)
: new ScatterGatherProcessor(context, DynamicRecipients!, AggregationStrategy,
ParallelProcessing, MaxDegreeOfParallelism, StopOnException, Timeout, logger);
}
}