Skip to content
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
7 changes: 5 additions & 2 deletions src/ObjectPool/src/DefaultPooledObjectPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ public override T Create()
/// <inheritdoc />
public override bool Return(T obj)
{
// DefaultObjectPool<T> doesn't call 'Return' for the default policy.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, this comment became untrue when I revamped the implementation of DefaultObjectPool a little while ago.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#45251 for reference.

// So take care adding any logic to this method, as it might require changes elsewhere.
if (obj is IResettable resettable)
{
return resettable.TryReset();
}

return true;
}
}
19 changes: 19 additions & 0 deletions src/ObjectPool/src/IResettable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Extensions.ObjectPool;

/// <summary>
/// Defines a method to reset an object to its initial state.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Defines a method to reset an object to its initial state.
/// Defines a method to reset an object to its initial state. This is used by <see cref="DefaultPooledObjectPolicy{T}"/> if implemented.

While others are free to call it, I think it's worth pointing out that this is the only thing that calls it by default.

/// </summary>
public interface IResettable
{
/// <summary>
/// Reset the object to a neutral state, semantically similar to when the object was first constructed.
/// </summary>
/// <returns><see langword="true" /> if the object was able to reset itself, otherwise <see langword="false" />.</returns>
/// <remarks>
/// In general, this method is not expected to be thread-safe.
/// </remarks>
bool TryReset();
}
2 changes: 2 additions & 0 deletions src/ObjectPool/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
#nullable enable
Microsoft.Extensions.ObjectPool.IResettable
Microsoft.Extensions.ObjectPool.IResettable.TryReset() -> bool
28 changes: 28 additions & 0 deletions src/ObjectPool/test/DefaultObjectPoolTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,34 @@ public void DefaultObjectPool_Return_RejectedByPolicy()
Assert.NotSame(list1, list2);
}

[Fact]
public static void DefaultObjectPool_Honors_IResettable()
{
var p = new DefaultObjectPool<Resettable>(new DefaultPooledObjectPolicy<Resettable>());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

Suggested change
var p = new DefaultObjectPool<Resettable>(new DefaultPooledObjectPolicy<Resettable>());
var p = ObjectPool.Create<Resettable>();

var r = new Resettable();

r.ResetReturnValue = false;
p.Return(r);
Assert.Equal(1, r.ResetCallCount);
Assert.NotSame(r, p.Get());

r.ResetReturnValue = true;
p.Return(r);
Assert.Equal(2, r.ResetCallCount);
Assert.Same(r, p.Get());
}

private sealed class Resettable : IResettable
{
public int ResetCallCount { get; set; }
public bool ResetReturnValue { get; set; }
public bool TryReset()
{
ResetCallCount++;
return ResetReturnValue;
}
}

private class ListPolicy : IPooledObjectPolicy<List<int>>
{
public List<int> Create()
Expand Down