Skip to content

Commit

Permalink
Add ConfigureAwait(false) (#1600)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Dec 25, 2022
1 parent af47efa commit 1b3b9dd
Show file tree
Hide file tree
Showing 125 changed files with 575 additions and 669 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -274,7 +274,7 @@ roslynator_blank_line_after_file_scoped_namespace_declaration = true
dotnet_diagnostic.RCS1080.severity = warning

# ConfigureAwait https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1090.md
dotnet_diagnostic.RCS1090.severity = suggestion
dotnet_diagnostic.RCS1090.severity = warning
roslynator_configure_await = true

# https://github.com/JosefPihrt/Roslynator/blob/master/docs/analyzers/RCS1102.md
Expand Down
9 changes: 3 additions & 6 deletions .github/workflows/healthchecks_ui_ci.yml
Expand Up @@ -7,9 +7,7 @@ on:
paths:
- src/HealthChecks.UI/**
- src/HealthChecks.UI.*/**
- test/HealthChecks.UI.Tests/**
- test/HealthChecks.UI.Core.Tests/**
- test/HealthChecks.UI.Client.Tests/**
- test/HealthChecks.UI.*/**
- .github/workflows/healthchecks_ui_ci.yml
- Directory.Build.props
- Directory.Build.targets
Expand All @@ -21,9 +19,8 @@ on:
branches: [ master ]
paths:
- src/HealthChecks.UI/**
- test/HealthChecks.UI.Tests/**
- test/HealthChecks.UI.Core.Tests/**
- test/HealthChecks.UI.Client.Tests/**
- src/HealthChecks.UI.*/**
- test/HealthChecks.UI.*/**
- .github/workflows/healthchecks_ui_ci.yml
- Directory.Build.props
- Directory.Build.targets
Expand Down
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<WarningsNotAsErrors>$(WarningsNotAsErrors);RCS1090</WarningsNotAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 4 additions & 4 deletions src/ClientCache.cs
Expand Up @@ -25,7 +25,7 @@ internal static class ClientCache
if (Cache<T>.Instance.TryGetValue(key, out var value))
return value;

value = await clientFactory(key);
value = await clientFactory(key).ConfigureAwait(false);

if (!Cache<T>.Instance.TryAdd(key, value))
{
Expand All @@ -45,7 +45,7 @@ internal static class ClientCache

if (!Cache<T>.Instance.TryAdd(key, value))
{
await value.DisposeAsync();
await value.DisposeAsync().ConfigureAwait(false);
return Cache<T>.Instance[key];
}

Expand All @@ -57,11 +57,11 @@ internal static class ClientCache
if (Cache<T>.Instance.TryGetValue(key, out var value))
return value;

value = await clientFactory(key);
value = await clientFactory(key).ConfigureAwait(false);

if (!Cache<T>.Instance.TryAdd(key, value))
{
await value.DisposeAsync();
await value.DisposeAsync().ConfigureAwait(false);
return Cache<T>.Instance[key];
}

Expand Down
16 changes: 7 additions & 9 deletions src/HealthChecks.ArangoDb/ArangoDbHealthCheck.cs
Expand Up @@ -18,14 +18,12 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{
using (var transport = await GetTransportAsync(_options))
using (var adb = new ArangoDBClient(transport))
{
var databases = await adb.Database.GetCurrentDatabaseInfoAsync();
return databases.Error
? new HealthCheckResult(context.Registration.FailureStatus, $"HealthCheck failed with status code: {databases.Code}.")
: HealthCheckResult.Healthy();
}
using var transport = await GetTransportAsync(_options).ConfigureAwait(false);
using var adb = new ArangoDBClient(transport);
var databases = await adb.Database.GetCurrentDatabaseInfoAsync().ConfigureAwait(false);
return databases.Error
? new HealthCheckResult(context.Registration.FailureStatus, $"HealthCheck failed with status code: {databases.Code}.")
: HealthCheckResult.Healthy();
}
catch (Exception ex)
{
Expand Down Expand Up @@ -53,7 +51,7 @@ private static async Task<HttpApiTransport> GetTransportAsync(ArangoDbOptions op

var transport = HttpApiTransport.UsingNoAuth(new Uri(options.HostUri), options.Database);
var authClient = new AuthApiClient(transport);
var jwtTokenResponse = await authClient.GetJwtTokenAsync(options.UserName, options.Password);
var jwtTokenResponse = await authClient.GetJwtTokenAsync(options.UserName, options.Password).ConfigureAwait(false);
transport.SetJwtToken(jwtTokenResponse.Jwt);
return transport;
}
Expand Down
2 changes: 1 addition & 1 deletion src/HealthChecks.Aws.S3/S3HealthCheck.cs
Expand Up @@ -40,7 +40,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context

using (client)
{
var response = await client.ListObjectsAsync(_bucketOptions.BucketName, cancellationToken);
var response = await client.ListObjectsAsync(_bucketOptions.BucketName, cancellationToken).ConfigureAwait(false);

if (_bucketOptions.CustomResponseCheck != null)
{
Expand Down
Expand Up @@ -20,7 +20,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
using var client = CreateSecretsManagerClient();
foreach (var secret in _secretsManagerOptions.Secrets)
{
await CheckSecretAsync(client, secret, cancellationToken);
await CheckSecretAsync(client, secret, cancellationToken).ConfigureAwait(false);
}

return HealthCheckResult.Healthy();
Expand Down Expand Up @@ -53,6 +53,6 @@ private async Task CheckSecretAsync(IAmazonSecretsManager client, string secretN
};

// Check the existence of the secret. If it does not throw it is a valid one (binary or not)
_ = await client.GetSecretValueAsync(request, cancellationToken);
_ = await client.GetSecretValueAsync(request, cancellationToken).ConfigureAwait(false);
}
}
Expand Up @@ -21,7 +21,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context

foreach (var (topicName, subscriptions) in _snsOptions.TopicsAndSubscriptions.Select(x => (x.Key, x.Value)))
{
var topic = await client.FindTopicAsync(topicName);
var topic = await client.FindTopicAsync(topicName).ConfigureAwait(false);

if (topic == null)
{
Expand All @@ -33,7 +33,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
continue;
}

var subscriptionsFromAws = await client.ListSubscriptionsByTopicAsync(topic.TopicArn, cancellationToken);
var subscriptionsFromAws = await client.ListSubscriptionsByTopicAsync(topic.TopicArn, cancellationToken).ConfigureAwait(false);

var subscriptionsArn = subscriptionsFromAws.Subscriptions.Select(s => s.SubscriptionArn);

Expand Down
2 changes: 1 addition & 1 deletion src/HealthChecks.Aws.Sqs/SqsHealthCheck.cs
Expand Up @@ -19,7 +19,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
using var client = CreateSqsClient();
foreach (var queueName in _sqsOptions.Queues)
{
_ = await client.GetQueueUrlAsync(queueName);
_ = await client.GetQueueUrlAsync(queueName).ConfigureAwait(false);
}

return HealthCheckResult.Healthy();
Expand Down
Expand Up @@ -20,7 +20,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
using var client = CreateParametersManagerClient();
foreach (var parameter in _systemsManagerOptions.Parameters)
{
await CheckParameterAsync(client, parameter, cancellationToken);
await CheckParameterAsync(client, parameter, cancellationToken).ConfigureAwait(false);
}

return HealthCheckResult.Healthy();
Expand Down Expand Up @@ -53,6 +53,6 @@ private IAmazonSimpleSystemsManagement CreateParametersManagerClient()
WithDecryption = true
};

_ = await client.GetParameterAsync(request, cancellationToken);
_ = await client.GetParameterAsync(request, cancellationToken).ConfigureAwait(false);
}
}
18 changes: 9 additions & 9 deletions src/HealthChecks.Azure.IoTHub/IoTHubHealthCheck.cs
Expand Up @@ -18,15 +18,15 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
if (_options.RegistryWriteCheck)
{
await ExecuteRegistryWriteCheckAsync(cancellationToken);
await ExecuteRegistryWriteCheckAsync(cancellationToken).ConfigureAwait(false);
}
else if (_options.RegistryReadCheck)
{
await ExecuteRegistryReadCheckAsync();
await ExecuteRegistryReadCheckAsync().ConfigureAwait(false);
}
if (_options.ServiceConnectionCheck)
{
await ExecuteServiceConnectionCheckAsync(cancellationToken);
await ExecuteServiceConnectionCheckAsync(cancellationToken).ConfigureAwait(false);
}

return HealthCheckResult.Healthy();
Expand All @@ -41,7 +41,7 @@ private async Task ExecuteServiceConnectionCheckAsync(CancellationToken cancella
{
using (var client = ServiceClient.CreateFromConnectionString(_options.ConnectionString, _options.ServiceConnectionTransport))
{
await client.GetServiceStatisticsAsync(cancellationToken);
await client.GetServiceStatisticsAsync(cancellationToken).ConfigureAwait(false);
}
}

Expand All @@ -50,7 +50,7 @@ private async Task ExecuteRegistryReadCheckAsync()
using (var client = RegistryManager.CreateFromConnectionString(_options.ConnectionString))
{
var query = client.CreateQuery(_options.RegistryReadQuery, 1);
await query.GetNextAsJsonAsync();
await query.GetNextAsJsonAsync().ConfigureAwait(false);
}
}

Expand All @@ -59,19 +59,19 @@ private async Task ExecuteRegistryWriteCheckAsync(CancellationToken cancellation
using (var client = RegistryManager.CreateFromConnectionString(_options.ConnectionString))
{
var deviceId = _options.RegistryWriteDeviceIdFactory();
var device = await client.GetDeviceAsync(deviceId, cancellationToken);
var device = await client.GetDeviceAsync(deviceId, cancellationToken).ConfigureAwait(false);

// in default implementation of configuration deviceId equals "health-check-registry-write-device-id"
// if in previous health check device were not removed -- try remove it
// if in previous health check device were added and removed -- try create and remove it
if (device != null)
{
await client.RemoveDeviceAsync(deviceId, cancellationToken);
await client.RemoveDeviceAsync(deviceId, cancellationToken).ConfigureAwait(false);
}
else
{
await client.AddDeviceAsync(new Device(deviceId), cancellationToken);
await client.RemoveDeviceAsync(deviceId, cancellationToken);
await client.AddDeviceAsync(new Device(deviceId), cancellationToken).ConfigureAwait(false);
await client.RemoveDeviceAsync(deviceId, cancellationToken).ConfigureAwait(false);
}
}
}
Expand Down
Expand Up @@ -29,7 +29,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
try
{
var digitalTwinClient = DigitalTwinClientConnections.GetOrAdd(ClientConnectionKey, _ => CreateDigitalTwinClient(_hostName));
_ = await digitalTwinClient.GetDigitalTwinAsync<BasicDigitalTwin>(_instanceName, cancellationToken: cancellationToken);
_ = await digitalTwinClient.GetDigitalTwinAsync<BasicDigitalTwin>(_instanceName, cancellationToken: cancellationToken).ConfigureAwait(false);
return HealthCheckResult.Healthy();

}
Expand Down
Expand Up @@ -19,7 +19,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
try
{
var managementClient = ManagementClientConnections.GetOrAdd(ClientConnectionKey, _ => CreateManagementClient());
_ = await managementClient.Operations.ListWithHttpMessagesAsync(cancellationToken: cancellationToken);
_ = await managementClient.Operations.ListWithHttpMessagesAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
return HealthCheckResult.Healthy();
}
catch (Exception ex)
Expand Down
6 changes: 3 additions & 3 deletions src/HealthChecks.AzureKeyVault/AzureKeyVaultHealthCheck.cs
Expand Up @@ -31,19 +31,19 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
foreach (var secret in _options.Secrets)
{
var secretClient = CreateSecretClient();
await secretClient.GetSecretAsync(secret, cancellationToken: cancellationToken);
await secretClient.GetSecretAsync(secret, cancellationToken: cancellationToken).ConfigureAwait(false);
}

foreach (var key in _options.Keys)
{
var keyClient = CreateKeyClient();
await keyClient.GetKeyAsync(key, cancellationToken: cancellationToken);
await keyClient.GetKeyAsync(key, cancellationToken: cancellationToken).ConfigureAwait(false);
}

foreach (var (key, checkExpired) in _options.Certificates)
{
var certificateClient = CreateCertificateClient();
var certificate = await certificateClient.GetCertificateAsync(key, cancellationToken: cancellationToken);
var certificate = await certificateClient.GetCertificateAsync(key, cancellationToken: cancellationToken).ConfigureAwait(false);

if (checkExpired && certificate.Value.Properties.ExpiresOn.HasValue)
{
Expand Down
4 changes: 2 additions & 2 deletions src/HealthChecks.AzureServiceBus/AzureEventHubHealthCheck.cs
Expand Up @@ -47,9 +47,9 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
try
{
var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient());
var client = await ClientCache.GetOrAddAsyncDisposableAsync(ConnectionKey, _ => CreateClient()).ConfigureAwait(false);

_ = await client.GetEventHubPropertiesAsync(cancellationToken);
_ = await client.GetEventHubPropertiesAsync(cancellationToken).ConfigureAwait(false);

return HealthCheckResult.Healthy();
}
Expand Down
Expand Up @@ -25,7 +25,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
var client = ClientConnections.GetOrAdd(ConnectionKey, _ => CreateClient());
var receiver = ServiceBusReceivers.GetOrAdd($"{nameof(AzureServiceBusQueueHealthCheck)}_{ConnectionKey}", client.CreateReceiver(_queueName));
_ = await receiver.PeekMessageAsync(cancellationToken: cancellationToken);
_ = await receiver.PeekMessageAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
return HealthCheckResult.Healthy();
}
catch (Exception ex)
Expand Down
Expand Up @@ -27,7 +27,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
{
var client = ClientConnections.GetOrAdd(ConnectionKey, _ => CreateClient());
var receiver = ServiceBusReceivers.GetOrAdd($"{nameof(AzureServiceBusSubscriptionHealthCheck)}_{ConnectionKey}", client.CreateReceiver(_topicName, _subscriptionName));
_ = await receiver.PeekMessageAsync(cancellationToken: cancellationToken);
_ = await receiver.PeekMessageAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
return HealthCheckResult.Healthy();
}
catch (Exception ex)
Expand Down
Expand Up @@ -27,7 +27,7 @@ public AzureServiceBusTopicHealthCheck(string endpoint, string topicName, TokenC
try
{
var managementClient = ManagementClientConnections.GetOrAdd(ConnectionKey, _ => CreateManagementClient());
_ = await managementClient.GetTopicRuntimePropertiesAsync(_topicName, cancellationToken);
_ = await managementClient.GetTopicRuntimePropertiesAsync(_topicName, cancellationToken).ConfigureAwait(false);
return HealthCheckResult.Healthy();
}
catch (Exception ex)
Expand Down
5 changes: 3 additions & 2 deletions src/HealthChecks.AzureStorage/AzureBlobStorageHealthCheck.cs
Expand Up @@ -38,12 +38,13 @@ await _blobServiceClient
.GetBlobContainersAsync(cancellationToken: cancellationToken)
.AsPages(pageSizeHint: 1)
.GetAsyncEnumerator(cancellationToken)
.MoveNextAsync();
.MoveNextAsync()
.ConfigureAwait(false);

if (!string.IsNullOrEmpty(_options.ContainerName))
{
var containerClient = _blobServiceClient.GetBlobContainerClient(_options.ContainerName);
await containerClient.GetPropertiesAsync(cancellationToken: cancellationToken);
await containerClient.GetPropertiesAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
}

return HealthCheckResult.Healthy();
Expand Down
5 changes: 3 additions & 2 deletions src/HealthChecks.AzureStorage/AzureFileShareHealthCheck.cs
Expand Up @@ -31,12 +31,13 @@ await _shareServiceClient
.GetSharesAsync(cancellationToken: cancellationToken)
.AsPages(pageSizeHint: 1)
.GetAsyncEnumerator(cancellationToken)
.MoveNextAsync();
.MoveNextAsync()
.ConfigureAwait(false);

if (!string.IsNullOrEmpty(_options.ShareName))
{
var shareClient = _shareServiceClient.GetShareClient(_options.ShareName);
await shareClient.GetPropertiesAsync(cancellationToken);
await shareClient.GetPropertiesAsync(cancellationToken).ConfigureAwait(false);
}

return HealthCheckResult.Healthy();
Expand Down
5 changes: 3 additions & 2 deletions src/HealthChecks.AzureStorage/AzureQueueStorageHealthCheck.cs
Expand Up @@ -38,12 +38,13 @@ await _queueServiceClient
.GetQueuesAsync(cancellationToken: cancellationToken)
.AsPages(pageSizeHint: 1)
.GetAsyncEnumerator(cancellationToken)
.MoveNextAsync();
.MoveNextAsync()
.ConfigureAwait(false);

if (!string.IsNullOrEmpty(_options.QueueName))
{
var queueClient = _queueServiceClient.GetQueueClient(_options.QueueName);
await queueClient.GetPropertiesAsync(cancellationToken);
await queueClient.GetPropertiesAsync(cancellationToken).ConfigureAwait(false);
}

return HealthCheckResult.Healthy();
Expand Down
2 changes: 1 addition & 1 deletion src/HealthChecks.Consul/ConsulHealthCheck.cs
Expand Up @@ -27,7 +27,7 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context

client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authHeaderValue);
}
using var result = await client.GetAsync($"{(_options.RequireHttps ? "https" : "http")}://{_options.HostName}:{_options.Port}/v1/status/leader", HttpCompletionOption.ResponseHeadersRead, cancellationToken);
using var result = await client.GetAsync($"{(_options.RequireHttps ? "https" : "http")}://{_options.HostName}:{_options.Port}/v1/status/leader", HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);

return result.IsSuccessStatusCode ? HealthCheckResult.Healthy() : new HealthCheckResult(context.Registration.FailureStatus, description: "Consul response was not a successful HTTP status code");
}
Expand Down

0 comments on commit 1b3b9dd

Please sign in to comment.