From 57aabf10af60d5f5d66de04eac0a09d8775943e3 Mon Sep 17 00:00:00 2001 From: peter-csala Date: Tue, 31 Oct 2023 11:44:01 +0100 Subject: [PATCH 1/8] Revise Policies, Retry --- docs/migration-v8.md | 114 ++++++++++++------------ src/Snippets/Docs/Migration.Policies.cs | 74 ++++++++------- src/Snippets/Docs/Migration.Retry.cs | 28 +++--- 3 files changed, 106 insertions(+), 110 deletions(-) diff --git a/docs/migration-v8.md b/docs/migration-v8.md index 8b44e18e651..71b5ad7bb98 100644 --- a/docs/migration-v8.md +++ b/docs/migration-v8.md @@ -27,66 +27,67 @@ This section describes how to migrate from execution policies (i.e. `IAsyncPolic In earlier versions, Polly exposed various interfaces to execute user code: -- `IAsyncPolicy` -- `IAsyncPolicy` - `ISyncPolicy` +- `IAsyncPolicy` - `ISyncPolicy` +- `IAsyncPolicy` These interfaces were created and used as shown below: ```cs // Create and use the ISyncPolicy. -ISyncPolicy syncPolicy = Policy.Handle().WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); +ISyncPolicy syncPolicy = Policy + .Handle() + .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); + syncPolicy.Execute(() => { - // Your code here + // Your code goes here }); // Create and use the IAsyncPolicy -IAsyncPolicy asyncPolicy = Policy.Handle().WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(1)); -await asyncPolicy.ExecuteAsync( - async cancellationToken => - { - // Your code here - }, - cancellationToken); +IAsyncPolicy asyncPolicy = Policy + .Handle() + .WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(1)); +await asyncPolicy.ExecuteAsync(async token => +{ + // Your code goes here +}, cancellationToken); // Create and use the ISyncPolicy -ISyncPolicy syncPolicyT = Policy - .HandleResult(r => !r.IsSuccessStatusCode) +ISyncPolicy syncPolicyT = Policy + .HandleResult(result => !result.IsSuccessStatusCode) .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); syncPolicyT.Execute(() => { - // Your code here + // Your code goes here return GetResponse(); }); // Create and use the IAsyncPolicy -IAsyncPolicy asyncPolicyT = Policy - .HandleResult(r => !r.IsSuccessStatusCode) +IAsyncPolicy asyncPolicyT = Policy + .HandleResult(result => !result.IsSuccessStatusCode) .WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(1)); -await asyncPolicyT.ExecuteAsync( - async cancellationToken => - { - // Your code here - return await GetResponseAsync(cancellationToken); - }, - cancellationToken); + +await asyncPolicyT.ExecuteAsync(async token => +{ + // Your code goes here + return await GetResponseAsync(token); +}, cancellationToken); ``` ### Configuring strategies in v8 -In Polly v8, the previous code becomes: +In Polly v8, there are no such interfaces. The previous samples become: ```cs // Create and use the ResiliencePipeline. // -// The ResiliencePipelineBuilder is used to start building the resilience pipeline, -// instead of the static Policy.HandleException() call. +// Use the ResiliencePipelineBuilder to start building the resilience pipeline ResiliencePipeline pipeline = new ResiliencePipelineBuilder() .AddRetry(new RetryStrategyOptions { @@ -98,23 +99,21 @@ ResiliencePipeline pipeline = new ResiliencePipelineBuilder() .Build(); // After all necessary strategies are added, call Build() to create the pipeline. // Synchronous execution -pipeline.Execute(() => +pipeline.Execute(static () => { - // Your code here + // Your code goes here }); // Asynchronous execution is also supported with the same pipeline instance -await pipeline.ExecuteAsync(static async cancellationToken => +await pipeline.ExecuteAsync(static async token => { - // Your code here -}, -cancellationToken); + // Your code goes here +}, cancellationToken); // Create and use the ResiliencePipeline. // // Building of generic resilience pipeline is very similar to non-generic one. -// Notice the use of generic RetryStrategyOptions to configure the strategy -// as opposed to providing the arguments into the method. +// Notice the use of generic RetryStrategyOptions to configure the strategy. ResiliencePipeline pipelineT = new ResiliencePipelineBuilder() .AddRetry(new RetryStrategyOptions { @@ -128,19 +127,18 @@ ResiliencePipeline pipelineT = new ResiliencePipelineBuilde .Build(); // Synchronous execution -pipelineT.Execute(() => +pipelineT.Execute(static () => { - // Your code here + // Your code goes here return GetResponse(); }); // Asynchronous execution -await pipelineT.ExecuteAsync(static async cancellationToken => +await pipelineT.ExecuteAsync(static async token => { - // Your code here - return await GetResponseAsync(cancellationToken); -}, -cancellationToken); + // Your code goes here + return await GetResponseAsync(token); +}, cancellationToken); ``` @@ -148,7 +146,7 @@ cancellationToken); ### Policy wrap in v7 -Policy wrap is used to combine multiple policies into one as shown in the v7 example below: +Policy wrap is used to combine multiple policies into one: ```cs @@ -165,7 +163,7 @@ IAsyncPolicy wrappedPolicy = Policy.WrapAsync(retryPolicy, timeoutPolicy); ### Policy wrap in v8 -In v8, there's no need to use policy wrap explicitly. Instead, policy wrapping is integrated into `ResiliencePipelineBuilder`, as shown in the example below: +In v8, there's no need to use policy wrap explicitly. Instead, policy wrapping is integrated into `ResiliencePipelineBuilder`: ```cs @@ -190,7 +188,7 @@ ResiliencePipeline pipeline = new ResiliencePipelineBuilder() ## Migrating retry policies -This section describes how to migrate the v7 retry policy to a resilience strategy in v8. +This section describes how to migrate v7 retry policies to V8 retry strategies. ### Retry in v7 @@ -200,13 +198,13 @@ In v7 the retry policy is configured as: ```cs // Retry once Policy - .Handle() - .Retry(); + .Handle() + .Retry(); // Retry multiple times Policy - .Handle() - .Retry(3); + .Handle() + .Retry(3); // Retry multiple times with callback Policy @@ -215,6 +213,11 @@ Policy { // Add logic to be executed before each retry, such as logging }); + +// Retry forever +Policy + .Handle() + .RetryForever(); ``` @@ -278,15 +281,10 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions ```cs -// Retry forever -Policy - .Handle() - .WaitAndRetryForever(_ => TimeSpan.FromSeconds(1)); - // Wait and retry multiple times Policy - .Handle() - .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); + .Handle() + .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); // Wait and retry multiple times with callback Policy @@ -350,9 +348,9 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions ```cs // Wait and retry with result handling Policy - .Handle() - .OrResult(response => response.StatusCode == HttpStatusCode.InternalServerError) - .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); + .Handle() + .OrResult(result => result.StatusCode == HttpStatusCode.InternalServerError) + .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); ``` diff --git a/src/Snippets/Docs/Migration.Policies.cs b/src/Snippets/Docs/Migration.Policies.cs index 77c1ecaef40..1a926a921f7 100644 --- a/src/Snippets/Docs/Migration.Policies.cs +++ b/src/Snippets/Docs/Migration.Policies.cs @@ -12,43 +12,45 @@ public static async Task Policies() #region migration-policies-v7 // Create and use the ISyncPolicy. - ISyncPolicy syncPolicy = Policy.Handle().WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); + ISyncPolicy syncPolicy = Policy + .Handle() + .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); + syncPolicy.Execute(() => { - // Your code here + // Your code goes here }); // Create and use the IAsyncPolicy - IAsyncPolicy asyncPolicy = Policy.Handle().WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(1)); - await asyncPolicy.ExecuteAsync( - async cancellationToken => - { - // Your code here - }, - cancellationToken); + IAsyncPolicy asyncPolicy = Policy + .Handle() + .WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(1)); + await asyncPolicy.ExecuteAsync(async token => + { + // Your code goes here + }, cancellationToken); // Create and use the ISyncPolicy - ISyncPolicy syncPolicyT = Policy - .HandleResult(r => !r.IsSuccessStatusCode) + ISyncPolicy syncPolicyT = Policy + .HandleResult(result => !result.IsSuccessStatusCode) .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); syncPolicyT.Execute(() => { - // Your code here + // Your code goes here return GetResponse(); }); // Create and use the IAsyncPolicy - IAsyncPolicy asyncPolicyT = Policy - .HandleResult(r => !r.IsSuccessStatusCode) + IAsyncPolicy asyncPolicyT = Policy + .HandleResult(result => !result.IsSuccessStatusCode) .WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(1)); - await asyncPolicyT.ExecuteAsync( - async cancellationToken => - { - // Your code here - return await GetResponseAsync(cancellationToken); - }, - cancellationToken); + + await asyncPolicyT.ExecuteAsync(async token => + { + // Your code goes here + return await GetResponseAsync(token); + }, cancellationToken); #endregion } @@ -61,8 +63,7 @@ public static async Task Strategies() // Create and use the ResiliencePipeline. // - // The ResiliencePipelineBuilder is used to start building the resilience pipeline, - // instead of the static Policy.HandleException() call. + // Use the ResiliencePipelineBuilder to start building the resilience pipeline ResiliencePipeline pipeline = new ResiliencePipelineBuilder() .AddRetry(new RetryStrategyOptions { @@ -74,23 +75,21 @@ public static async Task Strategies() .Build(); // After all necessary strategies are added, call Build() to create the pipeline. // Synchronous execution - pipeline.Execute(() => + pipeline.Execute(static () => { - // Your code here + // Your code goes here }); // Asynchronous execution is also supported with the same pipeline instance - await pipeline.ExecuteAsync(static async cancellationToken => + await pipeline.ExecuteAsync(static async token => { - // Your code here - }, - cancellationToken); + // Your code goes here + }, cancellationToken); // Create and use the ResiliencePipeline. // // Building of generic resilience pipeline is very similar to non-generic one. - // Notice the use of generic RetryStrategyOptions to configure the strategy - // as opposed to providing the arguments into the method. + // Notice the use of generic RetryStrategyOptions to configure the strategy. ResiliencePipeline pipelineT = new ResiliencePipelineBuilder() .AddRetry(new RetryStrategyOptions { @@ -104,19 +103,18 @@ await pipeline.ExecuteAsync(static async cancellationToken => .Build(); // Synchronous execution - pipelineT.Execute(() => + pipelineT.Execute(static () => { - // Your code here + // Your code goes here return GetResponse(); }); // Asynchronous execution - await pipelineT.ExecuteAsync(static async cancellationToken => + await pipelineT.ExecuteAsync(static async token => { - // Your code here - return await GetResponseAsync(cancellationToken); - }, - cancellationToken); + // Your code goes here + return await GetResponseAsync(token); + }, cancellationToken); #endregion } diff --git a/src/Snippets/Docs/Migration.Retry.cs b/src/Snippets/Docs/Migration.Retry.cs index 8947f95a073..b4e0ebf41e2 100644 --- a/src/Snippets/Docs/Migration.Retry.cs +++ b/src/Snippets/Docs/Migration.Retry.cs @@ -15,13 +15,13 @@ public static void Retry_V7() // Retry once Policy - .Handle() - .Retry(); + .Handle() + .Retry(); // Retry multiple times Policy - .Handle() - .Retry(3); + .Handle() + .Retry(3); // Retry multiple times with callback Policy @@ -31,19 +31,19 @@ public static void Retry_V7() // Add logic to be executed before each retry, such as logging }); - #endregion - - #region migration-retry-wait-v7 - // Retry forever Policy .Handle() - .WaitAndRetryForever(_ => TimeSpan.FromSeconds(1)); + .RetryForever(); + + #endregion + + #region migration-retry-wait-v7 // Wait and retry multiple times Policy - .Handle() - .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); + .Handle() + .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); // Wait and retry multiple times with callback Policy @@ -64,9 +64,9 @@ public static void Retry_V7() // Wait and retry with result handling Policy - .Handle() - .OrResult(response => response.StatusCode == HttpStatusCode.InternalServerError) - .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); + .Handle() + .OrResult(result => result.StatusCode == HttpStatusCode.InternalServerError) + .WaitAndRetry(3, _ => TimeSpan.FromSeconds(1)); #endregion } From 1e3e2f93fae47f4da36c903100375bbd11c53206 Mon Sep 17 00:00:00 2001 From: peter-csala Date: Tue, 31 Oct 2023 13:17:17 +0100 Subject: [PATCH 2/8] Add tldr and links at end of each section --- docs/migration-v8.md | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/docs/migration-v8.md b/docs/migration-v8.md index 71b5ad7bb98..5b44a00b784 100644 --- a/docs/migration-v8.md +++ b/docs/migration-v8.md @@ -142,6 +142,16 @@ await pipelineT.ExecuteAsync(static async token => ``` +> [!IMPORTANT] +> +> Things to remember: +> +> - Use `ResiliencePipelineBuilder{}` to build a resiliency pipeline +> - Use one of the `AddXYZ` builder method to add a new strategy to the pipeline +> - Use either `Execute` or `ExecuteAsync` depending on the execution context +> +> For further information please check out the [Resilience pipelines docs](pipelines/index.md) + ## Migrating policy wrap ### Policy wrap in v7 @@ -186,6 +196,16 @@ ResiliencePipeline pipeline = new ResiliencePipelineBuilder() > [!NOTE] > See [fallback after retries](strategies/fallback.md#fallback-after-retries) for an example on how the strategies are executed. +> [!IMPORTANT] +> +> Things to remember: +> +> - Use `ResiliencePipelineBuilder{}` to build a resiliency pipeline +> - Use multiple `AddXYZ` builder methods to add new strategies to your pipeline +> +> For further information please check out the [Resilience pipelines docs](pipelines/index.md) + + ## Migrating retry policies This section describes how to migrate v7 retry policies to V8 retry strategies. @@ -386,7 +406,15 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyO ``` -It's important to remember that the configuration in v8 is options based, i.e. `RetryStrategyOptions` are used. +> [!IMPORTANT] +> +> Things to remember: +> +> - Use `AddRetry` to add a retry strategy to your resiliency pipeline +> - Use the `RetryStrategyOptions` to customize your retry behavior to meet your requirements +> +> For further information please check out the [Retry resilience strategy docs](strategies/retry.md) + ## Migrating rate limit policies From 148d5dae417a446f7235586ebdefb912c0441752 Mon Sep 17 00:00:00 2001 From: peter-csala Date: Tue, 31 Oct 2023 13:35:32 +0100 Subject: [PATCH 3/8] Add tldr and links at end of each section --- docs/migration-v8.md | 45 ++++++++++++++++++++++--- src/Snippets/Docs/Migration.Bulkhead.cs | 16 ++++++--- 2 files changed, 52 insertions(+), 9 deletions(-) diff --git a/docs/migration-v8.md b/docs/migration-v8.md index 5b44a00b784..db8bd88123f 100644 --- a/docs/migration-v8.md +++ b/docs/migration-v8.md @@ -147,7 +147,7 @@ await pipelineT.ExecuteAsync(static async token => > Things to remember: > > - Use `ResiliencePipelineBuilder{}` to build a resiliency pipeline -> - Use one of the `AddXYZ` builder method to add a new strategy to the pipeline +> - Use one of the `AddXYZ` builder methods to add a new strategy to the pipeline > - Use either `Execute` or `ExecuteAsync` depending on the execution context > > For further information please check out the [Resilience pipelines docs](pipelines/index.md) @@ -478,6 +478,15 @@ ResiliencePipeline pipelineT = new ResiliencePipelineBuilde ``` +> [!IMPORTANT] +> +> Things to remember: +> +> - Use `AddRateLimiter` to add a rate limiter strategy to your resiliency pipeline +> - Use one of the derived classes of [`ReplenishingRateLimiter`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.ratelimiting.replenishingratelimiter) to customize your rate limiter behavior to meet your requirements +> +> For further information please check out the [Rate limiter resilience strategy docs](strategies/rate-limiter.md) + ## Migrating bulkhead policies The bulkhead policy is now replaced by the [rate limiter strategy](strategies/rate-limiter.md) which uses the [`System.Threading.RateLimiting`](https://www.nuget.org/packages/System.Threading.RateLimiting) package. The new counterpart to bulkhead is `ConcurrencyLimiter`. @@ -490,16 +499,24 @@ The bulkhead policy is now replaced by the [rate limiter strategy](strategies/ra ```cs // Create sync bulkhead -ISyncPolicy syncPolicy = Policy.Bulkhead(maxParallelization: 100, maxQueuingActions: 50); +ISyncPolicy syncPolicy = Policy.Bulkhead( + maxParallelization: 100, + maxQueuingActions: 50); // Create async bulkhead -IAsyncPolicy asyncPolicy = Policy.BulkheadAsync(maxParallelization: 100, maxQueuingActions: 50); +IAsyncPolicy asyncPolicy = Policy.BulkheadAsync( + maxParallelization: 100, + maxQueuingActions: 50); // Create generic sync bulkhead -ISyncPolicy syncPolicyT = Policy.Bulkhead(maxParallelization: 100, maxQueuingActions: 50); +ISyncPolicy syncPolicyT = Policy.Bulkhead( + maxParallelization: 100, + maxQueuingActions: 50); // Create generic async bulkhead -IAsyncPolicy asyncPolicyT = Policy.BulkheadAsync(maxParallelization: 100, maxQueuingActions: 50); +IAsyncPolicy asyncPolicyT = Policy.BulkheadAsync( + maxParallelization: 100, + maxQueuingActions: 50); ``` @@ -524,6 +541,15 @@ ResiliencePipeline pipelineT = new ResiliencePipelineBuilde ``` +> [!IMPORTANT] +> +> Things to remember: +> +> - Use `AddConcurrencyLimiter` to add a concurrency limiter strategy to your resiliency pipeline +> - Use the `ConcurrencyLimiterOptions` to customize your concurrency limiter behavior to meet your requirements +> +> For further information please check out the [Rate limiter resilience strategy docs](strategies/rate-limiter.md) + ## Migrating timeout policies > [!NOTE] @@ -565,6 +591,15 @@ ResiliencePipeline pipelineT = new ResiliencePipelineBuilde ``` +> [!IMPORTANT] +> +> Things to remember: +> +> - Use `AddTimeout` to add a timeout strategy to your resiliency pipeline +> - Use the `TimeoutStrategyOptions` to customize your timeout behavior to meet your requirements +> +> For further information please check out the [Timeout resilience strategy docs](strategies/timeout.md) + ## Migrating other policies Migrating is a process similar to the ones described in the previous sections. Keep in mind that: diff --git a/src/Snippets/Docs/Migration.Bulkhead.cs b/src/Snippets/Docs/Migration.Bulkhead.cs index a445b7a39d6..5439539def1 100644 --- a/src/Snippets/Docs/Migration.Bulkhead.cs +++ b/src/Snippets/Docs/Migration.Bulkhead.cs @@ -9,16 +9,24 @@ public static void Bulkhead_V7() #region migration-bulkhead-v7 // Create sync bulkhead - ISyncPolicy syncPolicy = Policy.Bulkhead(maxParallelization: 100, maxQueuingActions: 50); + ISyncPolicy syncPolicy = Policy.Bulkhead( + maxParallelization: 100, + maxQueuingActions: 50); // Create async bulkhead - IAsyncPolicy asyncPolicy = Policy.BulkheadAsync(maxParallelization: 100, maxQueuingActions: 50); + IAsyncPolicy asyncPolicy = Policy.BulkheadAsync( + maxParallelization: 100, + maxQueuingActions: 50); // Create generic sync bulkhead - ISyncPolicy syncPolicyT = Policy.Bulkhead(maxParallelization: 100, maxQueuingActions: 50); + ISyncPolicy syncPolicyT = Policy.Bulkhead( + maxParallelization: 100, + maxQueuingActions: 50); // Create generic async bulkhead - IAsyncPolicy asyncPolicyT = Policy.BulkheadAsync(maxParallelization: 100, maxQueuingActions: 50); + IAsyncPolicy asyncPolicyT = Policy.BulkheadAsync( + maxParallelization: 100, + maxQueuingActions: 50); #endregion } From 6b21555349cb2cbedc566673805acb3143260f90 Mon Sep 17 00:00:00 2001 From: peter-csala Date: Tue, 31 Oct 2023 13:55:48 +0100 Subject: [PATCH 4/8] Add missing static keywords --- docs/migration-v8.md | 10 +++++----- src/Snippets/Docs/Migration.Policies.cs | 2 +- src/Snippets/Docs/Migration.Retry.cs | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/migration-v8.md b/docs/migration-v8.md index db8bd88123f..e305cdec25e 100644 --- a/docs/migration-v8.md +++ b/docs/migration-v8.md @@ -119,7 +119,7 @@ ResiliencePipeline pipelineT = new ResiliencePipelineBuilde { ShouldHandle = new PredicateBuilder() .Handle() - .HandleResult(result => !result.IsSuccessStatusCode), + .HandleResult(static result => !result.IsSuccessStatusCode), Delay = TimeSpan.FromSeconds(1), MaxRetryAttempts = 3, BackoffType = DelayBackoffType.Constant @@ -277,7 +277,7 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions ShouldHandle = new PredicateBuilder().Handle(), MaxRetryAttempts = 3, Delay = TimeSpan.Zero, - OnRetry = args => + OnRetry = static args => { // Add logic to be executed before each retry, such as logging return default; @@ -342,7 +342,7 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyOptions MaxRetryAttempts = 3, Delay = TimeSpan.FromSeconds(1), BackoffType = DelayBackoffType.Constant, - OnRetry = args => + OnRetry = static args => { // Add logic to be executed before each retry, such as logging return default; @@ -384,7 +384,7 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyO // PredicateBuilder is a convenience API that can used to configure the ShouldHandle predicate. ShouldHandle = new PredicateBuilder() .Handle() - .HandleResult(result => result.StatusCode == HttpStatusCode.InternalServerError), + .HandleResult(static result => result.StatusCode == HttpStatusCode.InternalServerError), MaxRetryAttempts = 3, }) .Build(); @@ -394,7 +394,7 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyO { // Determine what results to retry using switch expressions. // Note that PredicateResult.True() is just a shortcut for "new ValueTask(true)". - ShouldHandle = args => args.Outcome switch + ShouldHandle = static args => args.Outcome switch { { Exception: SomeExceptionType } => PredicateResult.True(), { Result: { StatusCode: HttpStatusCode.InternalServerError } } => PredicateResult.True(), diff --git a/src/Snippets/Docs/Migration.Policies.cs b/src/Snippets/Docs/Migration.Policies.cs index 1a926a921f7..2c13bd9f50d 100644 --- a/src/Snippets/Docs/Migration.Policies.cs +++ b/src/Snippets/Docs/Migration.Policies.cs @@ -95,7 +95,7 @@ await pipeline.ExecuteAsync(static async token => { ShouldHandle = new PredicateBuilder() .Handle() - .HandleResult(result => !result.IsSuccessStatusCode), + .HandleResult(static result => !result.IsSuccessStatusCode), Delay = TimeSpan.FromSeconds(1), MaxRetryAttempts = 3, BackoffType = DelayBackoffType.Constant diff --git a/src/Snippets/Docs/Migration.Retry.cs b/src/Snippets/Docs/Migration.Retry.cs index b4e0ebf41e2..de97e046166 100644 --- a/src/Snippets/Docs/Migration.Retry.cs +++ b/src/Snippets/Docs/Migration.Retry.cs @@ -105,7 +105,7 @@ public static void Retry_V8() ShouldHandle = new PredicateBuilder().Handle(), MaxRetryAttempts = 3, Delay = TimeSpan.Zero, - OnRetry = args => + OnRetry = static args => { // Add logic to be executed before each retry, such as logging return default; @@ -144,7 +144,7 @@ public static void Retry_V8() MaxRetryAttempts = 3, Delay = TimeSpan.FromSeconds(1), BackoffType = DelayBackoffType.Constant, - OnRetry = args => + OnRetry = static args => { // Add logic to be executed before each retry, such as logging return default; @@ -172,7 +172,7 @@ public static void Retry_V8() // PredicateBuilder is a convenience API that can used to configure the ShouldHandle predicate. ShouldHandle = new PredicateBuilder() .Handle() - .HandleResult(result => result.StatusCode == HttpStatusCode.InternalServerError), + .HandleResult(static result => result.StatusCode == HttpStatusCode.InternalServerError), MaxRetryAttempts = 3, }) .Build(); @@ -182,7 +182,7 @@ public static void Retry_V8() { // Determine what results to retry using switch expressions. // Note that PredicateResult.True() is just a shortcut for "new ValueTask(true)". - ShouldHandle = args => args.Outcome switch + ShouldHandle = static args => args.Outcome switch { { Exception: SomeExceptionType } => PredicateResult.True(), { Result: { StatusCode: HttpStatusCode.InternalServerError } } => PredicateResult.True(), From 189694bb28604ae900b759083d336d1e94709a61 Mon Sep 17 00:00:00 2001 From: peter-csala Date: Tue, 31 Oct 2023 13:59:24 +0100 Subject: [PATCH 5/8] Review extra empty lines --- docs/migration-v8.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/migration-v8.md b/docs/migration-v8.md index e305cdec25e..5831053e0fd 100644 --- a/docs/migration-v8.md +++ b/docs/migration-v8.md @@ -205,7 +205,6 @@ ResiliencePipeline pipeline = new ResiliencePipelineBuilder() > > For further information please check out the [Resilience pipelines docs](pipelines/index.md) - ## Migrating retry policies This section describes how to migrate v7 retry policies to V8 retry strategies. @@ -415,7 +414,6 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyO > > For further information please check out the [Retry resilience strategy docs](strategies/retry.md) - ## Migrating rate limit policies The rate limit policy is now replaced by the [rate limiter strategy](strategies/rate-limiter.md) which uses the [`System.Threading.RateLimiting`](https://www.nuget.org/packages/System.Threading.RateLimiting) package. Polly does not implement its own rate limiter anymore. From f98e09621c784265e4e7af6957a49b0446011714 Mon Sep 17 00:00:00 2001 From: peter-csala Date: Tue, 31 Oct 2023 14:01:13 +0100 Subject: [PATCH 6/8] Fix linting --- docs/migration-v8.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/migration-v8.md b/docs/migration-v8.md index 5831053e0fd..b2ac70c8b66 100644 --- a/docs/migration-v8.md +++ b/docs/migration-v8.md @@ -193,8 +193,7 @@ ResiliencePipeline pipeline = new ResiliencePipelineBuilder() ``` -> [!NOTE] -> See [fallback after retries](strategies/fallback.md#fallback-after-retries) for an example on how the strategies are executed. +See [fallback after retries](strategies/fallback.md#fallback-after-retries) for an example on how the strategies are executed. > [!IMPORTANT] > From 7a187f4f4d6d9b0619faa6edab932e85201371ea Mon Sep 17 00:00:00 2001 From: peter-csala <57183693+peter-csala@users.noreply.github.com> Date: Tue, 31 Oct 2023 15:10:50 +0100 Subject: [PATCH 7/8] Apply suggestions from code review Co-authored-by: Martin Costello --- docs/migration-v8.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/docs/migration-v8.md b/docs/migration-v8.md index b2ac70c8b66..2d4f07260a0 100644 --- a/docs/migration-v8.md +++ b/docs/migration-v8.md @@ -147,10 +147,10 @@ await pipelineT.ExecuteAsync(static async token => > Things to remember: > > - Use `ResiliencePipelineBuilder{}` to build a resiliency pipeline -> - Use one of the `AddXYZ` builder methods to add a new strategy to the pipeline +> - Use one of the `Add*` builder methods to add a new strategy to the pipeline > - Use either `Execute` or `ExecuteAsync` depending on the execution context > -> For further information please check out the [Resilience pipelines docs](pipelines/index.md) +> For further information please check out the [Resilience pipelines documentation](pipelines/index.md). ## Migrating policy wrap @@ -200,9 +200,9 @@ See [fallback after retries](strategies/fallback.md#fallback-after-retries) for > Things to remember: > > - Use `ResiliencePipelineBuilder{}` to build a resiliency pipeline -> - Use multiple `AddXYZ` builder methods to add new strategies to your pipeline +> - Use multiple `Add*` builder methods to add new strategies to your pipeline > -> For further information please check out the [Resilience pipelines docs](pipelines/index.md) +> For further information please check out the [Resilience pipelines documentation](pipelines/index.md). ## Migrating retry policies @@ -411,7 +411,7 @@ new ResiliencePipelineBuilder().AddRetry(new RetryStrategyO > - Use `AddRetry` to add a retry strategy to your resiliency pipeline > - Use the `RetryStrategyOptions` to customize your retry behavior to meet your requirements > -> For further information please check out the [Retry resilience strategy docs](strategies/retry.md) +> For further information please check out the [Retry resilience strategy documentation](strategies/retry.md). ## Migrating rate limit policies @@ -480,9 +480,9 @@ ResiliencePipeline pipelineT = new ResiliencePipelineBuilde > Things to remember: > > - Use `AddRateLimiter` to add a rate limiter strategy to your resiliency pipeline -> - Use one of the derived classes of [`ReplenishingRateLimiter`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.ratelimiting.replenishingratelimiter) to customize your rate limiter behavior to meet your requirements +> - Use one of the derived classes of [`ReplenishingRateLimiter`](https://learn.microsoft.com/dotnet/api/system.threading.ratelimiting.replenishingratelimiter) to customize your rate limiter behavior to meet your requirements > -> For further information please check out the [Rate limiter resilience strategy docs](strategies/rate-limiter.md) +> For further information please check out the [Rate limiter resilience strategy documentation](strategies/rate-limiter.md). ## Migrating bulkhead policies @@ -545,7 +545,7 @@ ResiliencePipeline pipelineT = new ResiliencePipelineBuilde > - Use `AddConcurrencyLimiter` to add a concurrency limiter strategy to your resiliency pipeline > - Use the `ConcurrencyLimiterOptions` to customize your concurrency limiter behavior to meet your requirements > -> For further information please check out the [Rate limiter resilience strategy docs](strategies/rate-limiter.md) +> For further information please check out the [Rate limiter resilience strategy documentation](strategies/rate-limiter.md). ## Migrating timeout policies @@ -595,7 +595,7 @@ ResiliencePipeline pipelineT = new ResiliencePipelineBuilde > - Use `AddTimeout` to add a timeout strategy to your resiliency pipeline > - Use the `TimeoutStrategyOptions` to customize your timeout behavior to meet your requirements > -> For further information please check out the [Timeout resilience strategy docs](strategies/timeout.md) +> For further information please check out the [Timeout resilience strategy documentation](strategies/timeout.md). ## Migrating other policies From aa05cd766482de0a5851c976b362b780c570ee9a Mon Sep 17 00:00:00 2001 From: peter-csala Date: Tue, 31 Oct 2023 16:18:12 +0100 Subject: [PATCH 8/8] Reorder interfaces in doc --- docs/migration-v8.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/migration-v8.md b/docs/migration-v8.md index 2d4f07260a0..0a6179f6dc9 100644 --- a/docs/migration-v8.md +++ b/docs/migration-v8.md @@ -27,10 +27,10 @@ This section describes how to migrate from execution policies (i.e. `IAsyncPolic In earlier versions, Polly exposed various interfaces to execute user code: -- `ISyncPolicy` - `IAsyncPolicy` -- `ISyncPolicy` - `IAsyncPolicy` +- `ISyncPolicy` +- `ISyncPolicy` These interfaces were created and used as shown below: