Skip to content

Commit 8b145af

Browse files
committed
Use ThrottlingFailurePolicy as default failure policy
1 parent bfdcdf8 commit 8b145af

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

Enyim.Caching/Configuration/MemcachedClientConfiguration.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ public MemcachedClientConfiguration(
8282
_logger.LogInformation($"{nameof(SocketPool.QueueTimeout)}: {SocketPool.QueueTimeout}");
8383

8484
SocketPool.InitPoolTimeout = options.SocketPool.InitPoolTimeout;
85+
86+
SocketPool.FailurePolicyFactory = options.SocketPool.FailurePolicyFactory;
8587
}
8688

8789
Protocol = options.Protocol;

Enyim.Caching/Configuration/MemcachedClientOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class SocketPoolOptions
6969
public TimeSpan DeadTimeout { get; set; } = new TimeSpan(0, 0, 10);
7070
public TimeSpan QueueTimeout { get; set; } = new TimeSpan(0, 0, 0, 0, 100);
7171
public TimeSpan InitPoolTimeout { get; set; } = new TimeSpan(0, 1, 0);
72+
public INodeFailurePolicyFactory FailurePolicyFactory { get; set; } = new ThrottlingFailurePolicyFactory(5, TimeSpan.FromMilliseconds(2000));
7273

