Skip to content

Commit 28d32c3

Browse files
authored
Merge pull request #133 from cnblogs/avoid-long-waiting
Avoid long waiting
2 parents 575f5db + 807ee6d commit 28d32c3

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

Enyim.Caching/Memcached/MemcachedNode.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class MemcachedNode : IMemcachedNode
2929
private bool isDisposed;
3030

3131
private readonly EndPoint _endPoint;
32-
private readonly ISocketPoolConfiguration config;
32+
private readonly ISocketPoolConfiguration _config;
3333
private InternalPoolImpl internalPoolImpl;
3434
private bool isInitialized = false;
3535
private SemaphoreSlim poolInitSemaphore = new SemaphoreSlim(1, 1);
@@ -42,7 +42,7 @@ public MemcachedNode(
4242
{
4343
_endPoint = endpoint;
4444
EndPointString = endpoint?.ToString().Replace("Unspecified/", string.Empty);
45-
this.config = socketPoolConfig;
45+
_config = socketPoolConfig;
4646

4747
if (socketPoolConfig.ConnectionTimeout.TotalMilliseconds >= Int32.MaxValue)
4848
throw new InvalidOperationException("ConnectionTimeout must be < Int32.MaxValue");
@@ -65,7 +65,7 @@ public MemcachedNode(
6565

6666
protected INodeFailurePolicy FailurePolicy
6767
{
68-
get { return this.failurePolicy ?? (this.failurePolicy = this.config.FailurePolicyFactory.Create(this)); }
68+
get { return this.failurePolicy ?? (this.failurePolicy = _config.FailurePolicyFactory.Create(this)); }
6969
}
7070

7171
/// <summary>
@@ -121,7 +121,7 @@ public bool Ping()
121121
// it's easier to create a new pool than reinitializing a dead one
122122
// rewrite-then-dispose to avoid a race condition with Acquire (which does no locking)
123123
var oldPool = this.internalPoolImpl;
124-
var newPool = new InternalPoolImpl(this, this.config, _logger);
124+
var newPool = new InternalPoolImpl(this, _config, _logger);
125125

126126
Interlocked.Exchange(ref this.internalPoolImpl, newPool);
127127

@@ -774,7 +774,7 @@ protected internal virtual PooledSocket CreateSocket()
774774
{
775775
try
776776
{
777-
var ps = new PooledSocket(_endPoint, this.config.ConnectionTimeout, this.config.ReceiveTimeout, _logger);
777+
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger);
778778
ps.Connect();
779779
return ps;
780780
}
@@ -790,7 +790,7 @@ protected internal virtual async Task<PooledSocket> CreateSocketAsync()
790790
{
791791
try
792792
{
793-
var ps = new PooledSocket(_endPoint, this.config.ConnectionTimeout, this.config.ReceiveTimeout, _logger);
793+
var ps = new PooledSocket(_endPoint, _config.ConnectionTimeout, _config.ReceiveTimeout, _logger);
794794
await ps.ConnectAsync();
795795
return ps;
796796
}
@@ -872,11 +872,26 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
872872
var b = op.GetBuffer();
873873

874874
_logger.LogDebug("pooledSocket.WriteAsync...");
875-
await pooledSocket.WriteAsync(b);
875+
876+
var writeSocketTask = pooledSocket.WriteAsync(b);
877+
if(await Task.WhenAny(writeSocketTask, Task.Delay(_config.ConnectionTimeout)) != writeSocketTask)
878+
{
879+
result.Fail("Timeout to pooledSocket.WriteAsync");
880+
return result;
881+
}
882+
await writeSocketTask;
876883

877884
//if Get, call BinaryResponse
878885
_logger.LogDebug($"{op}.ReadResponseAsync...");
879-
var readResult = await op.ReadResponseAsync(pooledSocket);
886+
887+
var readResponseTask = op.ReadResponseAsync(pooledSocket);
888+
if (await Task.WhenAny(readResponseTask, Task.Delay(_config.ConnectionTimeout)) != readResponseTask)
889+
{
890+
result.Fail($"Timeout to ReadResponseAsync(pooledSocket) for {op}");
891+
return result;
892+
}
893+
894+
var readResult = await readResponseTask;
880895
if (readResult.Success)
881896
{
882897
result.Pass();
@@ -974,12 +989,12 @@ bool IMemcachedNode.Ping()
974989

975990
IOperationResult IMemcachedNode.Execute(IOperation op)
976991
{
977-
return this.ExecuteOperation(op);
992+
return ExecuteOperation(op);
978993
}
979994

980995
async Task<IOperationResult> IMemcachedNode.ExecuteAsync(IOperation op)
981996
{
982-
return await this.ExecuteOperationAsync(op);
997+
return await ExecuteOperationAsync(op);
983998
}
984999

9851000
async Task<bool> IMemcachedNode.ExecuteAsync(IOperation op, Action<bool> next)

0 commit comments

Comments
 (0)