From 02a32a677c4cde23b9ea29e53177d9f2d8ca0554 Mon Sep 17 00:00:00 2001 From: Pent Ploompuu Date: Tue, 11 Jun 2024 21:07:46 +0300 Subject: [PATCH] Cleanup Http.Resilience dependencies --- eng/packages/General.props | 1 - ...ensions.AmbientMetadata.Application.csproj | 1 - ...oft.Extensions.Compliance.Redaction.csproj | 3 --- ...osoft.Extensions.Compliance.Testing.csproj | 2 -- ....DependencyInjection.AutoActivation.csproj | 1 - ...soft.Extensions.Diagnostics.Testing.csproj | 3 +-- ...icrosoft.Extensions.Hosting.Testing.csproj | 1 - ...crosoft.Extensions.Http.Diagnostics.csproj | 3 --- .../Internal/Randomizer.cs | 9 +++++++-- .../Polly/HttpClientResiliencePredicates.cs | 3 +-- .../Polly/HttpRetryStrategyOptions.cs | 2 +- .../RoutingStrategyBuilderExtensions.cs | 4 ++-- .../Microsoft.Extensions.Resilience.csproj | 7 ------- ...tionEnricherServiceCollectionExtensions.cs | 19 ++----------------- ...cessEnricherServiceCollectionExtensions.cs | 19 ++----------------- .../Microsoft.Extensions.Telemetry.csproj | 3 --- src/Shared/RentedSpan/RentedSpan.cs | 2 +- 17 files changed, 17 insertions(+), 66 deletions(-) diff --git a/eng/packages/General.props b/eng/packages/General.props index b74a6cd1f0c..af9621dc0b7 100644 --- a/eng/packages/General.props +++ b/eng/packages/General.props @@ -16,7 +16,6 @@ - diff --git a/src/Libraries/Microsoft.Extensions.AmbientMetadata.Application/Microsoft.Extensions.AmbientMetadata.Application.csproj b/src/Libraries/Microsoft.Extensions.AmbientMetadata.Application/Microsoft.Extensions.AmbientMetadata.Application.csproj index 5cc014f22ca..2603f0b42f4 100644 --- a/src/Libraries/Microsoft.Extensions.AmbientMetadata.Application/Microsoft.Extensions.AmbientMetadata.Application.csproj +++ b/src/Libraries/Microsoft.Extensions.AmbientMetadata.Application/Microsoft.Extensions.AmbientMetadata.Application.csproj @@ -19,7 +19,6 @@ - diff --git a/src/Libraries/Microsoft.Extensions.Compliance.Redaction/Microsoft.Extensions.Compliance.Redaction.csproj b/src/Libraries/Microsoft.Extensions.Compliance.Redaction/Microsoft.Extensions.Compliance.Redaction.csproj index 286a14ca69e..8a6a526acc1 100644 --- a/src/Libraries/Microsoft.Extensions.Compliance.Redaction/Microsoft.Extensions.Compliance.Redaction.csproj +++ b/src/Libraries/Microsoft.Extensions.Compliance.Redaction/Microsoft.Extensions.Compliance.Redaction.csproj @@ -26,11 +26,8 @@ - - - diff --git a/src/Libraries/Microsoft.Extensions.Compliance.Testing/Microsoft.Extensions.Compliance.Testing.csproj b/src/Libraries/Microsoft.Extensions.Compliance.Testing/Microsoft.Extensions.Compliance.Testing.csproj index ef2fcd70e7f..85ebae934dc 100644 --- a/src/Libraries/Microsoft.Extensions.Compliance.Testing/Microsoft.Extensions.Compliance.Testing.csproj +++ b/src/Libraries/Microsoft.Extensions.Compliance.Testing/Microsoft.Extensions.Compliance.Testing.csproj @@ -24,9 +24,7 @@ - - diff --git a/src/Libraries/Microsoft.Extensions.DependencyInjection.AutoActivation/Microsoft.Extensions.DependencyInjection.AutoActivation.csproj b/src/Libraries/Microsoft.Extensions.DependencyInjection.AutoActivation/Microsoft.Extensions.DependencyInjection.AutoActivation.csproj index 9b17c21e82b..7d02c3f1e90 100644 --- a/src/Libraries/Microsoft.Extensions.DependencyInjection.AutoActivation/Microsoft.Extensions.DependencyInjection.AutoActivation.csproj +++ b/src/Libraries/Microsoft.Extensions.DependencyInjection.AutoActivation/Microsoft.Extensions.DependencyInjection.AutoActivation.csproj @@ -13,7 +13,6 @@ - diff --git a/src/Libraries/Microsoft.Extensions.Diagnostics.Testing/Microsoft.Extensions.Diagnostics.Testing.csproj b/src/Libraries/Microsoft.Extensions.Diagnostics.Testing/Microsoft.Extensions.Diagnostics.Testing.csproj index 618c6ba38ba..01f3b954262 100644 --- a/src/Libraries/Microsoft.Extensions.Diagnostics.Testing/Microsoft.Extensions.Diagnostics.Testing.csproj +++ b/src/Libraries/Microsoft.Extensions.Diagnostics.Testing/Microsoft.Extensions.Diagnostics.Testing.csproj @@ -25,11 +25,10 @@ - - + diff --git a/src/Libraries/Microsoft.Extensions.Hosting.Testing/Microsoft.Extensions.Hosting.Testing.csproj b/src/Libraries/Microsoft.Extensions.Hosting.Testing/Microsoft.Extensions.Hosting.Testing.csproj index 942468ef795..1cc91384dcd 100644 --- a/src/Libraries/Microsoft.Extensions.Hosting.Testing/Microsoft.Extensions.Hosting.Testing.csproj +++ b/src/Libraries/Microsoft.Extensions.Hosting.Testing/Microsoft.Extensions.Hosting.Testing.csproj @@ -18,7 +18,6 @@ - diff --git a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Microsoft.Extensions.Http.Diagnostics.csproj b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Microsoft.Extensions.Http.Diagnostics.csproj index 052c54939ba..f6c98baefce 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Microsoft.Extensions.Http.Diagnostics.csproj +++ b/src/Libraries/Microsoft.Extensions.Http.Diagnostics/Microsoft.Extensions.Http.Diagnostics.csproj @@ -38,12 +38,9 @@ - - - diff --git a/src/Libraries/Microsoft.Extensions.Http.Resilience/Internal/Randomizer.cs b/src/Libraries/Microsoft.Extensions.Http.Resilience/Internal/Randomizer.cs index bf9fba95ebb..1192f8d3954 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Resilience/Internal/Randomizer.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Resilience/Internal/Randomizer.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; -using System.Threading; namespace Microsoft.Extensions.Http.Resilience.Internal; @@ -11,9 +10,15 @@ namespace Microsoft.Extensions.Http.Resilience.Internal; internal class Randomizer { - private static readonly ThreadLocal _randomInstance = new(() => new Random()); +#if NET6_0_OR_GREATER + public virtual double NextDouble(double maxValue) => Random.Shared.NextDouble() * maxValue; + + public virtual int NextInt(int maxValue) => Random.Shared.Next(maxValue); +#else + private static readonly System.Threading.ThreadLocal _randomInstance = new(() => new Random()); public virtual double NextDouble(double maxValue) => _randomInstance.Value!.NextDouble() * maxValue; public virtual int NextInt(int maxValue) => _randomInstance.Value!.Next(maxValue); +#endif } diff --git a/src/Libraries/Microsoft.Extensions.Http.Resilience/Polly/HttpClientResiliencePredicates.cs b/src/Libraries/Microsoft.Extensions.Http.Resilience/Polly/HttpClientResiliencePredicates.cs index 3345c75f11a..97c5d37d66d 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Resilience/Polly/HttpClientResiliencePredicates.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Resilience/Polly/HttpClientResiliencePredicates.cs @@ -52,8 +52,7 @@ internal static bool IsTransientHttpException(Exception exception) internal static bool IsHttpConnectionTimeout(in Outcome outcome, in CancellationToken cancellationToken) => !cancellationToken.IsCancellationRequested - && outcome.Exception is OperationCanceledException { Source: "System.Private.CoreLib" } - && outcome.Exception.InnerException is TimeoutException; + && outcome.Exception is OperationCanceledException { Source: "System.Private.CoreLib", InnerException: TimeoutException }; /// /// Determines whether a response contains a transient failure. diff --git a/src/Libraries/Microsoft.Extensions.Http.Resilience/Polly/HttpRetryStrategyOptions.cs b/src/Libraries/Microsoft.Extensions.Http.Resilience/Polly/HttpRetryStrategyOptions.cs index afde7d0372d..e6b4a7f4fec 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Resilience/Polly/HttpRetryStrategyOptions.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Resilience/Polly/HttpRetryStrategyOptions.cs @@ -55,7 +55,7 @@ public bool ShouldRetryAfterHeader DelayGenerator = args => args.Outcome.Result switch { HttpResponseMessage response when RetryAfterHelper.TryParse(response, TimeProvider.System, out var retryAfter) => new ValueTask(retryAfter), - _ => new ValueTask((TimeSpan?)null) + _ => default }; } else diff --git a/src/Libraries/Microsoft.Extensions.Http.Resilience/Routing/RoutingStrategyBuilderExtensions.cs b/src/Libraries/Microsoft.Extensions.Http.Resilience/Routing/RoutingStrategyBuilderExtensions.cs index 0c861de2dfa..d2760422377 100644 --- a/src/Libraries/Microsoft.Extensions.Http.Resilience/Routing/RoutingStrategyBuilderExtensions.cs +++ b/src/Libraries/Microsoft.Extensions.Http.Resilience/Routing/RoutingStrategyBuilderExtensions.cs @@ -129,7 +129,7 @@ private static OptionsBuilder ConfigureOrderedGroup { var optionsCache = new NamedOptionsCache(builder.Name, serviceProvider.GetRequiredService>()); var factory = new OrderedGroupsRoutingStrategyFactory(serviceProvider.GetRequiredService(), optionsCache); - return () => factory.Get(); + return factory.Get; }); return builder.Services.AddOptionsWithValidateOnStart(builder.Name); @@ -141,7 +141,7 @@ private static OptionsBuilder ConfigureWeightedGro { var optionsCache = new NamedOptionsCache(builder.Name, serviceProvider.GetRequiredService>()); var factory = new WeightedGroupsRoutingStrategyFactory(serviceProvider.GetRequiredService(), optionsCache); - return () => factory.Get(); + return factory.Get; }); return builder.Services.AddOptionsWithValidateOnStart(builder.Name); diff --git a/src/Libraries/Microsoft.Extensions.Resilience/Microsoft.Extensions.Resilience.csproj b/src/Libraries/Microsoft.Extensions.Resilience/Microsoft.Extensions.Resilience.csproj index f1a2eabe129..ebd63256933 100644 --- a/src/Libraries/Microsoft.Extensions.Resilience/Microsoft.Extensions.Resilience.csproj +++ b/src/Libraries/Microsoft.Extensions.Resilience/Microsoft.Extensions.Resilience.csproj @@ -6,13 +6,8 @@ - true - true - true - true true true - true true @@ -28,12 +23,10 @@ - - diff --git a/src/Libraries/Microsoft.Extensions.Telemetry/Enrichment/ApplicationEnricherServiceCollectionExtensions.cs b/src/Libraries/Microsoft.Extensions.Telemetry/Enrichment/ApplicationEnricherServiceCollectionExtensions.cs index 75bb01c4a8b..b42eb799105 100644 --- a/src/Libraries/Microsoft.Extensions.Telemetry/Enrichment/ApplicationEnricherServiceCollectionExtensions.cs +++ b/src/Libraries/Microsoft.Extensions.Telemetry/Enrichment/ApplicationEnricherServiceCollectionExtensions.cs @@ -41,7 +41,7 @@ public static IServiceCollection AddServiceLogEnricher(this IServiceCollection s return services .AddStaticLogEnricher() - .AddLogEnricherOptions(configure); + .Configure(configure); } /// @@ -58,21 +58,6 @@ public static IServiceCollection AddServiceLogEnricher(this IServiceCollection s return services .AddStaticLogEnricher() - .AddLogEnricherOptions(_ => { }, section); - } - - private static IServiceCollection AddLogEnricherOptions( - this IServiceCollection services, - Action configure, - IConfigurationSection? section = null) - { - _ = services.Configure(configure); - - if (section is not null) - { - _ = services.Configure(section); - } - - return services; + .Configure(section); } } diff --git a/src/Libraries/Microsoft.Extensions.Telemetry/Enrichment/ProcessEnricherServiceCollectionExtensions.cs b/src/Libraries/Microsoft.Extensions.Telemetry/Enrichment/ProcessEnricherServiceCollectionExtensions.cs index bb987eb90bb..7a1f1568c83 100644 --- a/src/Libraries/Microsoft.Extensions.Telemetry/Enrichment/ProcessEnricherServiceCollectionExtensions.cs +++ b/src/Libraries/Microsoft.Extensions.Telemetry/Enrichment/ProcessEnricherServiceCollectionExtensions.cs @@ -42,7 +42,7 @@ public static IServiceCollection AddProcessLogEnricher(this IServiceCollection s return services .AddLogEnricher() .AddStaticLogEnricher() - .AddLogEnricherOptions(configure); + .Configure(configure); } /// @@ -60,21 +60,6 @@ public static IServiceCollection AddProcessLogEnricher(this IServiceCollection s return services .AddLogEnricher() .AddStaticLogEnricher() - .AddLogEnricherOptions(_ => { }, section); - } - - private static IServiceCollection AddLogEnricherOptions( - this IServiceCollection services, - Action configure, - IConfigurationSection? section = null) - { - _ = services.Configure(configure); - - if (section is not null) - { - _ = services.Configure(section); - } - - return services; + .Configure(section); } } diff --git a/src/Libraries/Microsoft.Extensions.Telemetry/Microsoft.Extensions.Telemetry.csproj b/src/Libraries/Microsoft.Extensions.Telemetry/Microsoft.Extensions.Telemetry.csproj index 9ad04ae8892..3d39591e547 100644 --- a/src/Libraries/Microsoft.Extensions.Telemetry/Microsoft.Extensions.Telemetry.csproj +++ b/src/Libraries/Microsoft.Extensions.Telemetry/Microsoft.Extensions.Telemetry.csproj @@ -27,13 +27,10 @@ - - - diff --git a/src/Shared/RentedSpan/RentedSpan.cs b/src/Shared/RentedSpan/RentedSpan.cs index 6e4a837cf7a..c7b429b0b67 100644 --- a/src/Shared/RentedSpan/RentedSpan.cs +++ b/src/Shared/RentedSpan/RentedSpan.cs @@ -90,7 +90,7 @@ public void Dispose() /// When a buffer isn't rented by this type, it's a cue to you to allocate buffer from the stack instead /// using stackalloc. /// - public Span Span => _rentedBuffer != null ? _rentedBuffer.AsSpan(0, _length) : Array.Empty().AsSpan(); + public Span Span => _rentedBuffer != null ? _rentedBuffer.AsSpan(0, _length) : default; /// /// Gets a value indicating whether a buffer has been rented.