Skip to content

Commit

Permalink
Add 'ConfigureAwait(false)' to all awaited tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
Marfusios committed Dec 7, 2018
1 parent 7f1c8d0 commit 83af266
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/Websocket.Client/WebsocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public async Task Start()
_cancelation = new CancellationTokenSource();
_cancelationTotal = new CancellationTokenSource();

await StartClient(_url, _cancelation.Token, ReconnectionType.Initial);
await StartClient(_url, _cancelation.Token, ReconnectionType.Initial).ConfigureAwait(false);

StartBackgroundThreadForSending();
}
Expand Down Expand Up @@ -152,7 +152,7 @@ public async Task Reconnect()
Log.Debug(L("Client not started, ignoring reconnection.."));
return;
}
await Reconnect(ReconnectionType.ByUser);
await Reconnect(ReconnectionType.ByUser).ConfigureAwait(false);
}

private async Task SendFromQueue()
Expand All @@ -161,7 +161,7 @@ private async Task SendFromQueue()
{
foreach (var message in _messagesToSendQueue.GetConsumingEnumerable(_cancelationTotal.Token))
{
await SendInternal(message);
await SendInternal(message).ConfigureAwait(false);
}
}
catch (TaskCanceledException)
Expand Down Expand Up @@ -193,8 +193,8 @@ private async Task SendInternal(string message)
Log.Verbose(L($"Sending: {message}"));
var buffer = Encoding.UTF8.GetBytes(message);
var messageSegment = new ArraySegment<byte>(buffer);
var client = await GetClient();
await client.SendAsync(messageSegment, WebSocketMessageType.Text, true, _cancelation.Token);
var client = await GetClient().ConfigureAwait(false);
await client.SendAsync(messageSegment, WebSocketMessageType.Text, true, _cancelation.Token).ConfigureAwait(false);
}

private async Task StartClient(Uri uri, CancellationToken token, ReconnectionType type)
Expand All @@ -204,7 +204,7 @@ private async Task StartClient(Uri uri, CancellationToken token, ReconnectionTyp

try
{
await _client.ConnectAsync(uri, token);
await _client.ConnectAsync(uri, token).ConfigureAwait(false);
IsRunning = true;
_reconnectionSubject.OnNext(type);
#pragma warning disable 4014
Expand All @@ -216,16 +216,16 @@ private async Task StartClient(Uri uri, CancellationToken token, ReconnectionTyp
{
Log.Error(e, L("Exception while connecting. " +
$"Waiting {ErrorReconnectTimeoutMs/1000} sec before next reconnection try."));
await Task.Delay(ErrorReconnectTimeoutMs, token);
await Reconnect(ReconnectionType.Error);
await Task.Delay(ErrorReconnectTimeoutMs, token).ConfigureAwait(false);
await Reconnect(ReconnectionType.Error).ConfigureAwait(false);
}
}

private async Task<ClientWebSocket> GetClient()
{
if (_client == null || (_client.State != WebSocketState.Open && _client.State != WebSocketState.Connecting))
{
await Reconnect(ReconnectionType.Lost);
await Reconnect(ReconnectionType.Lost).ConfigureAwait(false);
}
return _client;
}
Expand All @@ -237,10 +237,10 @@ private async Task Reconnect( ReconnectionType type)
return;
Log.Debug(L("Reconnecting..."));
_cancelation.Cancel();
await Task.Delay(1000);
await Task.Delay(1000).ConfigureAwait(false);

_cancelation = new CancellationTokenSource();
await StartClient(_url, _cancelation.Token, type);
await StartClient(_url, _cancelation.Token, type).ConfigureAwait(false);
}

private async Task Listen(ClientWebSocket client, CancellationToken token)
Expand All @@ -255,7 +255,7 @@ private async Task Listen(ClientWebSocket client, CancellationToken token)
var resultMessage = new StringBuilder();
do
{
result = await client.ReceiveAsync(message, token);
result = await client.ReceiveAsync(message, token).ConfigureAwait(false);
var receivedMessage = Encoding.UTF8.GetString(buffer, 0, result.Count);
resultMessage.Append(receivedMessage);
if (result.MessageType != WebSocketMessageType.Text)
Expand Down

0 comments on commit 83af266

Please sign in to comment.