Skip to content

Commit e41321d

Browse files
authored
Merge pull request #244 from reisenberger/reisenberger/feature/expandcontextusage
v5.1.0: Allow different parts of an execution to exchange state through `Context` flowing with execution
2 parents 4a35b2e + 81f8806 commit e41321d

File tree

91 files changed

+4780
-2754
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+4780
-2754
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 5.1.0
2+
- Allow different parts of a policy execution to exchange data via a mutable Context travelling with each execution.
3+
14
## 5.0.6
25
- Update NETStandard.Library dependency to latest 1.6.1 for .NetStandard1.0 target. Resolves compatibility for some Xamarin targets.
36

@@ -33,12 +36,14 @@
3336
## 5.0.0 alpha
3437

3538
A major release, adding significant new resilience policies:
39+
3640
- Timeout policy: allows timing out any execution. Thanks to [@reisenberger](https://github.com/reisenberger).
3741
- Bulkhead isolation policy: limits the resources consumable by governed actions, such that a faulting channel cannot cause cascading failures. Thanks to [@reisenberger](https://github.com/reisenberger) and contributions from [@brunolauze](https://github.com/brunolauze).
3842
- Fallback policy: provides for a fallback execution or value, in case of overall failure. Thanks to [@reisenberger](https://github.com/reisenberger)
3943
- PolicyWrap: allows flexibly combining Policy instances of any type, to form an overall resilience strategy. Thanks to [@reisenberger](https://github.com/reisenberger)
4044

4145
Other changes include:
46+
4247
- Add PolicyKeys and context to all policy executions, for logging and to support later introduction of policy events and metrics. Thanks to [@reisenberger](https://github.com/reisenberger)
4348
- Add CancellationToken support to synchronous executions. Thanks to [@brunolauze](https://github.com/brunolauze) and [@reisenberger](https://github.com/reisenberger)
4449
- Add some missing ExecuteAndCapture/Async overloads. Thanks to [@reisenberger](https://github.com/reisenberger)

GitVersionConfig.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
next-version: 5.0.6
1+
next-version: 5.1.0

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ From Polly v4.3.0 onwards, policies wrapping calls returning a `TResult` can als
7979
```csharp
8080
// Handle return value with condition
8181
Policy
82-
.HandleResult<HttpResponse>(r => r.StatusCode == 404)
82+
.HandleResult<HttpResponseMessage>(r => r.StatusCode == 404)
8383

8484
// Handle multiple return values
8585
Policy
86-
.HandleResult<HttpResponse>(r => r.StatusCode == 500)
87-
.OrResult<HttpResponse>(r => r.StatusCode == 502)
86+
.HandleResult<HttpResponseMessage>(r => r.StatusCode == 500)
87+
.OrResult<HttpResponseMessage>(r => r.StatusCode == 502)
8888

8989
// Handle primitive return values (implied use of .Equals())
9090
Policy
@@ -93,9 +93,9 @@ Policy
9393

9494
// Handle both exceptions and return values in one policy
9595
int[] httpStatusCodesWorthRetrying = {408, 500, 502, 503, 504};
96-
HttpResponse result = Policy
97-
.Handle<HttpException>()
98-
.OrResult<HttpResponse>(r => httpStatusCodesWorthRetrying.Contains(r.StatusCode))
96+
HttpResponseMessage result = Policy
97+
.Handle<HttpResponseException>()
98+
.OrResult<HttpResponseMessage>(r => httpStatusCodesWorthRetrying.Contains(r.StatusCode))
9999
```
100100

101101
For more information, see [Handling Return Values](#handing-return-values-and-policytresult) at foot of this readme.
@@ -723,11 +723,11 @@ As described at step 1b, from Polly v4.3.0 onwards, policies can handle return v
723723
```csharp
724724
// Handle both exceptions and return values in one policy
725725
int[] httpStatusCodesWorthRetrying = {408, 500, 502, 503, 504};
726-
HttpResponse result = Policy
727-
.Handle<HttpException>()
728-
.OrResult<HttpResponse>(r => httpStatusCodesWorthRetrying.Contains(r.StatusCode))
726+
HttpResponseMessage result = Policy
727+
.Handle<HttpResponseException>()
728+
.OrResult<HttpResponseMessage>(r => httpStatusCodesWorthRetrying.Contains(r.StatusCode))
729729
.Retry(...)
730-
.Execute( /* some Func<HttpResponse> */ )
730+
.Execute( /* some Func<HttpResponseMessage> */ )
731731
```
732732

733733
The exceptions and return results to handle can be expressed fluently in any order.
@@ -825,6 +825,7 @@ For details of changes by release see the [change log](https://github.com/App-vN
825825
* [@lakario](https://github.com/lakario) - Tidy CircuitBreaker LastException property.
826826
* [@lakario](https://github.com/lakario) - Add NoOpPolicy.
827827
* [@Julien-Mialon](https://github.com/Julien-Mialon) - Fixes, support and examples for .NETStandard compatibility with Xamarin PCL projects
828+
* [@reisenberger](https://github.com/reisenberger) - Add mutable Context and extra overloads taking Context. Allows different parts of a policy execution to exchange data via the mutable Context travelling with each execution.
828829
829830
# Sample Projects
830831

src/Polly.Net40Async.nuspec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
<releaseNotes>
1616
v5.0 is a major release with significant new resilience policies: Timeout; Bulkhead Isolation; Fallback; and PolicyWrap. See release notes back to v5.0.0 for full details.
1717

18+
5.1.0
19+
---------------------
20+
- Allow different parts of a policy execution to exchange data via a mutable Context travelling with each execution.
21+
1822
5.0.6
1923
---------------------
2024
- (.NETStandard1.0 changes only.)

src/Polly.NetStandard10/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using System.Runtime.CompilerServices;
44

55
[assembly: AssemblyTitle("Polly")]
6-
[assembly: AssemblyVersion("5.0.6.0")]
6+
[assembly: AssemblyVersion("5.1.0.0")]
77
[assembly: CLSCompliant(true)]
88

99
[assembly: InternalsVisibleTo("Polly.Pcl.Specs")]

src/Polly.Shared/Bulkhead/BulkheadEngine.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Polly.Bulkhead
1313
internal static partial class BulkheadEngine
1414
{
1515
internal static TResult Implementation<TResult>(
16-
Func<CancellationToken, TResult> action,
16+
Func<Context, CancellationToken, TResult> action,
1717
Context context,
1818
Action<Context> onBulkheadRejected,
1919
SemaphoreSlim maxParallelizationSemaphore,
@@ -31,7 +31,7 @@ internal static TResult Implementation<TResult>(
3131
maxParallelizationSemaphore.Wait(cancellationToken);
3232
try
3333
{
34-
return action(cancellationToken);
34+
return action(context, cancellationToken);
3535
}
3636
finally
3737
{

src/Polly.Shared/Bulkhead/BulkheadEngineAsync.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Polly.Bulkhead
1414
internal static partial class BulkheadEngine
1515
{
1616
internal static async Task<TResult> ImplementationAsync<TResult>(
17-
Func<CancellationToken, Task<TResult>> action,
17+
Func<Context, CancellationToken, Task<TResult>> action,
1818
Context context,
1919
Func<Context, Task> onBulkheadRejectedAsync,
2020
SemaphoreSlim maxParallelizationSemaphore,
@@ -33,7 +33,7 @@ internal static async Task<TResult> ImplementationAsync<TResult>(
3333

3434
try
3535
{
36-
return await action(cancellationToken).ConfigureAwait(continueOnCapturedContext);
36+
return await action(context, cancellationToken).ConfigureAwait(continueOnCapturedContext);
3737
}
3838
finally
3939
{

src/Polly.Shared/Bulkhead/BulkheadPolicy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public partial class BulkheadPolicy : Policy, IDisposable
2121
private readonly int _maxQueueingActions;
2222

2323
internal BulkheadPolicy(
24-
Action<Action<CancellationToken>, Context, CancellationToken> exceptionPolicy,
24+
Action<Action<Context, CancellationToken>, Context, CancellationToken> exceptionPolicy,
2525
int maxParallelization,
2626
int maxQueueingActions,
2727
SemaphoreSlim maxParallelizationSemaphore,
@@ -66,7 +66,7 @@ public partial class BulkheadPolicy<TResult> : Policy<TResult>, IDisposable
6666
private readonly int _maxQueueingActions;
6767

6868
internal BulkheadPolicy(
69-
Func<Func<CancellationToken, TResult>, Context, CancellationToken, TResult> executionPolicy,
69+
Func<Func<Context, CancellationToken, TResult>, Context, CancellationToken, TResult> executionPolicy,
7070
int maxParallelization,
7171
int maxQueueingActions,
7272
SemaphoreSlim maxParallelizationSemaphore,

src/Polly.Shared/Bulkhead/BulkheadPolicyAsync.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Polly.Bulkhead
1313
{
1414
public partial class BulkheadPolicy
1515
{
16-
internal BulkheadPolicy(Func<Func<CancellationToken, Task>, Context, CancellationToken, bool, Task> asyncExceptionPolicy,
16+
internal BulkheadPolicy(Func<Func<Context, CancellationToken, Task>, Context, CancellationToken, bool, Task> asyncExceptionPolicy,
1717
int maxParallelization,
1818
int maxQueueingActions,
1919
SemaphoreSlim maxParallelizationSemaphore,
@@ -30,7 +30,7 @@ internal BulkheadPolicy(Func<Func<CancellationToken, Task>, Context, Cancellatio
3030
public partial class BulkheadPolicy<TResult>
3131
{
3232
internal BulkheadPolicy(
33-
Func<Func<CancellationToken, Task<TResult>>, Context, CancellationToken, bool, Task<TResult>> asyncExecutionPolicy,
33+
Func<Func<Context, CancellationToken, Task<TResult>>, Context, CancellationToken, bool, Task<TResult>> asyncExecutionPolicy,
3434
int maxParallelization,
3535
int maxQueueingActions,
3636
SemaphoreSlim maxParallelizationSemaphore,

src/Polly.Shared/Bulkhead/BulkheadSyntax.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static BulkheadPolicy Bulkhead(int maxParallelization, int maxQueuingActi
8181

8282
return new BulkheadPolicy(
8383
(action, context, cancellationToken) => BulkheadEngine.Implementation(
84-
ct => { action(ct); return EmptyStruct.Instance; },
84+
(ctx, ct) => { action(ctx, ct); return EmptyStruct.Instance; },
8585
context,
8686
onBulkheadRejected,
8787
maxParallelizationSemaphore,

0 commit comments

Comments
 (0)