private async Task Handle(int partitionId, Queue queue) { // using (!_disposed) condition here instead of (true) means // that on shutdown queued events will be dropped - otherwise, // we could have a ton of events to process and shutting down // would take time - TODO: is this the right decision? while (!_disposed) { if (!queue.TryDequeue(out var eventData)) { lock (_mutex) { if (!queue.TryDequeue(out eventData)) { HConsole.WriteLine(this, $"Release queue:{partitionId}"); _queues.Remove(partitionId); queue.Task = null; _pool.Return(queue); return; } } } await Handle(eventData).CfAwait(); // does not throw } }
private async Task Handle(int partitionId, Queue queue) { while (true) { if (!queue.TryDequeue(out var eventData)) { lock (_mutex) { if (!queue.TryDequeue(out eventData)) { _queues.Remove(partitionId); queue.Task = null; _pool.Return(queue); return; } } } await Handle(eventData); // does not throw } }