private async Task BatchSendAsync()
{
while (IsNotDisposedOrHasMessagesToProcess())
{
List<TopicMessage> batch = null;
try
{
try
{
await _asyncCollection.OnHasDataAvailable(_stopToken.Token).ConfigureAwait(false);
batch = await _asyncCollection.TakeAsync(BatchSize, BatchDelayTime, _stopToken.Token).ConfigureAwait(false);
}
catch (OperationCanceledException ex)
{
//TODO log that the operation was canceled, this only happens during a dispose
}
if (_asyncCollection.IsCompleted && _asyncCollection.Count > 0)
{
batch = batch ?? new List<TopicMessage>(_asyncCollection.Count);
//Drain any messages remaining in the queue and add them to the send batch
batch.AddRange(_asyncCollection.Drain());
}
if (batch != null)
await ProduceAndSendBatchAsync(batch, _stopToken.Token).ConfigureAwait(false);
}
catch (Exception ex)
{
if (batch != null)
{
batch.ForEach(x => x.Tcs.TrySetException(ex));
}
}
}
}