Path: redb-route/redb.Route/Batching/BatchCollector.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/Batching/BatchCollector.cs ---- namespace redb.Route.Batching; /// /// Receives up to messages into , waiting at most /// for the first of them, and returns how many it added. A transport that hands out one /// message at a time adds at most one; one that receives several in a call (SQS) adds up to . /// It never adds more than . /// public delegate ValueTask BatchReceive(List into, int max, TimeSpan wait, CancellationToken ct); /// A batch the collector gathered. /// The messages, in the order they were received. /// /// The consumer stopped while the batch was still being collected. Such a batch is not run: the consumer gives its /// messages back to the broker (). /// public readonly record struct CollectedBatch(IReadOnlyList Items, bool Interrupted); /// /// Collects a batch by the contract of : the first message is awaited for /// ; after it the batch grows until it holds /// , until no message came for , or until /// has passed since its first message. One implementation for every broker: the /// transport supplies only . /// public sealed class BatchCollector { private readonly BatchOptions _options; private readonly BatchReceive _receive; private readonly TimeProvider _time; /// Creates a collector over a transport's receive. public BatchCollector(BatchOptions options, BatchReceive receive, TimeProvider? time = null) { _options = options ?? throw new ArgumentNullException(nameof(options)); _receive = receive ?? throw new ArgumentNullException(nameof(receive)); _time = time ?? TimeProvider.System; } /// /// Collects one batch. Returns an empty batch when no message came within , /// so the caller's loop gets to look at its stop token; returns the messages received so far, marked /// , when fires mid-batch. /// public async Task> CollectAsync(CancellationToken stop) { var items = new List(Math.Min(_options.MaxMessages, 1024)); if (stop.IsCancellationRequested) return new(items, Interrupted: false); try { await _receive(items, _options.MaxMessages, _options.PollTimeout, stop).ConfigureAwait(false); } catch (OperationCanceledException) when (stop.IsCancellationRequested) { return new(items, Interrupted: items.Count > 0); } if (items.Count == 0) return new(items, Interrupted: false); var now = _time.GetUtcNow(); var hardDeadline = _options.Interval is { } interval ? now + interval : DateTimeOffset.MaxValue; var idleDeadline = now + _options.PollTimeout; while (items.Count < _options.MaxMessages) { if (stop.IsCancellationRequested) return new(items, Interrupted: true); now = _time.GetUtcNow(); var deadline = idleDeadline < hardDeadline ? idleDeadline : hardDeadline; var wait = deadline - now; if (wait <= TimeSpan.Zero) break; int added; try { added = await _receive(items, _options.MaxMessages - items.Count, wait, stop).ConfigureAwait(false); } catch (OperationCanceledException) when (stop.IsCancellationRequested) { return new(items, Interrupted: true); } if (added > 0) idleDeadline = _time.GetUtcNow() + _options.PollTimeout; } return new(items, Interrupted: false); } }