diff --git a/src/JustEat.StatsD.Tests/Extensions/TimingConstants.cs b/src/JustEat.StatsD.Tests/Extensions/TimingConstants.cs index 38babf7e..e25c50a2 100644 --- a/src/JustEat.StatsD.Tests/Extensions/TimingConstants.cs +++ b/src/JustEat.StatsD.Tests/Extensions/TimingConstants.cs @@ -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); } } diff --git a/src/JustEat.StatsD/ConnectedSocketPool.cs b/src/JustEat.StatsD/ConnectedSocketPool.cs index 59d59a32..0807b50b 100644 --- a/src/JustEat.StatsD/ConnectedSocketPool.cs +++ b/src/JustEat.StatsD/ConnectedSocketPool.cs @@ -1,7 +1,6 @@ using System; using System.Net; using System.Net.Sockets; -using System.Runtime.CompilerServices; namespace JustEat.StatsD { @@ -13,23 +12,24 @@ internal sealed class ConnectedSocketPool : IDisposable public ConnectedSocketPool(IPEndPoint ipEndPoint) { IpEndPoint = ipEndPoint; - _pool = new SimpleObjectPool( - 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; } diff --git a/src/JustEat.StatsD/SimpleObjectPool.cs b/src/JustEat.StatsD/SimpleObjectPool.cs index 7b4dfbd7..30129ff3 100644 --- a/src/JustEat.StatsD/SimpleObjectPool.cs +++ b/src/JustEat.StatsD/SimpleObjectPool.cs @@ -6,18 +6,18 @@ namespace JustEat.StatsD /// /// A class that provides simple thread-safe object pooling semantics. /// - public sealed class SimpleObjectPool where T : class + internal sealed class SimpleObjectPool where T : class { - private readonly Func, T> _constructor; + private readonly Func _itemConstructor; private readonly ConcurrentBag _pool; - /// Constructor that populates a pool with the given number of items. - /// Thrown when the constructor is null. + /// Constructor that populates a pool with the given number of items. + /// Thrown when the itemConstructor is null. + /// The factory method used to create new instances of the object to populate the pool. /// Number of items in the pool at start - /// The factory method used to create new instances of the object to populate the pool. - public SimpleObjectPool(int initialSize, Func, T> constructor) + public SimpleObjectPool(Func itemConstructor, int initialSize = 0) { - _constructor = constructor ?? throw new ArgumentNullException(nameof(constructor)); + _itemConstructor = itemConstructor ?? throw new ArgumentNullException(nameof(itemConstructor)); _pool = new ConcurrentBag(); PrePopulate(initialSize); } @@ -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); @@ -56,7 +56,7 @@ public T Pop() /// An object from the pool. internal T PopOrCreate() { - return Pop() ?? _constructor(this); + return Pop() ?? _itemConstructor(); } /// Pushes an object back into the pool.