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

Simpler func for SimpleObjectPool #109

Merged
merged 4 commits into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/JustEat.StatsD.Tests/Extensions/TimingConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public static class TimingConstants
public const int DelayMilliseconds = 500;

public static readonly TimeSpan DeltaFast = TimeSpan.FromMilliseconds(DelayMilliseconds / 4);
public static readonly TimeSpan DeltaSlow = TimeSpan.FromMilliseconds(DelayMilliseconds * 2.5);
public static readonly TimeSpan DeltaSlow = TimeSpan.FromMilliseconds(DelayMilliseconds * 3);
}
}
34 changes: 17 additions & 17 deletions src/JustEat.StatsD/ConnectedSocketPool.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;

namespace JustEat.StatsD
{
Expand All @@ -13,23 +12,24 @@ internal sealed class ConnectedSocketPool : IDisposable
public ConnectedSocketPool(IPEndPoint ipEndPoint)
{
IpEndPoint = ipEndPoint;

_pool = new SimpleObjectPool<Socket>(
Environment.ProcessorCount,
pool =>
{
var socket = UdpTransport.CreateSocket();
try
{
socket.Connect(ipEndPoint);
return socket;
}
catch
{
socket.Dispose();
throw;
}
});
() => CreateSocket(ipEndPoint),
Environment.ProcessorCount);
}

private static Socket CreateSocket(IPEndPoint ipEndPoint)
{
var socket = UdpTransport.CreateSocket();
try
{
socket.Connect(ipEndPoint);
return socket;
}
catch
{
socket.Dispose();
throw;
}
}

public IPEndPoint IpEndPoint { get; }
Expand Down
18 changes: 9 additions & 9 deletions src/JustEat.StatsD/SimpleObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace JustEat.StatsD
/// </summary>
public sealed class SimpleObjectPool<T> where T : class
Copy link
Member

Choose a reason for hiding this comment

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

We can make this internal now too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, done

{
private readonly Func<SimpleObjectPool<T>, T> _constructor;
private readonly Func<T> _itemConstructor;
private readonly ConcurrentBag<T> _pool;

/// <summary> Constructor that populates a pool with the given number of items. </summary>
/// <exception cref="ArgumentNullException"> Thrown when the constructor is null. </exception>
/// <summary>Constructor that populates a pool with the given number of items. </summary>
/// <exception cref="ArgumentNullException"> Thrown when the itemConstructor is null. </exception>
/// <param name="itemConstructor"> The factory method used to create new instances of the object to populate the pool. </param>
/// <param name="initialSize"> Number of items in the pool at start </param>
/// <param name="constructor"> The factory method used to create new instances of the object to populate the pool. </param>
public SimpleObjectPool(int initialSize, Func<SimpleObjectPool<T>, T> constructor)
public SimpleObjectPool(Func<T> itemConstructor, int initialSize = 0)
{
_constructor = constructor ?? throw new ArgumentNullException(nameof(constructor));
_itemConstructor = itemConstructor ?? throw new ArgumentNullException(nameof(itemConstructor));
_pool = new ConcurrentBag<T>();
PrePopulate(initialSize);
}
Expand All @@ -26,10 +26,10 @@ private void PrePopulate(int size)
{
while (Count < size)
{
var instance = _constructor(this);
var instance = _itemConstructor();
if (instance == null)
{
throw new InvalidOperationException("constructor produced null object");
throw new InvalidOperationException("itemConstructor produced null object");
}

_pool.Add(instance);
Expand All @@ -56,7 +56,7 @@ public T Pop()
/// <returns>An object from the pool. </returns>
internal T PopOrCreate()
{
return Pop() ?? _constructor(this);
return Pop() ?? _itemConstructor();
}

/// <summary> Pushes an object back into the pool. </summary>
Expand Down