AsyncEx
AsyncEx copied to clipboard
Approximate count for the AsyncProducerConsumerQueue
Can we add an "approximate count" property to the AsyncProducerConsumerQueue?
In my usage, the queue often provides a buffer that allows a data source thread to continue operating without waiting for consuming threads to process the data.
This brings up the issue of backpressure monitoring, and I would like the app to be able to report the queue Count as an indicator of app performance.
The implementation could be as simple as Interlocked.Increment(ref count) when enqueing an item, and decrementing when the item is dequeued.
The intention of the property is not for it to be perfectly accurate and synchronized with the reading/writing threads, but for it to give a general idea of how much backpressure exists by the approximate size of the queue.
I'm in the same situation - I need to monitor the number of items in the queue for status reporting. I think it's as simple as adding:
public int Count => _queue.Count;
If I add this, it certainly won't be named Count. ;) That would encourage misuse. I'd probably call it ApproximateCount.