Path: redb-route/redb.Route/Core/DefaultInflightRepository.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/DefaultInflightRepository.cs ---- using System.Collections.Concurrent; using redb.Route.Abstractions; namespace redb.Route.Core; /// /// Thread-safe in-memory implementation of . /// Uses ConcurrentDictionary for O(1) register/unregister and per-route counting. /// /// /// An exchange is registered once per entry into a route, and a copy keeps the exchange id: an exchange entering a /// route again (a route calling itself, parallel branches through one sub-route) registers the same id more than once. /// The entries are counted, so the exchange stays in flight until the last of them has finished; the entry shown is /// the first one. /// public sealed class DefaultInflightRepository : IInflightRepository { private sealed record Tracked(InflightExchange Entry, int Entries); private readonly ConcurrentDictionary _entries = new(); /// public void Register(InflightExchange entry) { ArgumentNullException.ThrowIfNull(entry); _entries.AddOrUpdate(entry.ExchangeId, static (_, added) => new Tracked(added, 1), static (_, tracked, _) => tracked with { Entries = tracked.Entries + 1 }, entry); } /// public void Unregister(string exchangeId) { while (_entries.TryGetValue(exchangeId, out var tracked)) { if (tracked.Entries <= 1) { if (_entries.TryRemove(new KeyValuePair(exchangeId, tracked))) return; } else if (_entries.TryUpdate(exchangeId, tracked with { Entries = tracked.Entries - 1 }, tracked)) { return; } // Raced with another entry of the same exchange: read it again. } } /// public IReadOnlyList Browse() => _entries.Values.Select(t => t.Entry).ToList(); /// public IReadOnlyList Browse(string routeId) => _entries.Values.Select(t => t.Entry).Where(e => e.RouteId == routeId).ToList(); /// public int Count => _entries.Count; /// public int CountByRoute(string routeId) => _entries.Values.Count(t => t.Entry.RouteId == routeId); /// public IDisposable BeginWait(string exchangeId, TimeSpan delay) { ArgumentNullException.ThrowIfNull(exchangeId); var until = DateTime.UtcNow + delay; Update(exchangeId, e => e with { WaitingUntil = until }); return new Wait(this, exchangeId); } private void Update(string exchangeId, Func change) { while (_entries.TryGetValue(exchangeId, out var tracked)) { if (_entries.TryUpdate(exchangeId, tracked with { Entry = change(tracked.Entry) }, tracked)) return; // Raced with another entry of the same exchange: read it again. } } private sealed class Wait(DefaultInflightRepository owner, string exchangeId) : IDisposable { private int _disposed; public void Dispose() { if (Interlocked.Exchange(ref _disposed, 1) == 0) owner.Update(exchangeId, e => e with { WaitingUntil = null, LastWaitEndedAt = DateTime.UtcNow }); } } }