Skip to content

Commit

Permalink
Configure await false everywhere(cnblogs#247)
Browse files Browse the repository at this point in the history
refactor: configure await false everywhere
  • Loading branch information
james-fairbairn authored and cw-sanikachavan committed Jan 17, 2025
1 parent 5eb98fa commit 7ba2613
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 103 deletions.
22 changes: 11 additions & 11 deletions src/Enyim.Caching/DistributedCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ byte[] IDistributedCache.Get(string key)

async Task<byte[]> IDistributedCache.GetAsync(string key, CancellationToken token = default)
{
var value = await GetValueAsync<byte[]>(key);
var value = await GetValueAsync<byte[]>(key).ConfigureAwait(false);

if (value != null)
{
await RefreshAsync(key);
await RefreshAsync(key).ConfigureAwait(false);
}

return value;
Expand Down Expand Up @@ -58,17 +58,17 @@ void IDistributedCache.Set(string key, byte[] value, DistributedCacheEntryOption
{
if (!HasSlidingExpiration(options))
{
await PerformStoreAsync(StoreMode.Set, key, value, 0);
await PerformStoreAsync(StoreMode.Set, key, value, 0).ConfigureAwait(false);
return;
}

var expiration = GetExpiration(options);
await PerformStoreAsync(StoreMode.Set, key, value, expiration);
await PerformStoreAsync(StoreMode.Set, key, value, expiration).ConfigureAwait(false);

if (options.SlidingExpiration.HasValue)
{
var sldExp = options.SlidingExpiration.Value;
await AddAsync(GetSlidingExpirationKey(key), sldExp.ToString(), sldExp);
await AddAsync(GetSlidingExpirationKey(key), sldExp.ToString(), sldExp).ConfigureAwait(false);
}
}

Expand Down Expand Up @@ -108,15 +108,15 @@ public void Refresh(string key)
public async Task RefreshAsync(string key, CancellationToken token = default)
{
var sldExpKey = GetSlidingExpirationKey(key);
var sldExpStr = await GetValueAsync<string>(sldExpKey);
var sldExpStr = await GetValueAsync<string>(sldExpKey).ConfigureAwait(false);
if (!string.IsNullOrEmpty(sldExpStr)
&& TimeSpan.TryParse(sldExpStr, out var sldExp))
{
var value = (await GetAsync(key)).Value;
var value = (await GetAsync(key).ConfigureAwait(false)).Value;
if (value != null)
{
await ReplaceAsync(key, value, sldExp);
await ReplaceAsync(sldExpKey, sldExpStr, sldExp);
await ReplaceAsync(key, value, sldExp).ConfigureAwait(false);
await ReplaceAsync(sldExpKey, sldExpStr, sldExp).ConfigureAwait(false);
}
}
}
Expand All @@ -129,8 +129,8 @@ void IDistributedCache.Remove(string key)

async Task IDistributedCache.RemoveAsync(string key, CancellationToken token = default)
{
await RemoveAsync(key);
await RemoveAsync(GetSlidingExpirationKey(key));
await RemoveAsync(key).ConfigureAwait(false);
await RemoveAsync(GetSlidingExpirationKey(key)).ConfigureAwait(false);
}

private uint GetExpiration(DistributedCacheEntryOptions options)
Expand Down
44 changes: 22 additions & 22 deletions src/Enyim.Caching/Memcached/MemcachedNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public async Task<IPooledSocketResult> AcquireAsync()
var result = new PooledSocketResult();
if (!_isInitialized)
{
if (!await poolInitSemaphore.WaitAsync(_initPoolTimeout))
if (!await poolInitSemaphore.WaitAsync(_initPoolTimeout).ConfigureAwait(false))
{
return result.Fail("Timeout to poolInitSemaphore.Wait", _logger) as PooledSocketResult;
}
Expand All @@ -221,7 +221,7 @@ public async Task<IPooledSocketResult> AcquireAsync()
if (!_isInitialized)
{
var startTime = DateTime.Now;
await _internalPoolImpl.InitPoolAsync();
await _internalPoolImpl.InitPoolAsync().ConfigureAwait(false);
_isInitialized = true;

if (_logger.IsEnabled(LogLevel.Information))
Expand All @@ -239,7 +239,7 @@ public async Task<IPooledSocketResult> AcquireAsync()

try
{
return await _internalPoolImpl.AcquireAsync();
return await _internalPoolImpl.AcquireAsync().ConfigureAwait(false);
}
catch (Exception e)
{
Expand Down Expand Up @@ -401,7 +401,7 @@ internal async Task InitPoolAsync()
{
try
{
_freeItems.AddFirst(await CreateSocketAsync());
_freeItems.Push(await CreateSocketAsync().ConfigureAwait(false));
}
catch (Exception ex)
{
Expand Down Expand Up @@ -518,7 +518,7 @@ private async Task ReconcileAsync(CancellationToken cancellationToken)

private async Task<PooledSocket> CreateSocketAsync()
{
var ps = await _ownerNode.CreateSocketAsync();
var ps = await _ownerNode.CreateSocketAsync().ConfigureAwait(false);
ps.CleanupCallback = ReleaseSocket;

return ps;
Expand Down Expand Up @@ -686,7 +686,7 @@ public async Task<IPooledSocketResult> AcquireAsync()

PooledSocket socket = null;

if (!await _semaphore.WaitAsync(_queueTimeout))
if (!await _semaphore.WaitAsync(_queueTimeout).ConfigureAwait(false))
{
message = "Pool is full, timeouting. " + _endPoint;
_logger.LogInformation(message);
Expand Down Expand Up @@ -729,9 +729,9 @@ public async Task<IPooledSocketResult> AcquireAsync()
{
var resetTask = socket.ResetAsync();

if (await Task.WhenAny(resetTask, Task.Delay(_receiveTimeout)) == resetTask)
if (await Task.WhenAny(resetTask, Task.Delay(_receiveTimeout)).ConfigureAwait(false) == resetTask)
{
await resetTask;
await resetTask.ConfigureAwait(false);
}
else
{
Expand Down Expand Up @@ -776,7 +776,7 @@ public async Task<IPooledSocketResult> AcquireAsync()
{
// okay, create the new item
var startTime = DateTime.Now;
socket = await CreateSocketAsync();
socket = await CreateSocketAsync().ConfigureAwait(false);

if (_logger.IsEnabled(LogLevel.Information))
{
Expand Down Expand Up @@ -1042,13 +1042,13 @@ protected internal virtual async Task<PooledSocket> CreateSocketAsync()
{
try
{
return await CreateSocketInternalAsync();
return await CreateSocketInternalAsync().ConfigureAwait(false);
}
catch
{
try
{
return await CreateSocketInternalAsync();
return await CreateSocketInternalAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
Expand All @@ -1066,7 +1066,7 @@ private async Task<PooledSocket> CreateSocketInternalAsync()
#else
_useSslStream, _useIPv6);
#endif
await ps.ConnectAsync();
await ps.ConnectAsync().ConfigureAwait(false);
return ps;
}

Expand Down Expand Up @@ -1133,7 +1133,7 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
_logger.LogDebug($"ExecuteOperationAsync({op})");
}

var result = await AcquireAsync();
var result = await AcquireAsync().ConfigureAwait(false);
if (result.Success && result.HasValue)
{
try
Expand All @@ -1149,13 +1149,13 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
}

var writeSocketTask = pooledSocket.WriteAsync(b);
if (await Task.WhenAny(writeSocketTask, Task.Delay(_config.ConnectionTimeout)) != writeSocketTask)
if (await Task.WhenAny(writeSocketTask, Task.Delay(_config.ConnectionTimeout)).ConfigureAwait(false) != writeSocketTask)
{
result.Fail("Timeout to pooledSocket.WriteAsync");
return result;
}

await writeSocketTask;
await writeSocketTask.ConfigureAwait(false);

//if Get, call BinaryResponse
if (_logger.IsEnabled(LogLevel.Debug))
Expand All @@ -1164,13 +1164,13 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati
}

var readResponseTask = op.ReadResponseAsync(pooledSocket);
if (await Task.WhenAny(readResponseTask, Task.Delay(_config.ConnectionTimeout)) != readResponseTask)
if (await Task.WhenAny(readResponseTask, Task.Delay(_config.ConnectionTimeout)).ConfigureAwait(false) != readResponseTask)
{
result.Fail($"Timeout to ReadResponseAsync(pooledSocket) for {op}");
return result;
}

var readResult = await readResponseTask;
var readResult = await readResponseTask.ConfigureAwait(false);
if (readResult.Success)
{
result.Pass();
Expand Down Expand Up @@ -1218,22 +1218,22 @@ protected virtual async Task<IPooledSocketResult> ExecuteOperationAsync(IOperati

protected virtual async Task<bool> ExecuteOperationAsync(IOperation op, Action<bool> next)
{
var socket = (await AcquireAsync()).Value;
var socket = (await AcquireAsync().ConfigureAwait(false)).Value;
if (socket == null) return false;

//key(string) to buffer(btye[])
var b = op.GetBuffer();

try
{
await socket.WriteAsync(b);
await socket.WriteAsync(b).ConfigureAwait(false);

var rrs = await op.ReadResponseAsync(socket, readSuccess =>
{
((IDisposable)socket).Dispose();

next(readSuccess);
});
}).ConfigureAwait(false);

return rrs;
}
Expand Down Expand Up @@ -1279,12 +1279,12 @@ IOperationResult IMemcachedNode.Execute(IOperation op)

async Task<IOperationResult> IMemcachedNode.ExecuteAsync(IOperation op)
{
return await ExecuteOperationAsync(op);
return await ExecuteOperationAsync(op).ConfigureAwait(false);
}

async Task<bool> IMemcachedNode.ExecuteAsync(IOperation op, Action<bool> next)
{
return await ExecuteOperationAsync(op, next);
return await ExecuteOperationAsync(op, next).ConfigureAwait(false);
}

event Action<IMemcachedNode> IMemcachedNode.Failed
Expand Down
22 changes: 10 additions & 12 deletions src/Enyim.Caching/Memcached/PooledSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ public async Task<bool> ConnectAsync()
{
var connTask = _socket.ConnectAsync(_endpoint);

if (await Task.WhenAny(connTask, Task.Delay(_connectionTimeout)) == connTask)
if (await Task.WhenAny(connTask, Task.Delay(_connectionTimeout)).ConfigureAwait(false) == connTask)
{
await connTask;
await connTask.ConfigureAwait(false);
}
else
{
Expand All @@ -176,7 +176,7 @@ public async Task<bool> ConnectAsync()
{
var ep = GetIPEndPoint(_endpoint);
if (_isSocketDisposed) return false;
await _socket.ConnectAsync(ep.Address, ep.Port);
await _socket.ConnectAsync(ep.Address, ep.Port).ConfigureAwait(false);
}

if (_socket != null)
Expand All @@ -203,7 +203,7 @@ await _sslStream.AuthenticateAsClientAsync(
#else
((DnsEndPoint)_endpoint).Host
#endif
);
).ConfigureAwait(false);
}
else
{
Expand Down Expand Up @@ -256,8 +256,6 @@ public void Reset()

public async Task ResetAsync()
{
// await _inputStream.FlushAsync();

int available = _socket.Available;

if (available > 0)
Expand All @@ -271,7 +269,7 @@ public async Task ResetAsync()

byte[] data = new byte[available];

await ReadAsync(data, 0, available);
await ReadAsync(data, 0, available).ConfigureAwait(false);
}

if (_logger.IsEnabled(LogLevel.Debug))
Expand Down Expand Up @@ -431,8 +429,8 @@ public async Task ReadAsync(byte[] buffer, int offset, int count)
try
{
int currentRead = (_useSslStream
? await _sslStream.ReadAsync(buffer, offset, shouldRead)
: await _inputStream.ReadAsync(buffer, offset, shouldRead));
? await _sslStream.ReadAsync(buffer, offset, shouldRead).ConfigureAwait(false)
: await _inputStream.ReadAsync(buffer, offset, shouldRead).ConfigureAwait(false));
if (currentRead == count)
break;
if (currentRead < 1)
Expand Down Expand Up @@ -581,14 +579,14 @@ public async Task WriteAsync(IList<ArraySegment<byte>> buffers)
{
foreach (var buf in buffers)
{
await _sslStream.WriteAsync(buf.Array, 0, buf.Count);
await _sslStream.WriteAsync(buf.Array, 0, buf.Count).ConfigureAwait(false);
}

await _sslStream.FlushAsync();
await _sslStream.FlushAsync().ConfigureAwait(false);
}
else
{
var bytesTransferred = await _socket.SendAsync(buffers, SocketFlags.None);
var bytesTransferred = await _socket.SendAsync(buffers, SocketFlags.None).ConfigureAwait(false);
if (bytesTransferred <= 0)
{
_isAlive = false;
Expand Down
10 changes: 5 additions & 5 deletions src/Enyim.Caching/Memcached/Protocol/Binary/BinaryNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ protected internal override PooledSocket CreateSocket()

protected internal override async Task<PooledSocket> CreateSocketAsync()
{
var retval = await base.CreateSocketAsync();
var retval = await base.CreateSocketAsync().ConfigureAwait(false);

if (_authenticationProvider != null && !(await AuthAsync(retval)))
if (_authenticationProvider != null && !await AuthAsync(retval).ConfigureAwait(false))
{
_logger.LogError("Authentication failed: " + EndPoint);

Expand Down Expand Up @@ -105,15 +105,15 @@ private async Task<bool> AuthAsync(PooledSocket socket)
{
SaslStep currentStep = new SaslStart(_authenticationProvider);

await socket.WriteAsync(currentStep.GetBuffer());
await socket.WriteAsync(currentStep.GetBuffer()).ConfigureAwait(false);

while (!(await currentStep.ReadResponseAsync(socket)).Success)
while (!(await currentStep.ReadResponseAsync(socket).ConfigureAwait(false)).Success)
{
// challenge-response authentication
if (currentStep.StatusCode == 0x21)
{
currentStep = new SaslContinue(_authenticationProvider, currentStep.Data);
await socket.WriteAsync(currentStep.GetBuffer());
await socket.WriteAsync(currentStep.GetBuffer()).ConfigureAwait(false);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/Enyim.Caching/Memcached/Protocol/Binary/BinaryResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task<bool> ReadAsync(PooledSocket socket)
if (!socket.IsAlive) return false;

var header = new byte[HeaderLength];
await socket.ReadAsync(header, 0, header.Length);
await socket.ReadAsync(header, 0, header.Length).ConfigureAwait(false);

int dataLength, extraLength;

Expand All @@ -89,7 +89,7 @@ public async Task<bool> ReadAsync(PooledSocket socket)
if (dataLength > 0)
{
var data = new byte[dataLength];
await socket.ReadAsync(data, 0, dataLength);
await socket.ReadAsync(data, 0, dataLength).ConfigureAwait(false);

Extra = new ArraySegment<byte>(data, 0, extraLength);
Data = new ArraySegment<byte>(data, extraLength, data.Length - extraLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected internal override IOperationResult ReadResponse(PooledSocket socket)
protected internal override async ValueTask<IOperationResult> ReadResponseAsync(PooledSocket socket)
{
var response = new BinaryResponse();
var retval = await response.ReadAsync(socket);
var retval = await response.ReadAsync(socket).ConfigureAwait(false);

Cas = response.CAS;
StatusCode = response.StatusCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected internal override IOperationResult ReadResponse(PooledSocket socket)
protected internal override async ValueTask<IOperationResult> ReadResponseAsync(PooledSocket socket)
{
var response = new BinaryResponse();
var retval = await response.ReadAsync(socket);
var retval = await response.ReadAsync(socket).ConfigureAwait(false);

StatusCode = StatusCode;
var result = new BinaryOperationResult()
Expand Down
Loading

0 comments on commit 7ba2613

Please sign in to comment.