Path: redb-route/redb.Route.As4/Signals/RecentReceipts.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.As4/Signals/RecentReceipts.cs
----
using System.Collections.Concurrent;
using redb.Route.Abstractions;
namespace redb.Route.As4.Signals;
///
/// The receipts a node sent, kept in memory to answer duplicates with the same receipt. Every receipt is stored
/// and almost none is read again, so entries leave by age (retention) and by count (the oldest beyond
/// capacity) on every store, not on a read that never comes.
///
internal sealed class RecentReceipts(TimeSpan retention, int capacity) : IClaimCheckRepository
{
private readonly ConcurrentDictionary _entries = new(StringComparer.Ordinal);
private readonly ConcurrentQueue<(string Key, Entry Entry)> _order = new();
private readonly object _trim = new();
public Task Store(ReadOnlyMemory data, TimeSpan? ttl = null, CancellationToken ct = default) =>
throw new NotSupportedException("Receipts are stored under the id of the message they answer.");
public Task Store(string key, ReadOnlyMemory data, TimeSpan? ttl = null, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(key);
var entry = new Entry(data.ToArray(), DateTimeOffset.UtcNow + (ttl ?? retention));
_entries[key] = entry;
_order.Enqueue((key, entry));
Trim();
return Task.CompletedTask;
}
public Task Retrieve(string key, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(key);
return Task.FromResult(_entries.TryGetValue(key, out var entry) && entry.ExpiresAt > DateTimeOffset.UtcNow
? entry.Data
: null);
}
public async Task RetrieveAndRemove(string key, CancellationToken ct = default)
{
var data = await Retrieve(key, ct).ConfigureAwait(false);
await Remove(key, ct).ConfigureAwait(false);
return data;
}
public Task Remove(string key, CancellationToken ct = default)
{
ArgumentNullException.ThrowIfNull(key);
_entries.TryRemove(key, out _);
return Task.CompletedTask;
}
/// Drops from the oldest end what has expired or is beyond the capacity.
private void Trim()
{
lock (_trim)
{
var now = DateTimeOffset.UtcNow;
while (_order.TryPeek(out var oldest) && (oldest.Entry.ExpiresAt <= now || _order.Count > capacity))
{
_order.TryDequeue(out _);
// Only the entry this queue item stored: a later Store under the same key has its own item.
_entries.TryRemove(new KeyValuePair(oldest.Key, oldest.Entry));
}
}
}
private sealed record Entry(byte[] Data, DateTimeOffset ExpiresAt);
}