Path: redb-route/redb.Route/Components/SedaComponent.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/Components/SedaComponent.cs
----
using System.Collections.Concurrent;
using System.Threading.Channels;
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Transactions;
namespace redb.Route.Components;
///
/// SEDA (Staged Event-Driven Architecture) component. Scheme: "seda".
/// Provides asynchronous in-memory queuing between route segments.
/// Producer enqueues the exchange and returns immediately; a background consumer
/// loop dequeues and processes exchanges asynchronously.
/// URI: seda://name?concurrentConsumers=1&size=0
///
public class SedaComponent : ComponentBase
{
private readonly ConcurrentDictionary _endpoints = new(StringComparer.OrdinalIgnoreCase);
///
public override string Scheme => "seda";
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
var options = new SedaEndpointOptions();
options.BindFromUri(uri.RawParameters);
options.Validate();
return _endpoints.GetOrAdd(uri.BaseKey, _ => new SedaEndpoint(uri, this, options));
}
}
///
/// Options for SEDA endpoints.
///
public class SedaEndpointOptions : EndpointOptions
{
/// Concurrent consumer workers: a number or "auto" (= max(CPU, 2)). Default 1.
// A string so "auto" binds verbatim instead of silently degrading to the int default.
public string? ConcurrentConsumers { get; set; }
/// Resolved worker count (see ).
public int ResolvedConcurrentConsumers => ConcurrencyOption.Resolve(ConcurrentConsumers, "concurrentConsumers");
///
/// Maximum queue size (default: 1000, as Camel). A full queue makes the sender wait up to timeout. 0 = unbounded,
/// an explicit choice: a consumer slower than its senders then grows the queue without limit.
///
public int Size { get; set; } = 1000;
/// Timeout in milliseconds for enqueue when queue is bounded. 0 = wait forever (default: 30000).
public int Timeout { get; set; } = 30000;
///
public override void Validate()
{
_ = ConcurrencyOption.Resolve(ConcurrentConsumers, "concurrentConsumers"); // loud on garbage
if (Size < 0)
throw new ArgumentOutOfRangeException(nameof(Size), Size, "Size cannot be negative.");
}
}
///
/// SEDA endpoint. Uses as the in-memory async queue.
///
public class SedaEndpoint : EndpointBase
{
private readonly object _queueGate = new();
private Channel _queue;
private bool _closed;
///
/// The queue of the endpoint. A stopping consumer closes it () and the next start of a consumer
/// opens a new one (): the endpoint is cached by its component and outlives a
/// Stop() and Start() of the route context, while a closed channel cannot take writes again.
///
internal Channel Queue => Volatile.Read(ref _queue);
/// Creates a SEDA endpoint.
public SedaEndpoint(EndpointUri uri, SedaComponent component, SedaEndpointOptions options)
: base(uri, component, options)
{
_queue = CreateQueue(options);
}
private static Channel CreateQueue(SedaEndpointOptions options) => options.Size > 0
? Channel.CreateBounded(new BoundedChannelOptions(options.Size)
{
FullMode = BoundedChannelFullMode.Wait,
SingleReader = options.ResolvedConcurrentConsumers == 1,
SingleWriter = false
})
: Channel.CreateUnbounded(new UnboundedChannelOptions
{
SingleReader = options.ResolvedConcurrentConsumers == 1,
SingleWriter = false
});
///
/// Stops the queue taking new exchanges: a send fails with , and the consumer
/// finishes what is already queued.
///
internal void Close()
{
lock (_queueGate)
{
_closed = true;
_queue.Writer.TryComplete();
}
}
///
/// Opens a new queue in place of a closed one, for a consumer that starts again. Exchanges the stopping consumer
/// did not get to (its drain timed out) cannot be read from the closed queue by anyone: they are returned to be
/// released, and never handed over silently to the new one.
///
internal IReadOnlyList Reopen()
{
lock (_queueGate)
{
if (!_closed)
return [];
var left = new List();
while (_queue.Reader.TryRead(out var exchange))
left.Add(exchange);
Volatile.Write(ref _queue, CreateQueue(Options));
_closed = false;
return left;
}
}
/// Gets the current approximate queue size.
public int CurrentQueueSize => Queue.Reader.CanCount ? Queue.Reader.Count : -1;
///
public override IProducer CreateProducer() => new SedaProducer(this);
///
public override IConsumer CreateConsumer(IProcessor processor)
{
ArgumentNullException.ThrowIfNull(processor);
return new SedaConsumer(this, processor, Options);
}
}
///
/// SEDA producer. Enqueues the exchange into the channel and returns immediately.
/// The exchange is processed asynchronously by the consumer.
///
public class SedaProducer : IProducer
{
private readonly SedaEndpoint _endpoint;
private ILogger? _logger;
/// Creates a SEDA producer.
public SedaProducer(SedaEndpoint endpoint)
{
_endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
_logger = (endpoint.Component as ComponentBase)?.Logger;
}
///
public async Task Process(IExchange exchange, CancellationToken ct = default)
{
// Clone to ensure thread-safety: the pipeline thread and the SEDA worker
// must not share a mutable exchange. Clone also creates a new DI scope.
var copy = exchange.CorrelatedCopy();
// The copy runs later on the SEDA worker, a unit of work of its own: it must not see the sending
// .Transacted() block as its own.
TransactedActions.DetachFromBlock(copy);
try
{
await _endpoint.Queue.Writer.WriteAsync(copy, ct).ConfigureAwait(false);
}
catch
{
// The clone was NOT enqueued (cancellation, or the queue was completed on shutdown), so
// the consumer will never dispose it — release its per-exchange DI scope / DB connection
// here, then rethrow so the caller still observes the failure.
await copy.DisposeAsync().ConfigureAwait(false);
throw;
}
}
///
public Task Start(CancellationToken ct = default)
{
_logger?.LogInformation("SEDA producer started: {Name}", EndpointUri.ForLog(_endpoint.Uri.NormalizedKey, _logger));
return Task.CompletedTask;
}
///
public Task Stop(CancellationToken ct = default)
{
_logger?.LogInformation("SEDA producer stopped: {Name}", EndpointUri.ForLog(_endpoint.Uri.NormalizedKey, _logger));
return Task.CompletedTask;
}
}
///
/// SEDA consumer. Runs N concurrent workers that dequeue and process exchanges.
///
public class SedaConsumer : DrainableConsumer
{
private readonly SedaEndpoint _endpoint;
private readonly SedaEndpointOptions _options;
private Task[]? _workers;
private long _processedCount;
///
protected override IEndpoint ConsumerEndpoint => _endpoint;
///
protected override string ConsumerName => _endpoint.Uri.NormalizedKey;
/// Total number of exchanges processed across all workers.
public long ProcessedCount => Interlocked.Read(ref _processedCount);
/// Creates a SEDA consumer.
public SedaConsumer(SedaEndpoint endpoint, IProcessor processor, SedaEndpointOptions options)
: base(processor)
{
_endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
_options = options ?? throw new ArgumentNullException(nameof(options));
}
///
protected override Task RunAsync(CancellationToken pollCt, CancellationToken processingCt)
{
_workers = new Task[_options.ResolvedConcurrentConsumers];
for (var i = 0; i < _options.ResolvedConcurrentConsumers; i++)
_workers[i] = WorkerLoop(pollCt, processingCt);
return Task.CompletedTask;
}
///
protected override async Task OnStarting(CancellationToken ct)
{
// A consumer that starts again after a stop (a Stop() and Start() of the route context, a restart of the route)
// gets a new queue: the stop closed the old one for good.
var left = _endpoint.Reopen();
if (left.Count == 0)
return;
Logger?.LogWarning(
"{Consumer}: {Count} exchange(s) were still queued when the consumer last stopped and are dropped.",
ConsumerName, left.Count);
foreach (var exchange in left)
await exchange.DisposeAsync().ConfigureAwait(false);
}
///
protected override Task OnStopAccepting()
{
// Sends fail from here on, and the workers finish what is already queued (ReadAllAsync reads buffered
// exchanges without looking at the poll token).
_endpoint.Close();
return Task.CompletedTask;
}
///
protected override async Task AwaitRunTask()
{
if (_workers != null)
{
try { await Task.WhenAll(_workers).ConfigureAwait(false); }
catch (OperationCanceledException) { }
}
}
private async Task WorkerLoop(CancellationToken pollCt, CancellationToken processingCt)
{
try
{
await foreach (var exchange in _endpoint.Queue.Reader.ReadAllAsync(pollCt).ConfigureAwait(false))
{
await ProcessWithTracking(exchange, processingCt).ConfigureAwait(false);
Interlocked.Increment(ref _processedCount);
}
}
catch (OperationCanceledException) { }
catch (ChannelClosedException) { }
}
}