Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

Commit

Permalink
timed object pool is ok
Browse files Browse the repository at this point in the history
  • Loading branch information
pomma89 committed Apr 7, 2017
1 parent 72aa9fd commit 616be1a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
3 changes: 1 addition & 2 deletions src/CodeProject.ObjectPool/Core/PooledObjectBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ public int Count
/// <returns>An enumerator that can be used to iterate through the collection.</returns>
public IEnumerator<T> GetEnumerator()
{
for (var i = 0; i <= _pooledObjects.Length; ++i)
foreach (var item in _pooledObjects)
{
var item = _pooledObjects[i];
if (item != null)
{
yield return item;
Expand Down
7 changes: 7 additions & 0 deletions src/CodeProject.ObjectPool/ITimedObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
// OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;

namespace CodeProject.ObjectPool
{
/// <summary>
Expand All @@ -38,5 +40,10 @@ public interface ITimedObjectPool<out T> : IObjectPool<T>
#endif
where T : PooledObject
{
/// <summary>
/// When pooled objects have not been used for a time greater than <see cref="Timeout"/>,
/// then they will be destroyed by a cleaning task.
/// </summary>
TimeSpan Timeout { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/CodeProject.ObjectPool/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ void IObjectPoolHandle.ReturnObjectToPool(PooledObject objectToReturnToPool, boo
/// Creates a new pooled object, initializing its info.
/// </summary>
/// <returns>A new pooled object.</returns>
protected T CreatePooledObject()
protected virtual T CreatePooledObject()
{
if (Diagnostics.Enabled)
{
Expand Down
19 changes: 18 additions & 1 deletion src/CodeProject.ObjectPool/TimedObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace CodeProject.ObjectPool
/// The type of the object that which will be managed by the pool. The pooled object have to be
/// a sub-class of PooledObject.
/// </typeparam>
internal class TimedObjectPool<T> : ObjectPool<T>, ITimedObjectPool<T>
public class TimedObjectPool<T> : ObjectPool<T>, ITimedObjectPool<T>
where T : PooledObject
{
#region Fields
Expand Down Expand Up @@ -137,6 +137,23 @@ public TimeSpan Timeout

#region Core Methods

/// <summary>
/// Creates a new pooled object, initializing its info.
/// </summary>
/// <returns>A new pooled object.</returns>
protected override T CreatePooledObject()
{
var pooledObject = base.CreatePooledObject();

// Register an handler which records the time at which the object returned to the pool.
pooledObject.OnResetState += () =>
{
pooledObject.PooledObjectInfo.Payload = DateTime.UtcNow;
};

return pooledObject;
}

/// <summary>
/// Updates the timer according to a new timeout.
/// </summary>
Expand Down
15 changes: 15 additions & 0 deletions test/CodeProject.ObjectPool.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
// OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;
using System.Threading;

namespace CodeProject.ObjectPool.Examples
{
Expand Down Expand Up @@ -60,6 +61,20 @@ private static void Main()
wrapper.InternalResource.DoOtherStuff();
} // Exiting the using scope will return the object back to the pool.

// Creates a pool where objects which have not been used for over 2 seconds will be
// cleaned up by a dedicated thread.
var timedPool = new TimedObjectPool<ExpensiveResource>(TimeSpan.FromSeconds(2));

using (var resource = timedPool.GetObject())
{
// Using the resource...
resource.DoStuff();
} // Exiting the using scope will return the object back to the pool and record last usage.

Console.WriteLine($"Timed pool size after 0 seconds: {timedPool.ObjectsInPoolCount}"); // Should be 1
Thread.Sleep(TimeSpan.FromSeconds(4));
Console.WriteLine($"Timed pool size after 4 seconds: {timedPool.ObjectsInPoolCount}"); // Should be 0

Console.Read();
}

Expand Down

0 comments on commit 616be1a

Please sign in to comment.