From 0cebeea2cdae10490a5c944600a0af0de4b74f6f Mon Sep 17 00:00:00 2001 From: martintmk <103487740+martintmk@users.noreply.github.com> Date: Tue, 25 Jul 2023 11:41:20 +0200 Subject: [PATCH] Improve usability of ResilienceStrategy (#1428) --- src/Polly.Core/PublicAPI.Unshipped.txt | 24 ++++++++--------- .../ResilienceStrategy.TResult.Async.cs | 25 ++++++++++++------ .../ResilienceStrategy.TResult.Sync.cs | 26 ++++++++++++++----- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/src/Polly.Core/PublicAPI.Unshipped.txt b/src/Polly.Core/PublicAPI.Unshipped.txt index 87c39641991..29884cc11e7 100644 --- a/src/Polly.Core/PublicAPI.Unshipped.txt +++ b/src/Polly.Core/PublicAPI.Unshipped.txt @@ -248,18 +248,18 @@ Polly.ResilienceStrategy.ExecuteAsync(System.Func(System.Func! callback, TState state, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask Polly.ResilienceStrategy.ExecuteOutcomeAsync(System.Func>>! callback, Polly.ResilienceContext! context, TState state) -> System.Threading.Tasks.ValueTask> Polly.ResilienceStrategy.ResilienceStrategy() -> void -Polly.ResilienceStrategy -Polly.ResilienceStrategy.Execute(System.Func! callback, Polly.ResilienceContext! context) -> TResult -Polly.ResilienceStrategy.Execute(System.Func! callback, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> TResult -Polly.ResilienceStrategy.Execute(System.Func! callback) -> TResult -Polly.ResilienceStrategy.Execute(System.Func! callback, Polly.ResilienceContext! context, TState state) -> TResult -Polly.ResilienceStrategy.Execute(System.Func! callback, TState state, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> TResult -Polly.ResilienceStrategy.Execute(System.Func! callback, TState state) -> TResult -Polly.ResilienceStrategy.ExecuteAsync(System.Func>! callback, Polly.ResilienceContext! context) -> System.Threading.Tasks.ValueTask -Polly.ResilienceStrategy.ExecuteAsync(System.Func>! callback, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -Polly.ResilienceStrategy.ExecuteAsync(System.Func>! callback, Polly.ResilienceContext! context, TState state) -> System.Threading.Tasks.ValueTask -Polly.ResilienceStrategy.ExecuteAsync(System.Func>! callback, TState state, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask -Polly.ResilienceStrategy.ExecuteOutcomeAsync(System.Func>>! callback, Polly.ResilienceContext! context, TState state) -> System.Threading.Tasks.ValueTask> +Polly.ResilienceStrategy +Polly.ResilienceStrategy.Execute(System.Func! callback, Polly.ResilienceContext! context, TState state) -> TResult +Polly.ResilienceStrategy.Execute(System.Func! callback, TState state, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> TResult +Polly.ResilienceStrategy.Execute(System.Func! callback, TState state) -> TResult +Polly.ResilienceStrategy.Execute(System.Func! callback, Polly.ResilienceContext! context) -> TResult +Polly.ResilienceStrategy.Execute(System.Func! callback, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> TResult +Polly.ResilienceStrategy.Execute(System.Func! callback) -> TResult +Polly.ResilienceStrategy.ExecuteAsync(System.Func>! callback, Polly.ResilienceContext! context, TState state) -> System.Threading.Tasks.ValueTask +Polly.ResilienceStrategy.ExecuteAsync(System.Func>! callback, TState state, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +Polly.ResilienceStrategy.ExecuteAsync(System.Func>! callback, Polly.ResilienceContext! context) -> System.Threading.Tasks.ValueTask +Polly.ResilienceStrategy.ExecuteAsync(System.Func>! callback, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.ValueTask +Polly.ResilienceStrategy.ExecuteOutcomeAsync(System.Func>>! callback, Polly.ResilienceContext! context, TState state) -> System.Threading.Tasks.ValueTask> Polly.ResilienceStrategyBuilder Polly.ResilienceStrategyBuilder.Build() -> Polly.ResilienceStrategy! Polly.ResilienceStrategyBuilder.ResilienceStrategyBuilder() -> void diff --git a/src/Polly.Core/ResilienceStrategy.TResult.Async.cs b/src/Polly.Core/ResilienceStrategy.TResult.Async.cs index da8df1b7f69..c5c9752a759 100644 --- a/src/Polly.Core/ResilienceStrategy.TResult.Async.cs +++ b/src/Polly.Core/ResilienceStrategy.TResult.Async.cs @@ -7,12 +7,12 @@ namespace Polly; /// /// Resilience strategy is used to execute the user-provided callbacks. /// -/// The type of result this strategy supports. +/// The type of result this strategy supports. /// -/// Resilience strategy supports various types of callbacks of result type +/// Resilience strategy supports various types of callbacks of result type /// and provides a unified way to execute them. This includes overloads for synchronous and asynchronous callbacks. /// -public partial class ResilienceStrategy +public partial class ResilienceStrategy { internal ResilienceStrategy(ResilienceStrategy strategy) => Strategy = strategy; @@ -21,16 +21,18 @@ public partial class ResilienceStrategy /// /// Executes the specified callback. /// + /// The type of the result. /// The type of state associated with the callback. /// The user-provided callback. /// The context associated with the callback. /// The state associated with the callback. /// The instance of that represents the asynchronous execution. /// Thrown when or is . - public ValueTask ExecuteAsync( + public ValueTask ExecuteAsync( Func> callback, ResilienceContext context, TState state) + where TResult : T { Guard.NotNull(callback); Guard.NotNull(context); @@ -41,13 +43,15 @@ public ValueTask ExecuteAsync( /// /// Executes the specified callback. /// + /// The type of the result. /// The user-provided callback. /// The context associated with the callback. /// The instance of that represents the asynchronous execution. /// Thrown when or is . - public ValueTask ExecuteAsync( + public ValueTask ExecuteAsync( Func> callback, ResilienceContext context) + where TResult : T { Guard.NotNull(callback); Guard.NotNull(context); @@ -58,16 +62,18 @@ public ValueTask ExecuteAsync( /// /// Executes the specified callback. /// + /// The type of the result. /// The type of state associated with the callback. /// The user-provided callback. /// The state associated with the callback. /// The associated with the callback. /// The instance of that represents the asynchronous execution. /// Thrown when is . - public ValueTask ExecuteAsync( + public ValueTask ExecuteAsync( Func> callback, TState state, CancellationToken cancellationToken = default) + where TResult : T { Guard.NotNull(callback); @@ -77,11 +83,12 @@ public ValueTask ExecuteAsync( /// /// Executes the specified callback. /// + /// The type of the result. /// The user-provided callback. /// The associated with the callback. /// The instance of that represents the asynchronous execution. /// Thrown when is . - public ValueTask ExecuteAsync( + public ValueTask ExecuteAsync( Func> callback, CancellationToken cancellationToken = default) { @@ -93,6 +100,7 @@ public ValueTask ExecuteAsync( /// /// Executes the specified outcome-based callback. /// + /// The type of the result. /// The type of state associated with the callback. /// The user-provided callback. /// The context associated with the callback. @@ -103,10 +111,11 @@ public ValueTask ExecuteAsync( /// This method is for advanced and high performance scenarios. The caller must make sure that the /// does not throw any exceptions. Instead, it converts them to . /// - public ValueTask> ExecuteOutcomeAsync( + public ValueTask> ExecuteOutcomeAsync( Func>> callback, ResilienceContext context, TState state) + where TResult : T { Guard.NotNull(callback); Guard.NotNull(context); diff --git a/src/Polly.Core/ResilienceStrategy.TResult.Sync.cs b/src/Polly.Core/ResilienceStrategy.TResult.Sync.cs index 65149ee2c48..b21034af0e0 100644 --- a/src/Polly.Core/ResilienceStrategy.TResult.Sync.cs +++ b/src/Polly.Core/ResilienceStrategy.TResult.Sync.cs @@ -2,21 +2,23 @@ namespace Polly; #pragma warning disable RS0027 // API with optional parameter(s) should have the most parameters amongst its public overloads -public partial class ResilienceStrategy +public partial class ResilienceStrategy { /// /// Executes the specified callback. /// + /// The type of the result. /// The type of state associated with the callback. /// The user-provided callback. /// The context associated with the callback. /// The state associated with the callback. /// An instance of that represents the asynchronous execution. /// Thrown when or is . - public TResult Execute( + public TResult Execute( Func callback, ResilienceContext context, TState state) + where TResult : T { Guard.NotNull(callback); Guard.NotNull(context); @@ -27,13 +29,15 @@ public TResult Execute( /// /// Executes the specified callback. /// + /// The type of the result. /// The user-provided callback. /// The context associated with the callback. /// An instance of that represents the asynchronous execution. /// Thrown when or is . - public TResult Execute( + public TResult Execute( Func callback, ResilienceContext context) + where TResult : T { Guard.NotNull(callback); Guard.NotNull(context); @@ -44,13 +48,15 @@ public TResult Execute( /// /// Executes the specified callback. /// + /// The type of the result. /// The user-provided callback. /// The associated with the callback. /// An instance of that represents the asynchronous execution. /// Thrown when is . - public TResult Execute( + public TResult Execute( Func callback, CancellationToken cancellationToken = default) + where TResult : T { Guard.NotNull(callback); @@ -60,10 +66,12 @@ public TResult Execute( /// /// Executes the specified callback. /// + /// The type of the result. /// The user-provided callback. /// An instance of that represents the asynchronous execution. /// Thrown when is . - public TResult Execute(Func callback) + public TResult Execute(Func callback) + where TResult : T { Guard.NotNull(callback); @@ -73,12 +81,14 @@ public TResult Execute(Func callback) /// /// Executes the specified callback. /// + /// The type of the result. /// The type of state associated with the callback. /// The user-provided callback. /// The state associated with the callback. /// An instance of that represents the asynchronous execution. /// Thrown when is . - public TResult Execute(Func callback, TState state) + public TResult Execute(Func callback, TState state) + where TResult : T { Guard.NotNull(callback); @@ -88,16 +98,18 @@ public TResult Execute(Func callback, TState state) /// /// Executes the specified callback. /// + /// The type of the result. /// The type of state associated with the callback. /// The user-provided callback. /// The state associated with the callback. /// The associated with the callback. /// An instance of that represents the asynchronous execution. /// Thrown when is . - public TResult Execute( + public TResult Execute( Func callback, TState state, CancellationToken cancellationToken = default) + where TResult : T { Guard.NotNull(callback);