App-vNext / Polly

Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. From version 6.0.1, Polly targets .NET Standard 1.1 and 2.0+.
https://www.thepollyproject.org
BSD 3-Clause "New" or "Revised" License
13.46k stars 1.23k forks source link

[Feature request]: Add AddStrategy API with no options #1985

Closed eerhardt closed 7 months ago

eerhardt commented 9 months ago

Is your feature request related to a specific problem? Or an existing feature?

The AddStrategy extension methods take a ResilienceStrategyOptions options argument, which gets validated using DataAnnotations. This makes the method incompatible with trimming and native AOT, and it is annotated as such:

https://github.com/App-vNext/Polly/blob/82474bf10b5097d4bf6934100c9daa1efdb02f4b/src/Polly.Core/ResiliencePipelineBuilderExtensions.cs#L68-L70

https://github.com/App-vNext/Polly/blob/82474bf10b5097d4bf6934100c9daa1efdb02f4b/src/Polly.Core/ResiliencePipelineBuilderExtensions.cs#L90-L93

https://github.com/App-vNext/Polly/blob/82474bf10b5097d4bf6934100c9daa1efdb02f4b/src/Polly.Core/ResiliencePipelineBuilderExtensions.cs#L114-L117

Callers who want to use this API in a trim compatible way need to define an empty class derived from ResilienceStrategyOptions and suppress the warning from calling this API.

Describe the solution you'd like

We should add overloads to these AddStrategy methods that are trim compatible, and don't take an options argument. Similar to the AddPipeline methods.

 [RequiresUnreferencedCode(Constants.OptionsValidation)] 
 public static TBuilder AddStrategy<TBuilder>(this TBuilder builder, Func<StrategyBuilderContext, ResilienceStrategy> factory, ResilienceStrategyOptions options) 
     where TBuilder : ResiliencePipelineBuilderBase  {}

+public static TBuilder AddStrategy<TBuilder>(this TBuilder builder, Func<StrategyBuilderContext, ResilienceStrategy> factory) 
+    where TBuilder : ResiliencePipelineBuilderBase  {}

 [RequiresUnreferencedCode(Constants.OptionsValidation)] 
 public static ResiliencePipelineBuilder AddStrategy( 
     this ResiliencePipelineBuilder builder, Func<StrategyBuilderContext, ResilienceStrategy<object>> factory, 
     ResilienceStrategyOptions options) {}

+public static ResiliencePipelineBuilder AddStrategy(
+    this ResiliencePipelineBuilder builder, Func<StrategyBuilderContext, ResilienceStrategy<object>> factory) {}

 [RequiresUnreferencedCode(Constants.OptionsValidation)] 
 public static ResiliencePipelineBuilder<TResult> AddStrategy<TResult>( 
     this ResiliencePipelineBuilder<TResult> builder, Func<StrategyBuilderContext, ResilienceStrategy<TResult>> factory, 
     ResilienceStrategyOptions options) {}

+ public static ResiliencePipelineBuilder<TResult> AddStrategy<TResult>(
+    this ResiliencePipelineBuilder<TResult> builder, Func<StrategyBuilderContext, ResilienceStrategy<TResult>> factory) {}

Additional context

This is being used by Microsoft.Extensions.Http.Resilience here: https://github.com/dotnet/extensions/pull/4962#discussion_r1497965809

Adding this API would make the caller simpler. It wouldn't need to declare an empty class, and suppress the trim warning.

martincostello commented 9 months ago

Seems reasonable to me, plus would make the API easier to use even without wanting to use AoT.

chrbkr commented 8 months ago

This seems to be actually a bug, at least in the documentation. The options parameter in the ResiliencePipelineBuilderExtensions.AddStrategy methods is described like this:

The options associated with the strategy. If none are provided the default instance of ResilienceStrategyOptions is created.

So one would expect the parameter already to be optional. There is the ´EmptyOptions´ class, but it's internal, so I need to copy it if I don't need any options for my strategy.

martintmk commented 7 months ago

This seems to be actually a bug, at least in the documentation. The options parameter in the ResiliencePipelineBuilderExtensions.AddStrategy methods is described like this:

The options associated with the strategy. If none are provided the default instance of ResilienceStrategyOptions is created.

So one would expect the parameter already to be optional. There is the ´EmptyOptions´ class, but it's internal, so I need to copy it if I don't need any options for my strategy.

These are incorrect docs, thanks for noticing. Both new API and fixed docs are addressed in #2068.

cc @eerhardt