public async Task Flush(IReceivableSourceBlock <T> source, CancellationToken cancellationToken) { while (!source.Completion.IsCompleted) { try { await Task.Delay(this.interval, cancellationToken).ConfigureAwait(false); } catch (OperationCanceledException) { source.Complete(); } if (source.TryReceiveAll(out var items)) { var itemOffsets = Enumerable.Range(0, items.Count) .Select(_ => new List <TopicPartitionOffset>()) .ToArray(); IEnumerable <TopicPartitionOffset> commitOffsets; if (this.offsetSource == null) { commitOffsets = Array.Empty <TopicPartitionOffset>(); } else { var commitPositions = new Dictionary <TopicPartition, Offset>(); for (var i = 0; i < items.Count; i++) { var n = this.offsetMapping?.Invoke(items[i]) ?? 1; while (n-- > 0) { if (!this.offsetSource.TryTake(out var tpo)) { throw new InvalidOperationException("No offset stored!"); } itemOffsets[i].Add(tpo); commitPositions[tpo.TopicPartition] = tpo.Offset; } } commitOffsets = commitPositions.Select( kvp => new TopicPartitionOffset(kvp.Key, kvp.Value + 1)); } var published = await this.transactor.Send(items, commitOffsets).ConfigureAwait(false); foreach (var kvp in published) { itemOffsets[kvp.Key].Add(kvp.Value); } for (var i = 0; i < items.Count; i++) { this.OnDelivered?.Invoke(items[i], itemOffsets[i]); } } } }
// Signals to this target block that it should not accept any more messages, // nor consume postponed messages. public void Complete() { _msource.Complete(); }
public void Complete() { _markedForCompletion = true; _source.Complete(); }