Skip to content

Commit

Permalink
Added missing ConfigureAwait(...) calls and an analyzer to check
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed May 10, 2024
1 parent 4dd4929 commit 6794e67
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
1 change: 1 addition & 0 deletions AzureIPNetworks/AzureIPNetworks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ConfigureAwaitChecker.Analyzer" Version="5.0.0.1" PrivateAssets="All"/>
<PackageReference Include="Polyfill " Version="4.6.0" PrivateAssets="All"/>
</ItemGroup>

Expand Down
8 changes: 4 additions & 4 deletions AzureIPNetworks/AzureIPsDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@ public class AzureIPsDownloader(HttpClient client)

var pageUrl = $"https://www.microsoft.com/en-us/download/confirmation.aspx?id={fileId}";
#if NET5_0_OR_GREATER
var response = await client.GetStringAsync(pageUrl, cancellationToken);
var response = await client.GetStringAsync(pageUrl, cancellationToken).ConfigureAwait(false);
#else
var response = await client.GetStringAsync(pageUrl);
var response = await client.GetStringAsync(pageUrl).ConfigureAwait(false);
#endif
var matches = fileUriParserRegex.Match(response);
if (matches.Success)
{
var url = matches.Value;
#if NET5_0_OR_GREATER
var stream = await client.GetStreamAsync(url, cancellationToken);
var stream = await client.GetStreamAsync(url, cancellationToken).ConfigureAwait(false);
#else
var stream = await client.GetStreamAsync(url);
var stream = await client.GetStreamAsync(url).ConfigureAwait(false);
#endif
return (url, stream);
}
Expand Down
26 changes: 14 additions & 12 deletions AzureIPNetworks/AzureIPsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract class AzureIPsProvider
/// <param name="cancellationToken">A token that may be used to cancel the operation.</param>
/// <returns></returns>
public async ValueTask<bool> IsAzureIpAsync(IPAddress address, AzureCloud cloud = AzureCloud.Public, string? service = null, string? region = null, CancellationToken cancellationToken = default)
=> Contained(await GetNetworksAsync(cloud, service, region, cancellationToken), address);
=> Contained(await GetNetworksAsync(cloud, service, region, cancellationToken).ConfigureAwait(false), address);

/// <summary>Checks if the supplied IP address is an Azure IP.</summary>
/// <param name="cloud">The Azure Cloud to check in.</param>
Expand All @@ -59,7 +59,7 @@ public async ValueTask<bool> IsAzureIpAsync(IPAddress address,
IReadOnlyList<string> services,
IReadOnlyList<string> regions,
CancellationToken cancellationToken = default)
=> Contained(await GetNetworksAsync(cloud, services, regions, cancellationToken), address);
=> Contained(await GetNetworksAsync(cloud, services, regions, cancellationToken).ConfigureAwait(false), address);

#if !NET8_0_OR_GREATER
/// <summary>Checks if the supplied IP network is an Azure IP.</summary>
Expand All @@ -76,7 +76,7 @@ public async ValueTask<bool> IsAzureIpAsync(IPAddress address,
/// <param name="cancellationToken">A token that may be used to cancel the operation.</param>
/// <returns></returns>
public async ValueTask<bool> IsAzureIpAsync(IPNetwork2 network, AzureCloud cloud = AzureCloud.Public, string? service = null, string? region = null, CancellationToken cancellationToken = default)
=> Contained(await GetNetworksAsync(cloud, service, region, cancellationToken), network);
=> Contained(await GetNetworksAsync(cloud, service, region, cancellationToken).ConfigureAwait(false), network);

/// <summary>Checks if the supplied IP network is an Azure IP.</summary>
/// <param name="cloud">The Azure Cloud to check in.</param>
Expand All @@ -96,7 +96,7 @@ public async ValueTask<bool> IsAzureIpAsync(IPNetwork2 network,
IReadOnlyList<string> services,
IReadOnlyList<string> regions,
CancellationToken cancellationToken = default)
=> Contained(await GetNetworksAsync(cloud, services, regions, cancellationToken), network);
=> Contained(await GetNetworksAsync(cloud, services, regions, cancellationToken).ConfigureAwait(false), network);
#endif

/// <summary>Get the Azure IP networks.</summary>
Expand Down Expand Up @@ -142,15 +142,17 @@ public async ValueTask<IEnumerable<IPNetwork2>> GetNetworksAsync(AzureCloud clou
CancellationToken cancellationToken = default)
#endif
{
IEnumerable<ServiceTag> tags = cloud switch
ValueTask<IEnumerable<ServiceTag>> func = cloud switch
{
AzureCloud.Public => await GetServiceTagsAsync(AzureCloud.Public, cancellationToken),
AzureCloud.China => await GetServiceTagsAsync(AzureCloud.China, cancellationToken),
AzureCloud.AzureGovernment => await GetServiceTagsAsync(AzureCloud.AzureGovernment, cancellationToken),
AzureCloud.AzureGermany => await GetServiceTagsAsync(AzureCloud.AzureGermany, cancellationToken),
AzureCloud.Public => GetServiceTagsAsync(AzureCloud.Public, cancellationToken),
AzureCloud.China => GetServiceTagsAsync(AzureCloud.China, cancellationToken),
AzureCloud.AzureGovernment => GetServiceTagsAsync(AzureCloud.AzureGovernment, cancellationToken),
AzureCloud.AzureGermany => GetServiceTagsAsync(AzureCloud.AzureGermany, cancellationToken),
_ => throw new NotImplementedException(),
};

var tags = await func.ConfigureAwait(false);

// if the services are provided, only retain networks for those services
if (services?.Count > 0) tags = tags.Where(t => services.Contains(t.Properties?.SystemService, StringComparer.OrdinalIgnoreCase));

Expand All @@ -177,10 +179,10 @@ public async ValueTask<IEnumerable<ServiceTag>> GetServiceTagsAsync(AzureCloud c
if (!data.TryGetValue(cloud, out var ranges))
{
// get the stream
var stream = await GetStreamAsync(cloud, cancellationToken);
var stream = await GetStreamAsync(cloud, cancellationToken).ConfigureAwait(false);

// deserialize the JSON file
data[cloud] = ranges = (await JsonSerializer.DeserializeAsync(stream, AzureIPNetworksJsonSerializerContext.Default.CloudServiceTags, cancellationToken))!;
data[cloud] = ranges = (await JsonSerializer.DeserializeAsync(stream, AzureIPNetworksJsonSerializerContext.Default.CloudServiceTags, cancellationToken).ConfigureAwait(false))!;
}

return ranges.Values;
Expand Down Expand Up @@ -236,5 +238,5 @@ public AzureIPsProviderRemote() : this(new HttpClient()) { }

/// <inheritdoc/>
protected override async ValueTask<Stream> GetStreamAsync(AzureCloud cloud, CancellationToken cancellationToken = default)
=> (await downloader.DownloadAsync(cloud, cancellationToken)).stream;
=> (await downloader.DownloadAsync(cloud, cancellationToken).ConfigureAwait(false)).stream;
}

0 comments on commit 6794e67

Please sign in to comment.