Skip to content

Commit

Permalink
Simplify ObjectPool class (#2312)
Browse files Browse the repository at this point in the history
Remove code only used by tests.
  • Loading branch information
gabidabet authored Sep 26, 2024
1 parent 67f0b17 commit 22d09c0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 13 deletions.
14 changes: 2 additions & 12 deletions src/Polly.Core/Utils/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,15 @@ internal sealed class ObjectPool<T>
{
internal static readonly int MaxCapacity = (Environment.ProcessorCount * 2) - 1; // the - 1 is to account for _fastItem

private readonly Func<ObjectPool<T>, T> _createFunc;
private readonly Func<T> _createFunc;
private readonly Func<T, bool> _returnFunc;

private readonly ConcurrentQueue<T> _items = new();

private T? _fastItem;
private int _numItems;

public ObjectPool(Func<T> createFunc, Action<T> reset)
: this(createFunc, o => { reset(o); return true; })
{
}

public ObjectPool(Func<T> createFunc, Func<T, bool> returnFunc)
: this(_ => createFunc(), returnFunc)
{
}

public ObjectPool(Func<ObjectPool<T>, T> createFunc, Func<T, bool> returnFunc)
{
_createFunc = createFunc;
_returnFunc = returnFunc;
Expand All @@ -42,7 +32,7 @@ public T Get()
}

// no object available, so go get a brand new one
return _createFunc(this);
return _createFunc();
}

return item;
Expand Down
2 changes: 1 addition & 1 deletion test/Polly.Core.Tests/Utils/ObjectPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ObjectPoolTests
public void GetAnd_ReturnObject_SameInstance()
{
// Arrange
var pool = new ObjectPool<object>(() => new object(), _ => { });
var pool = new ObjectPool<object>(() => new object(), _ => true);

var obj1 = pool.Get();
pool.Return(obj1);
Expand Down

0 comments on commit 22d09c0

Please sign in to comment.