Open
Description
Background and Motivation
It would be nice to be able to emit a metric anytime the DefaultObjectPool limit is breached. Currently this is not possible because the bool ReturnCore
is marked private protected
so it cannot be overridden to get the bool
response from this method. If the _numbItems was encapsulated as a Property {get}, than it would be possible to determine if the pool is full.
Proposed API
public class DefaultObjectPool<T> : ObjectPool<T> where T : class
{
private readonly Func<T> _createFunc;
private readonly Func<T, bool> _returnFunc;
private readonly int _maxCapacity;
private int _numItems;
private protected readonly ConcurrentQueue<T> _items = new();
private protected T? _fastItem;
+ public int ItemCount => _numItems;
Usage Examples
// return item to the pool:
pool.Return(item);
// is it full?
if (pool.ItemCount == _maximumRetained)
{
// emit metric for tracking
}
Alternative Designs
- Remove
private
from theprivate protected bool ReturnCore
so it can be overridden, call the base class to get the boolean response and emit the metric there. - Change
ObjectPool<T>.void Return(T obj)
toObjectPool<T>.bool Return(T obj)
Risks
The least risk is to expose the int
property so that the underlying APIs to need to change. I don't foresee any risks by exposing the ItemCount
property.