Path: redb-route/redb.Route/Processors/CheckpointProcessor.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/CheckpointProcessor.cs
----
using Microsoft.Extensions.Logging;
using redb.Route.Abstractions;
namespace redb.Route.Processors;
///
/// Compiled body of a .Replayable("name") marker (a save-point). On each pass it captures a
/// deep, isolated into
/// [] (last marker wins),
/// then runs the marker's body. The same compiled is what
///
/// re-runs from a stored snapshot — no synthetic endpoint.
///
/// SECURITY: the snapshot is a verbatim, UNREDACTED copy of the mid-flight exchange — body, headers
/// and Properties may hold decrypted tokens, PII or secrets. Nothing is masked here (URI redaction
/// covers endpoint URIs, NOT payloads). Enabling .Replayable() accepts that; storing or
/// exposing snapshots is the platform's responsibility, not the route's.
///
///
public sealed class CheckpointProcessor : IProcessor
{
private readonly string _markerName;
private readonly IProcessor _body;
private readonly ILogger? _logger;
/// The marker's compiled body — the tail re-run on replay.
public IProcessor Body => _body;
/// The save-point name (unique within the route).
public string MarkerName => _markerName;
/// Creates a checkpoint processor wrapping the marker body.
public CheckpointProcessor(string markerName, IProcessor body, ILogger? logger = null)
{
_markerName = markerName ?? throw new ArgumentNullException(nameof(markerName));
_body = body ?? throw new ArgumentNullException(nameof(body));
_logger = logger;
}
///
public Task Process(IExchange exchange, CancellationToken ct = default)
{
try
{
EnsureAttachmentsReplayable(exchange);
// Freeze the state at this marker — deep, isolated (NOT Clone). Last marker wins.
exchange.Properties[RouteCheckpoint.PropertyKey] =
new RouteCheckpoint(exchange.RouteId ?? string.Empty, _markerName, exchange.Snapshot());
}
catch (NotSupportedException ex)
{
// A non-snapshot-able body, or attachments above RedbRoute:Attachments:MaxReplayableBytes, must NEVER break
// the happy path: degrade gracefully — this pass captures no checkpoint (replay simply won't be available
// for it) and processing continues. The exception says which of the two it was.
_logger?.LogWarning(ex,
"Checkpoint '{Marker}' not captured: {Reason} Replay unavailable for this pass.",
_markerName, ex.Message);
}
return _body.Process(exchange, ct);
}
// A replay snapshot copies attachment content into memory, so it keeps at most RedbRoute:Attachments:MaxReplayableBytes
// of it (the Tsak DLQ stores this checkpoint, so the same limit holds there). Checked here, not in Snapshot itself:
// other snapshot users (a deferred transactional write) are not replays and have no such limit.
private static void EnsureAttachmentsReplayable(IExchange exchange)
{
var total = Core.MessageAttachments.TotalLength(exchange.In)
+ (exchange.Out is { } outMessage ? Core.MessageAttachments.TotalLength(outMessage) : 0);
if (total == 0)
return;
var limit = Extensions.RouteContextExtensions.GetAttachmentOptions(exchange.Context).MaxReplayableBytes;
if (total > limit)
throw new NotSupportedException(
$"the attachments of the exchange hold {total} bytes, more than RedbRoute:Attachments:MaxReplayableBytes ({limit}).");
}
}