Skip to content

Commit 30bc54a

Browse files
Merge pull request #121 from nullinside-development-group/chore/await
chore: configureawait
2 parents 6962fe9 + 13bd899 commit 30bc54a

22 files changed

+114
-105
lines changed

src/Nullinside.Api.Common.AspNetCore/Middleware/BasicAuthenticationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected override async Task<AuthenticateResult> HandleAuthenticateAsync() {
6161
.AsNoTracking()
6262
.FirstOrDefaultAsync(u => !string.IsNullOrWhiteSpace(u.Token) &&
6363
u.Token == token &&
64-
!u.IsBanned);
64+
!u.IsBanned).ConfigureAwait(false);
6565

6666
if (null == dbUser) {
6767
return AuthenticateResult.Fail("Invalid token");
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
1+
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib"
2+
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xml:space="preserve">
24
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String></wpf:ResourceDictionary>

src/Nullinside.Api.Common/Docker/DockerProxy.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class DockerProxy : IDockerProxy {
4040
/// <inheritdoc />
4141
public async Task<IEnumerable<DockerResource>> GetContainers(CancellationToken cancellationToken) {
4242
(string output, string error) response =
43-
await ExecuteCommand("docker container ls -a --format '{{.Names}}|{{.Status}}'", cancellationToken);
43+
await ExecuteCommand("docker container ls -a --format '{{.Names}}|{{.Status}}'", cancellationToken).ConfigureAwait(false);
4444
if (string.IsNullOrWhiteSpace(response.output)) {
4545
return Enumerable.Empty<DockerResource>();
4646
}
@@ -69,7 +69,7 @@ public async Task<IEnumerable<DockerResource>> GetContainers(CancellationToken c
6969
/// <inheritdoc />
7070
public async Task<IEnumerable<DockerResource>> GetDockerComposeProjects(CancellationToken cancellationToken) {
7171
(string output, string error) responseJson =
72-
await ExecuteCommand("docker compose ls -a --format 'json'", cancellationToken);
72+
await ExecuteCommand("docker compose ls -a --format 'json'", cancellationToken).ConfigureAwait(false);
7373
if (string.IsNullOrWhiteSpace(responseJson.output)) {
7474
return Enumerable.Empty<DockerResource>();
7575
}
@@ -89,7 +89,7 @@ public async Task<IEnumerable<DockerResource>> GetDockerComposeProjects(Cancella
8989
public async Task<bool> TurnOnOffDockerContainer(string name, bool turnOn, CancellationToken cancellationToken) {
9090
string command = turnOn ? "start" : "stop";
9191
(string output, string error) responseJson =
92-
await ExecuteCommand($"docker container {command} {name}", cancellationToken);
92+
await ExecuteCommand($"docker container {command} {name}", cancellationToken).ConfigureAwait(false);
9393
if (string.IsNullOrWhiteSpace(responseJson.error)) {
9494
return false;
9595
}
@@ -101,11 +101,11 @@ public async Task<bool> TurnOnOffDockerContainer(string name, bool turnOn, Cance
101101
/// <inheritdoc />
102102
public async Task<bool> TurnOnOffDockerCompose(string name, bool turnOn, CancellationToken cancellationToken,
103103
string? backupFolder) {
104-
IEnumerable<DockerResource> existing = await GetDockerComposeProjects(cancellationToken);
104+
IEnumerable<DockerResource> existing = await GetDockerComposeProjects(cancellationToken).ConfigureAwait(false);
105105
if (null != existing.FirstOrDefault(e => name.Equals(e.Name))) {
106106
string command = turnOn ? "start" : "stop";
107107
(string output, string error) stdout =
108-
await ExecuteCommand($"docker compose -p {name} {command}", cancellationToken);
108+
await ExecuteCommand($"docker compose -p {name} {command}", cancellationToken).ConfigureAwait(false);
109109
if (string.IsNullOrWhiteSpace(stdout.error)) {
110110
return false;
111111
}
@@ -119,7 +119,7 @@ public async Task<bool> TurnOnOffDockerCompose(string name, bool turnOn, Cancell
119119
}
120120

121121
(string output, string error)
122-
output = await ExecuteCommand("docker compose up -d", cancellationToken, backupFolder);
122+
output = await ExecuteCommand("docker compose up -d", cancellationToken, backupFolder).ConfigureAwait(false);
123123
if (string.IsNullOrWhiteSpace(output.error)) {
124124
return false;
125125
}
@@ -138,7 +138,7 @@ public async Task<bool> TurnOnOffDockerCompose(string name, bool turnOn, Cancell
138138
private async Task<(string output, string error)> ExecuteCommand(string command, CancellationToken token = new(),
139139
string? dir = null) {
140140
using SshClient client = new(_server!, _username!, _password!);
141-
await client.ConnectAsync(token);
141+
await client.ConnectAsync(token).ConfigureAwait(false);
142142
using SshCommand? responseJson = client.RunCommand($"cd {dir}; echo {_password2} | sudo -S {command}");
143143
return (responseJson.Result, responseJson.Error);
144144
}

src/Nullinside.Api.Common/Extensions/WebSocketExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static class WebSocketExtensions {
1414
/// <param name="message">The message to send.</param>
1515
/// <param name="cancelToken">The cancellation token.</param>
1616
public static async Task SendTextAsync(this WebSocket webSocket, string message, CancellationToken cancelToken = new()) {
17-
await webSocket.SendAsync(Encoding.ASCII.GetBytes(message), WebSocketMessageType.Text, true, cancelToken);
17+
await webSocket.SendAsync(Encoding.ASCII.GetBytes(message), WebSocketMessageType.Text, true, cancelToken).ConfigureAwait(false);
1818
}
1919

2020
/// <summary>
@@ -29,7 +29,7 @@ public static class WebSocketExtensions {
2929

3030
do {
3131
var data = new ArraySegment<byte>(new byte[1024]);
32-
response = await webSocket.ReceiveAsync(data, cancelToken);
32+
response = await webSocket.ReceiveAsync(data, cancelToken).ConfigureAwait(false);
3333
fullMessage.AddRange(data);
3434
} while (null == response.CloseStatus && !response.EndOfMessage);
3535

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
1+
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib"
2+
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xml:space="preserve">
24
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String></wpf:ResourceDictionary>

src/Nullinside.Api.Common/Retry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ public static class Retry {
3232
Exception? exceptionToThrow = null;
3333
while (tries <= numberOfRetries && !token.IsCancellationRequested) {
3434
try {
35-
return await action();
35+
return await action().ConfigureAwait(false);
3636
}
3737
catch (Exception ex) {
3838
exceptionToThrow = ex;
3939
++tries;
4040

4141
// Why did I do as delay THEN method? Honestly...I don't have a good reason. You can change it if you want, just
4242
// update the documentation if you do.
43-
await Task.Delay(waitTime.HasValue ? (int)waitTime.Value.TotalMilliseconds : 1000, token);
43+
await Task.Delay(waitTime.HasValue ? (int)waitTime.Value.TotalMilliseconds : 1000, token).ConfigureAwait(false);
4444
if (null != runOnFailure) {
4545
runOnFailure(ex);
4646
}

src/Nullinside.Api.Common/Twitch/TwitchApiProxy.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires,
8888
public virtual async Task<TwitchAccessToken?> CreateAccessToken(string code, CancellationToken token = new()) {
8989
ITwitchAPI api = GetApi();
9090
AuthCodeResponse? response = await api.Auth.GetAccessTokenFromCodeAsync(code, TwitchAppConfig?.ClientSecret,
91-
TwitchAppConfig?.ClientRedirect);
91+
TwitchAppConfig?.ClientRedirect).ConfigureAwait(false);
9292
if (null == response) {
9393
return null;
9494
}
@@ -109,7 +109,7 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires,
109109
}
110110

111111
ITwitchAPI api = GetApi();
112-
RefreshResponse? response = await api.Auth.RefreshAuthTokenAsync(OAuth?.RefreshToken, TwitchAppConfig?.ClientSecret, TwitchAppConfig?.ClientId);
112+
RefreshResponse? response = await api.Auth.RefreshAuthTokenAsync(OAuth?.RefreshToken, TwitchAppConfig?.ClientSecret, TwitchAppConfig?.ClientId).ConfigureAwait(false);
113113
if (null == response) {
114114
return null;
115115
}
@@ -129,7 +129,7 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires,
129129
/// <inheritdoc />
130130
public async Task<bool> GetAccessTokenIsValid(CancellationToken token = new()) {
131131
try {
132-
return !string.IsNullOrWhiteSpace((await GetUser(token))?.Id);
132+
return !string.IsNullOrWhiteSpace((await GetUser(token).ConfigureAwait(false))?.Id);
133133
}
134134
catch {
135135
return false;
@@ -140,40 +140,40 @@ public TwitchApiProxy(string token, string refreshToken, DateTime tokenExpires,
140140
public virtual async Task<User?> GetUser(CancellationToken token = new()) {
141141
return await Retry.Execute(async () => {
142142
ITwitchAPI api = GetApi();
143-
GetUsersResponse? response = await api.Helix.Users.GetUsersAsync();
143+
GetUsersResponse? response = await api.Helix.Users.GetUsersAsync().ConfigureAwait(false);
144144
if (null == response) {
145145
return null;
146146
}
147147

148148
return response.Users.FirstOrDefault();
149-
}, Retries, token);
149+
}, Retries, token).ConfigureAwait(false);
150150
}
151151

152152
/// <inheritdoc />
153153
public virtual async Task<(string? id, string? username)> GetUser(string username, CancellationToken token = new()) {
154154
return await Retry.Execute(async () => {
155155
ITwitchAPI api = GetApi();
156-
GetUsersResponse? response = await api.Helix.Users.GetUsersAsync(logins: [username]);
156+
GetUsersResponse? response = await api.Helix.Users.GetUsersAsync(logins: [username]).ConfigureAwait(false);
157157
if (null == response) {
158158
return (null, null);
159159
}
160160

161161
User? user = response.Users.FirstOrDefault();
162162
return (user?.Id, user?.Login);
163-
}, Retries, token);
163+
}, Retries, token).ConfigureAwait(false);
164164
}
165165

166166
/// <inheritdoc />
167167
public virtual async Task<string?> GetUserEmail(CancellationToken token = new()) {
168168
return await Retry.Execute(async () => {
169169
ITwitchAPI api = GetApi();
170-
GetUsersResponse? response = await api.Helix.Users.GetUsersAsync();
170+
GetUsersResponse? response = await api.Helix.Users.GetUsersAsync().ConfigureAwait(false);
171171
if (null == response) {
172172
return null;
173173
}
174174

175175
return response.Users.FirstOrDefault()?.Email;
176-
}, Retries, token);
176+
}, Retries, token).ConfigureAwait(false);
177177
}
178178

179179
/// <inheritdoc />
@@ -192,9 +192,9 @@ public virtual async Task<IEnumerable<TwitchModeratedChannel>> GetUserModChannel
192192
request.Headers.Add("Authorization", $"Bearer {OAuth?.AccessToken}");
193193
request.Headers.Add("Client-Id", TwitchAppConfig?.ClientId);
194194

195-
using HttpResponseMessage response = await client.SendAsync(request);
195+
using HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
196196
response.EnsureSuccessStatusCode();
197-
string responseBody = await response.Content.ReadAsStringAsync();
197+
string responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
198198
var moderatedChannels = JsonConvert.DeserializeObject<TwitchModeratedChannelsResponse>(responseBody);
199199
if (null == moderatedChannels) {
200200
break;
@@ -219,7 +219,7 @@ public virtual async Task<IEnumerable<BannedUser>> BanChannelUsers(string channe
219219
BanUserResponse? response = await api.Helix.Moderation.BanUserAsync(channelId, botId, new BanUserRequest {
220220
UserId = user.Id,
221221
Reason = reason
222-
});
222+
}).ConfigureAwait(false);
223223

224224
if (null == response || null == response.Data) {
225225
continue;
@@ -229,7 +229,7 @@ public virtual async Task<IEnumerable<BannedUser>> BanChannelUsers(string channe
229229
LOG.Info($"Banned {user.Username} ({user.Id}) in {channelId}: {reason}");
230230
}
231231
catch (HttpResponseException ex) {
232-
string exceptionReason = await ex.HttpResponse.Content.ReadAsStringAsync(token);
232+
string exceptionReason = await ex.HttpResponse.Content.ReadAsStringAsync(token).ConfigureAwait(false);
233233
LOG.Debug($"Failed to ban {user.Username} ({user.Id}) in {channelId}: {exceptionReason}", ex);
234234
}
235235
catch (Exception ex) {
@@ -238,7 +238,7 @@ public virtual async Task<IEnumerable<BannedUser>> BanChannelUsers(string channe
238238
}
239239

240240
return bannedUsers;
241-
}, Retries, token);
241+
}, Retries, token).ConfigureAwait(false);
242242
}
243243

244244
/// <inheritdoc />
@@ -250,7 +250,7 @@ public virtual async Task<IEnumerable<Chatter>> GetChannelUsers(string channelId
250250
string? cursor = null;
251251
int total = 0;
252252
do {
253-
GetChattersResponse? response = await api.Helix.Chat.GetChattersAsync(channelId, botId, 1000, cursor);
253+
GetChattersResponse? response = await api.Helix.Chat.GetChattersAsync(channelId, botId, 1000, cursor).ConfigureAwait(false);
254254
if (null == response) {
255255
break;
256256
}
@@ -262,7 +262,7 @@ public virtual async Task<IEnumerable<Chatter>> GetChannelUsers(string channelId
262262

263263
Debug.Assert(chatters.Count == total);
264264
return chatters;
265-
}, Retries, token);
265+
}, Retries, token).ConfigureAwait(false);
266266
}
267267

268268
/// <inheritdoc />
@@ -279,7 +279,7 @@ public virtual async Task<IEnumerable<string>> GetChannelsLive(IEnumerable<strin
279279
}
280280

281281
GetStreamsResponse? response =
282-
await api.Helix.Streams.GetStreamsAsync(userIds: twitchIdsArray[i..lastIndex].ToList());
282+
await api.Helix.Streams.GetStreamsAsync(userIds: twitchIdsArray[i..lastIndex].ToList()).ConfigureAwait(false);
283283
if (null != response) {
284284
liveUsers.AddRange(response.Streams.Where(s =>
285285
"live".Equals(s.Type, StringComparison.InvariantCultureIgnoreCase)));
@@ -298,7 +298,7 @@ public virtual async Task<IEnumerable<string>> GetChannelsLive(IEnumerable<strin
298298
GetModeratorsResponse? response = null;
299299
do {
300300
response = await api.Helix.Moderation.GetModeratorsAsync(channelId, first: 100,
301-
after: response?.Pagination?.Cursor);
301+
after: response?.Pagination?.Cursor).ConfigureAwait(false);
302302
if (null == response || null == response.Data) {
303303
break;
304304
}
@@ -313,16 +313,16 @@ public virtual async Task<IEnumerable<string>> GetChannelsLive(IEnumerable<strin
313313
} while (null != response.Pagination?.Cursor);
314314

315315
return results;
316-
}, Retries, token);
316+
}, Retries, token).ConfigureAwait(false);
317317
}
318318

319319
/// <inheritdoc />
320320
public virtual async Task<bool> AddChannelMod(string channelId, string userId, CancellationToken token = new()) {
321321
return await Retry.Execute(async () => {
322322
ITwitchAPI api = GetApi();
323-
await api.Helix.Moderation.AddChannelModeratorAsync(channelId, userId);
323+
await api.Helix.Moderation.AddChannelModeratorAsync(channelId, userId).ConfigureAwait(false);
324324
return true;
325-
}, Retries, token);
325+
}, Retries, token).ConfigureAwait(false);
326326
}
327327

328328
/// <summary>

src/Nullinside.Api.Common/Twitch/TwitchClientProxy.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public async Task<bool> SendMessage(string channel, string message, uint retryCo
150150
// Try to connect and join the channel.
151151
bool connectedAndJoined = false;
152152
for (int i = 0; i < retryConnection; i++) {
153-
if (await JoinChannel(channel)) {
153+
if (await JoinChannel(channel).ConfigureAwait(false)) {
154154
connectedAndJoined = true;
155155
break;
156156
}
@@ -184,7 +184,7 @@ public async Task<bool> SendMessage(string channel, string message, uint retryCo
184184

185185
/// <inheritdoc />
186186
public async Task AddMessageCallback(string channel, Action<OnMessageReceivedArgs> callback) {
187-
await JoinChannel(channel);
187+
await JoinChannel(channel).ConfigureAwait(false);
188188
string channelSan = channel.ToLowerInvariant();
189189
lock (_onMessageReceived) {
190190
if (!_onMessageReceived.TryAdd(channelSan, callback)) {
@@ -223,7 +223,7 @@ public void RemoveMessageCallback(string channel, Action<OnMessageReceivedArgs>
223223

224224
/// <inheritdoc />
225225
public async Task AddBannedCallback(string channel, Action<OnUserBannedArgs> callback) {
226-
await JoinChannel(channel);
226+
await JoinChannel(channel).ConfigureAwait(false);
227227

228228
lock (_onUserBanReceived) {
229229
_onUserBanReceived[channel] = callback;
@@ -250,7 +250,7 @@ public void RemoveDisconnectedCallback(Action callback) {
250250

251251
/// <inheritdoc />
252252
public async Task AddRaidCallback(string channel, Action<OnRaidNotificationArgs> callback) {
253-
await JoinChannel(channel);
253+
await JoinChannel(channel).ConfigureAwait(false);
254254

255255
lock (_onRaid) {
256256
_onRaid[channel] = callback;
@@ -282,7 +282,7 @@ private async Task<bool> JoinChannel(string channel) {
282282
}
283283

284284
// Try to connect.
285-
if (!await Connect()) {
285+
if (!await Connect().ConfigureAwait(false)) {
286286
return false;
287287
}
288288

@@ -319,7 +319,7 @@ private async Task<bool> JoinChannel(string channel) {
319319
/// <param name="e">The event arguments.</param>
320320
private async void TwitchChatClientReconnectOnElapsed(object? sender, ElapsedEventArgs e) {
321321
// Connect the chat client.
322-
await Connect();
322+
await Connect().ConfigureAwait(false);
323323

324324
// Pull the master list of channels we should be connected to the stack.
325325
string[]? allChannels = null;
@@ -329,7 +329,7 @@ private async void TwitchChatClientReconnectOnElapsed(object? sender, ElapsedEve
329329

330330
// Join all the channels.
331331
foreach (string channel in allChannels) {
332-
await JoinChannel(channel);
332+
await JoinChannel(channel).ConfigureAwait(false);
333333
}
334334

335335
// Restart the timer.
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1-
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
1+
<wpf:ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib"
2+
xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xml:space="preserve">
24
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String></wpf:ResourceDictionary>

src/Nullinside.Api.Model/Shared/DatabaseLock.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void Dispose() {
4848

4949
// This is only used with hard coded names.
5050
#pragma warning disable EF1002
51-
await _mysqlDbContext.Database.ExecuteSqlRawAsync($"SELECT GET_LOCK('{name}', -1)", cancellationToken);
51+
await _mysqlDbContext.Database.ExecuteSqlRawAsync($"SELECT GET_LOCK('{name}', -1)", cancellationToken).ConfigureAwait(false);
5252
#pragma warning restore EF1002
5353
_name = name;
5454
return true;
@@ -63,13 +63,13 @@ public void Dispose() {
6363
if (!string.IsNullOrWhiteSpace(name) && !name.Equals(_name, StringComparison.InvariantCultureIgnoreCase)) {
6464
// This is only used with hard coded names.
6565
#pragma warning disable EF1002
66-
await _mysqlDbContext.Database.ExecuteSqlRawAsync($"SELECT RELEASE_LOCK('{_name}')", cancellationToken);
66+
await _mysqlDbContext.Database.ExecuteSqlRawAsync($"SELECT RELEASE_LOCK('{_name}')", cancellationToken).ConfigureAwait(false);
6767
#pragma warning restore EF1002
6868
}
6969

7070
// This is only used with hard coded names.
7171
#pragma warning disable EF1002
72-
await _mysqlDbContext.Database.ExecuteSqlRawAsync($"SELECT RELEASE_LOCK('{name}')", cancellationToken);
72+
await _mysqlDbContext.Database.ExecuteSqlRawAsync($"SELECT RELEASE_LOCK('{name}')", cancellationToken).ConfigureAwait(false);
7373
#pragma warning restore EF1002
7474
_name = null;
7575
}

0 commit comments

Comments
 (0)