Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean the Polly.Utils.ObjectPool class #2312

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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