Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Fallback Resilience Strategy #1158

Merged
merged 4 commits into from
Apr 25, 2023
Merged

Introduce Fallback Resilience Strategy #1158

merged 4 commits into from
Apr 25, 2023

Conversation

martintmk
Copy link
Contributor

@martintmk martintmk commented Apr 25, 2023

The issue or feature being addressed

Closes #1097

Details on the issue fix or feature implementation

This PR adds the fallback resilience strategy to V8.

API usage:

// convenience fallback that handles single result type
new ResilienceStrategyBuilder().AddFallback<int>(
    shouldHandle => shouldHandle
        .HandleResult(-1)
        .HandleException<InvalidOperationException>(),
    (outcome, context) => new ValueTask<int>(0));

// fallback that handles single result type
new ResilienceStrategyBuilder().AddFallback(new FallbackStrategyOptions<int>
{
    ShouldHandle = new OutcomePredicate<HandleFallbackArguments, int>()
        .HandleResult(-1)
        .HandleException<InvalidOperationException>(),
    FallbackAction = (outcome, context) => new ValueTask<int>(0),
    OnFallback = new OutcomeEvent<OnFallbackArguments, int>().Register(() => Console.WriteLine("Fallback"))
});

// the main method allowing configuration of fallbacks for multiple result types
new ResilienceStrategyBuilder().AddFallback(new FallbackStrategyOptions
{
    Handler = new FallbackHandler()
        .SetFallback<int>(handler =>
        {
            handler
                .ShouldHandle
                .HandleResult(-1)
                .HandleException<InvalidOperationException>();

            handler.FallbackAction = (outcome, context) => new ValueTask<int>(0);
        })
        .SetFallback<double>(handler =>
        {
            handler
                .ShouldHandle
                .HandleResult(-1)
                .HandleException<InvalidOperationException>();

            handler.FallbackAction = (outcome, context) => new ValueTask<double>(0);
        }),
    OnFallback = new OutcomeEvent<OnFallbackArguments>().Register(() => Console.WriteLine("Fallback"))
});

Confirm the following

  • I started this PR by branching from the head of the default branch
  • I have targeted the PR to merge into the default branch
  • I have included unit tests for the issue/feature
  • I have successfully run a local build

@martintmk martintmk self-assigned this Apr 25, 2023
@martintmk martintmk added the v8 Issues related to the new version 8 of the Polly library. label Apr 25, 2023
@martintmk martintmk added this to the v8.0.0 milestone Apr 25, 2023
@codecov-commenter
Copy link

codecov-commenter commented Apr 25, 2023

Codecov Report

Merging #1158 (1dfc954) into main (a1ef1bb) will increase coverage by 0.34%.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##             main    #1158      +/-   ##
==========================================
+ Coverage   81.50%   81.85%   +0.34%     
==========================================
  Files         243      253      +10     
  Lines        5673     5780     +107     
  Branches      952      961       +9     
==========================================
+ Hits         4624     4731     +107     
  Misses        844      844              
  Partials      205      205              
Flag Coverage Δ
linux ?
macos 81.85% <100.00%> (+0.34%) ⬆️
windows 81.85% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/Polly.Core/Fallback/FallbackHandler.Handler.cs 100.00% <100.00%> (ø)
src/Polly.Core/Fallback/FallbackHandler.TResult.cs 100.00% <100.00%> (ø)
src/Polly.Core/Fallback/FallbackHandler.cs 100.00% <100.00%> (ø)
.../Polly.Core/Fallback/FallbackResilienceStrategy.cs 100.00% <100.00%> (ø)
...ack/FallbackResilienceStrategyBuilderExtensions.cs 100.00% <100.00%> (ø)
...y.Core/Fallback/FallbackStrategyOptions.TResult.cs 100.00% <100.00%> (ø)
src/Polly.Core/Fallback/FallbackStrategyOptions.cs 100.00% <100.00%> (ø)
src/Polly.Core/Fallback/HandleFallbackArguments.cs 100.00% <100.00%> (ø)
src/Polly.Core/Fallback/OnFallbackArguments.cs 100.00% <100.00%> (ø)
src/Polly.Core/Fallback/VoidFallbackHandler.cs 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@martintmk martintmk enabled auto-merge (squash) April 25, 2023 15:13
@martintmk martintmk merged commit 6be94b1 into main Apr 25, 2023
@martintmk martintmk deleted the mtomka/fallback branch April 25, 2023 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v8 Issues related to the new version 8 of the Polly library.
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Introduce Fallback Strategy
3 participants