7374
public void CheckPoolSize()
7475
{

Enyim.Caching/Configuration/SocketPoolConfiguration.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class SocketPoolConfiguration : ISocketPoolConfiguration
1515
private TimeSpan deadTimeout = new TimeSpan(0, 0, 10);
1616
private TimeSpan queueTimeout = new TimeSpan(0, 0, 0, 0, 100);
1717
private TimeSpan _initPoolTimeout = new TimeSpan(0, 1, 0);
18-
private INodeFailurePolicyFactory policyFactory = new FailImmediatelyPolicyFactory();
18+
private INodeFailurePolicyFactory FailurePolicyFactory = new ThrottlingFailurePolicyFactory(5, TimeSpan.FromMilliseconds(2000));
1919

2020
int ISocketPoolConfiguration.MinPoolSize
2121
{
@@ -96,13 +96,13 @@ TimeSpan ISocketPoolConfiguration.DeadTimeout
9696

9797
INodeFailurePolicyFactory ISocketPoolConfiguration.FailurePolicyFactory
9898
{
99-
get { return this.policyFactory; }
99+
get { return this.FailurePolicyFactory; }
100100
set
101101
{
102102
if (value == null)
103103
throw new ArgumentNullException("value");
104104

105-
this.policyFactory = value;
105+
this.FailurePolicyFactory = value;
106106
}
107107
}
108108
}

Enyim.Caching/Memcached/MemcachedNode.cs

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public bool IsAlive
8787

8888
/// <summary>
8989
/// Gets a value indicating whether the server is working or not.
90-
///
90+
///
9191
/// If the server is back online, we'll ercreate the internal socket pool and mark the server as alive so operations can target it.
9292
/// </summary>
9393
/// <returns>true if the server is alive; false otherwise.</returns>
@@ -263,7 +263,7 @@ private class InternalPoolImpl : IDisposable
263263
/// <summary>
264264
/// A list of already connected but free to use sockets
265265
/// </summary>
266-
private InterlockedStack<PooledSocket> freeItems;
266+
private InterlockedStack<PooledSocket> _freeItems;
267267

268268
private bool isDisposed;
269269
private bool isAlive;
@@ -300,7 +300,7 @@ internal InternalPoolImpl(
300300
this.maxItems = config.MaxPoolSize;
301301

302302
_semaphore = new SemaphoreSlim(maxItems, maxItems);
303-
this.freeItems = new InterlockedStack<PooledSocket>();
303+
_freeItems = new InterlockedStack<PooledSocket>();
304304

305305
_logger = logger;
306306
_isDebugEnabled = _logger.IsEnabled(LogLevel.Debug);
@@ -314,7 +314,14 @@ internal void InitPool()
314314
{
315315
for (int i = 0; i < this.minItems; i++)
316316
{
317-
this.freeItems.Push(this.CreateSocket());
317+
try
318+
{
319+
_freeItems.Push(CreateSocket());
320+
}
321+
catch (Exception ex)
322+
{
323+
_logger.LogError(ex, $"Failed to put {nameof(PooledSocket)} {i} in Pool");
324+
}
318325

319326
// cannot connect to the server
320327
if (!this.isAlive)
@@ -342,7 +349,14 @@ internal async Task InitPoolAsync()
342349
{
343350
for (int i = 0; i < this.minItems; i++)
344351
{
345-
this.freeItems.Push(await this.CreateSocketAsync());
352+
try
353+
{
354+
_freeItems.Push(await CreateSocketAsync());
355+
}
356+
catch(Exception ex)
357+
{
358+
_logger.LogError(ex, $"Failed to put {nameof(PooledSocket)} {i} in Pool");
359+
}
346360

347361
// cannot connect to the server
348362
if (!this.isAlive)
@@ -432,7 +446,7 @@ public IPooledSocketResult Acquire()
432446
}
433447

434448
// do we have free items?
435-
if (this.freeItems.TryPop(out retval))
449+
if (_freeItems.TryPop(out retval))
436450
{
437451
#region [ get it from the pool ]
438452

@@ -538,7 +552,7 @@ public async Task<IPooledSocketResult> AcquireAsync()
538552
}
539553

540554
// do we have free items?
541-
if (this.freeItems.TryPop(out retval))
555+
if (_freeItems.TryPop(out retval))
542556
{
543557
#region [ get it from the pool ]
544558

@@ -643,7 +657,7 @@ private void ReleaseSocket(PooledSocket socket)
643657
try
644658
{
645659
// mark the item as free
646-
this.freeItems.Push(socket);
660+
_freeItems.Push(socket);
647661
}
648662
finally
649663
{
@@ -679,7 +693,7 @@ private void ReleaseSocket(PooledSocket socket)
679693
{
680694
try
681695
{
682-
// one of our previous sockets has died, so probably all of them
696+
// one of our previous sockets has died, so probably all of them
683697
// are dead. so, kill the socket (this will eventually clear the pool as well)
684698
socket.Destroy();
685699
}
@@ -716,7 +730,7 @@ public void Dispose()
716730

717731
PooledSocket ps;
718732

719-
while (this.freeItems.TryPop(out ps))
733+
while (_freeItems.TryPop(out ps))
720734
{
721735
try { ps.Destroy(); }
722736
catch { }
@@ -725,7 +739,7 @@ public void Dispose()
725739
this.ownerNode = null;
726740
_semaphore.Dispose();
727741
_semaphore = null;
728-
this.freeItems = null;
742+
_freeItems = null;
729743
}
730744
}
731745

@@ -904,7 +918,7 @@ protected virtual async Task<bool> ExecuteOperationAsync(IOperation op, Action<b
904918
var socket = (await this.AcquireAsync()).Value;
905919
if (socket == null) return false;
906920

907-
//key(string) to buffer(btye[])
921+
//key(string) to buffer(btye[])
908922
var b = op.GetBuffer();
909923

910924
try
@@ -982,20 +996,20 @@ event Action<IMemcachedNode> IMemcachedNode.Failed
982996

983997
#region [ License information ]
984998
/* ************************************************************
985-
*
999+
*
9861000
* Copyright (c) 2010 Attila Kisk? enyim.com
987-
*
1001+
*
9881002
* Licensed under the Apache License, Version 2.0 (the "License");
9891003
* you may not use this file except in compliance with the License.
9901004
* You may obtain a copy of the License at
991-
*
1005+
*
9921006
* http://www.apache.org/licenses/LICENSE-2.0
993-
*
1007+
*
9941008
* Unless required by applicable law or agreed to in writing, software
9951009
* distributed under the License is distributed on an "AS IS" BASIS,
9961010
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9971011
* See the License for the specific language governing permissions and
9981012
* limitations under the License.
999-
*
1013+
*
10001014
* ************************************************************/
10011015
#endregion

0 commit comments

Comments
 (0)