Path: redb-route/redb.Route/Core/ConsumerTemplate.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/Core/ConsumerTemplate.cs
----
using System.Collections.Concurrent;
using System.Threading.Channels;
using redb.Route.Abstractions;
using redb.Route.Components;
namespace redb.Route.Core;
///
/// Template for polling messages from endpoints programmatically, as Apache Camel's.
/// SEDA endpoints are read straight from their queue. Any other endpoint gets one consumer per
/// endpoint, started on first use and kept until : every exchange it produces waits
/// in a queue for a Receive, and its source does not commit it (delete or move the file,
/// acknowledge the message) until the caller completes it with . Exchanges no
/// Receive took stay uncommitted in their source.
///
public class ConsumerTemplate : IConsumerTemplate, IDisposable
{
private readonly ConcurrentDictionary>> _consumers =
new(ReferenceEqualityComparer.Instance);
// Exchanges handed out and not completed yet; the value is null for a SEDA exchange, which has
// no unit of work to complete.
private readonly ConcurrentDictionary _open = new(ReferenceEqualityComparer.Instance);
private volatile bool _started;
private volatile bool _disposed;
///
/// Initializes a new instance with the specified route context.
///
/// The route context for endpoint resolution.
public ConsumerTemplate(IRouteContext context)
{
Context = context ?? throw new ArgumentNullException(nameof(context));
}
///
public IRouteContext Context { get; }
///
public bool IsStarted => _started;
// ── Receive (blocking) ──
///
public Task Receive(string endpointUri, CancellationToken ct = default)
{
EnsureStarted();
var endpoint = Context.GetEndpoint(endpointUri);
return Receive(endpoint, ct);
}
///
public async Task Receive(IEndpoint endpoint, CancellationToken ct = default)
{
EnsureStarted();
ArgumentNullException.ThrowIfNull(endpoint);
// Optimized path for SEDA — read directly from the channel
if (endpoint is SedaEndpoint seda)
return HandOut(await seda.Queue.Reader.ReadAsync(ct).ConfigureAwait(false), null);
return await ReceiveViaConsumer(endpoint, Timeout.InfiniteTimeSpan, ct).ConfigureAwait(false)
?? throw new OperationCanceledException("Receive was cancelled: the template stopped.", ct);
}
// ── Receive with timeout ──
///
public Task Receive(string endpointUri, TimeSpan timeout, CancellationToken ct = default)
{
EnsureStarted();
var endpoint = Context.GetEndpoint(endpointUri);
return Receive(endpoint, timeout, ct);
}
///
public async Task Receive(IEndpoint endpoint, TimeSpan timeout, CancellationToken ct = default)
{
EnsureStarted();
ArgumentNullException.ThrowIfNull(endpoint);
// Optimized path for SEDA
if (endpoint is SedaEndpoint seda)
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct);
cts.CancelAfter(timeout);
try
{
return HandOut(await seda.Queue.Reader.ReadAsync(cts.Token).ConfigureAwait(false), null);
}
catch (OperationCanceledException) when (!ct.IsCancellationRequested)
{
return null; // Timeout — not caller cancellation
}
}
return await ReceiveViaConsumer(endpoint, timeout, ct).ConfigureAwait(false);
}
// ── ReceiveNoWait ──
///
public Task ReceiveNoWait(string endpointUri, CancellationToken ct = default)
{
EnsureStarted();
var endpoint = Context.GetEndpoint(endpointUri);
return ReceiveNoWait(endpoint, ct);
}
///
public Task ReceiveNoWait(IEndpoint endpoint, CancellationToken ct = default)
{
EnsureStarted();
ArgumentNullException.ThrowIfNull(endpoint);
ct.ThrowIfCancellationRequested();
// Optimized path for SEDA
if (endpoint is SedaEndpoint seda)
{
return Task.FromResult(
seda.Queue.Reader.TryRead(out var exchange) ? HandOut(exchange, null) : null);
}
// Generic: whatever the endpoint's consumer has already offered
return Receive(endpoint, TimeSpan.Zero, ct);
}
// ── Typed body convenience ──
///
public async Task