dotnet / dotnet/aspnetcore

Surface the _numItems as a public property on DefaultObjectPool

Open
#61,029 3 comments 0 reactions 0 assignees View on GitHub
api-proposal api-suggestion area-networking
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

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

```diff
public class DefaultObjectPool : ObjectPool where T : class
{
private readonly Func _createFunc;
private readonly Func _returnFunc;
private readonly int _maxCapacity;
private int _numItems;

private protected readonly ConcurrentQueue _items = new();
private protected T? _fastItem;

+ public int ItemCount => _numItems;
```

## Usage Examples

```csharp
// 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.void Return(T obj)` to `ObjectPool.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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.