Path: redb-route/redb.Route/Processors/PipelineProcessor.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/PipelineProcessor.cs
----
using redb.Route.Abstractions;
namespace redb.Route.Processors;
///
/// Executes a list of processors sequentially. The backbone of route processing.
/// Stops early if exchange.IsStopped or cancellation is requested.
///
public class PipelineProcessor : IProcessor
{
private readonly List _processors = [];
/// Gets the list of processors in this pipeline.
public IReadOnlyList Processors => _processors;
/// Adds a processor to the end of the pipeline.
/// Processor to add.
public void Add(IProcessor processor)
{
ArgumentNullException.ThrowIfNull(processor);
_processors.Add(processor);
}
/// Adds multiple processors to the pipeline.
/// Processors to add.
public void AddRange(IEnumerable processors)
{
ArgumentNullException.ThrowIfNull(processors);
_processors.AddRange(processors);
}
///
public Task Process(IExchange exchange, CancellationToken ct = default) => ProcessFrom(exchange, 0, ct);
///
/// Runs the pipeline from . A failure records where the pipeline stopped
/// (), so an OnException ... Continued() handler can pick the route up at the next
/// step, as Apache Camel does.
///
internal async Task ProcessFrom(IExchange exchange, int startIndex, CancellationToken ct)
{
// Pipeline EIP semantics:
// - Intermediate steps that produce Out are merged (Out → In, Out := null) so the
// next step sees the result as its input.
// - The final step's Out (if any) is left intact so InOut callers (direct-vm,
// replyTo, RPC) get the reply via exchange.Out.
// - If the final step does NOT produce Out, we do not synthesize one from earlier
// steps: any subsequent step that modified In is the authoritative final result.
for (var i = startIndex; i < _processors.Count; i++)
{
ct.ThrowIfCancellationRequested();
if (exchange.IsStopped) break;
try
{
await _processors[i].Process(exchange, ct).ConfigureAwait(false);
}
catch (Exception failure) when (failure is not OperationCanceledException)
{
ResumePoints.Record(exchange, failure, this, i + 1);
throw;
}
// Pipeline EIP: propagate Out body/headers → In so the next processor sees the result.
// ContentType is merged like a header, not taken wholesale: a step that writes an Out body
// without setting a content type leaves the In content type in place (it used to be nulled,
// which lost the format for a body that had not declared its own).
if (exchange.HasOut && i < _processors.Count - 1)
{
var outMsg = exchange.Out!;
exchange.In.Body = outMsg.Body;
if (outMsg.ContentType is not null)
exchange.In.ContentType = outMsg.ContentType;
foreach (var (key, value) in outMsg.Headers)
exchange.In.Headers[key] = value;
// Attachments follow the content type rule: an Out that carries some replaces those of In, an Out that
// carries none (a producer's reply) leaves the In ones for the next step.
Core.MessageAttachments.ReplaceWhenPresent(outMsg, exchange.In);
exchange.Out = null; // prevent stale Out from re-merging on next step
}
}
}
}