Path: redb-route/redb.Route.As2/As2CorrelationStore.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.As2/As2CorrelationStore.cs
----
using System.Collections.Concurrent;
using redb.Route.As2.Crypto;
namespace redb.Route.As2;
///
/// Correlates asynchronous AS2 MDNs with the messages that requested them: a producer sending in async mode records
/// the outgoing Message-ID and the MIC it expects back; when the partner later POSTs the MDN to our receiver, the MIC
/// is looked up by Original-Message-ID and compared. Nothing waits on an entry — the verdict reaches the
/// ReceiveMdn route as its own exchange. In-memory and per-process: after a restart, past the TTL, or on another
/// node an MDN finds no entry and its MIC status is unknown (see ).
///
internal sealed class As2CorrelationStore : IDisposable
{
private sealed record Pending(As2Mic ExpectedMic, DateTimeOffset RegisteredAt);
private readonly ConcurrentDictionary _pending = new(StringComparer.Ordinal);
private readonly Timer _sweepTimer;
private readonly TimeSpan _maxAge;
/// Evict pending entries older than this (default 30 min).
/// How often to run the sweep (default 5 min).
public As2CorrelationStore(TimeSpan? maxAge = null, TimeSpan? sweepInterval = null)
{
_maxAge = maxAge ?? TimeSpan.FromMinutes(30);
var interval = sweepInterval ?? TimeSpan.FromMinutes(5);
// Periodic eviction: a partner that never delivers a promised async MDN would otherwise leave one entry per
// message forever.
_sweepTimer = new Timer(static s => ((As2CorrelationStore)s!).Sweep(((As2CorrelationStore)s!)._maxAge),
this, interval, interval);
}
///
public void Dispose() => _sweepTimer.Dispose();
/// Records an outgoing message awaiting an async MDN, with the MIC the MDN must carry.
public void Register(string messageId, As2Mic expectedMic) =>
_pending[messageId] = new Pending(expectedMic, DateTimeOffset.UtcNow);
/// The MIC expected for a pending message, or null if unknown.
public As2Mic? ExpectedMic(string messageId)
=> _pending.TryGetValue(messageId, out var pending) ? pending.ExpectedMic : null;
/// Removes the entry of a message whose MDN has been accepted. False if there was none.
public bool Complete(string messageId) => _pending.TryRemove(messageId, out _);
/// Evicts pending entries older than ; returns how many.
public int Sweep(TimeSpan maxAge)
{
var cutoff = DateTimeOffset.UtcNow - maxAge;
var removed = 0;
foreach (var (key, pending) in _pending)
if (pending.RegisteredAt < cutoff && _pending.TryRemove(key, out _))
removed++;
return removed;
}
}