Skip to content

Surface the _numItems as a public property on DefaultObjectPool #61029

Open
@Shazwazza

Description

@Shazwazza

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 the private 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) to ObjectPool<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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviewsneeds-area-labelUsed by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions