Skip to content

Commit

Permalink
Cleanup Polly.Core README.md (#1298)
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Jun 14, 2023
1 parent 934eef3 commit 833ad99
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/Polly.Core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ At the heart of Polly V8 is the [ResilienceStrategy](ResilienceStrategy.cs) clas
public abstract class ResilienceStrategy
{
// the main method that all the others call
protected virtual ValueTask<TResult> ExecuteCoreAsync<TResult, TState>(Func<ResilienceContext, TState, ValueTask<TResult>> execution, ResilienceContext context, TState state);
protected virtual ValueTask<Outcome<TResult>> ExecuteCoreAsync<TResult, TState>(Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> execution, ResilienceContext context, TState state);

// convenience methods for various types of user-callbacks
public void Execute(Action callback);
Expand Down Expand Up @@ -76,7 +76,7 @@ public void Execute(Action execute)
strategy.ExecuteCoreAsync(static (context, state) =>
{
state();
return new ValueTask<VoidResult>(VoidResult.Instance);
return new ValueTask<Outcome<VoidResult>>(new(VoidResult.Instance));
},
context,
execute).GetAwaiter().GetResult();
Expand Down Expand Up @@ -109,7 +109,7 @@ internal class DelayStrategy : ResilienceStrategy
}

protected override async ValueTask<T> ExecuteCoreAsync<T, TState>(
Func<ResilienceContext, TState, ValueTask<T>> callback,
Func<ResilienceContext, TState, ValueTask<Outcome<T>>> callback,
ResilienceContext context,
TState state)
{
Expand All @@ -126,22 +126,17 @@ This way, the responsibility of how to execute method is lifted from the user an

The life of extensibility author is also simplified as they only maintain one implementation of strategy instead of multiple ones. See the duplications in [`Polly.Retry`](https://github.com/App-vNext/Polly/tree/main/src/Polly/Retry).

## Creation of `ResilienceStrategy`

This API exposes [ResilienceStrategyBuilder](Builder/ResilienceStrategyBuilder.cs) that can be used to create the resilience strategy:
### Generic Resilience Strategy

``` csharp
public class ResilienceStrategyBuilder
{
// omitted properties for simplicity
Polly also exposes the sealed `ResilienceStrategy<T>` strategy that is just a simple wrapper over `ResilienceStrategy`. This strategy is used for scenarios when the consumer handles the single result type.

ResilienceStrategyBuilder AddStrategy(ResilienceStrategy strategy, ResilienceStrategyOptions? options = null);
## Creation of `ResilienceStrategy`

ResilienceStrategyBuilder AddStrategy(Func<ResilienceStrategyBuilderContext, ResilienceStrategy> factory, ResilienceStrategyOptions? options = null);
This API exposes the following builders:

ResilienceStrategy Build();
}
```
- [ResilienceStrategyBuilder](ResilienceStrategyBuilder.cs): Used to create resilience strategies that can execute all types of callbacks. In general, these strategies only handle exceptions.
- [ResilienceStrategyBuilder<T>](ResilienceStrategyBuilder.TResult.cs): Used to create generic resilience strategies that can only execute callbacks that return the same result type.
- [ResilienceStrategyBuilderBase](ResilienceStrategyBuilderBase.cs): The base class for both builders above. You can use it as a target for strategy extensions that work for both builders above.

To create a strategy or pipeline of strategies you chain various extensions for `ResilienceStrategyBuilder` followed by the `Build` call:

Expand All @@ -165,6 +160,16 @@ var resilienceStrategy = new ResilienceStrategyBuilder()

The resilience extensibility is simple. You just expose extensions for `ResilienceStrategyBuilder` that use the `ResilienceStrategyBuilder.AddStrategy` methods.

If you want to create a resilience strategy that works for both generic and non-generic builders you can use `ResilienceStrategyBuilderBase` as a target:

``` csharp
public static TBuilder AddMyStrategy<TBuilder>(this TBuilder builder)
where TBuilder : ResilienceStrategyBuilderBase
{
return builder.AddStrategy(new MyStrategy());
}
```

# Resilience Strategy Delegates

Resilience strategies leverage the following delegate types:
Expand Down

0 comments on commit 833ad99

Please sign in to comment.