question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

FEATURE: Fallback policy

See original GitHub issue

As I understand it today, if I use Polly to setup a circuit breaker in my policy and that the circuit does break, I will receive a BrokenCircuitException. As a user of the framework, I need to handle this exception and write some code to specify how I want to react to this event. I would prefer to give to Polly that code and let it execute it if the circuit is broken. It would be better performance-wise, and much cleaner.

Policy
  .Handle<DivideByZeroException>()
  .CircuitBreaker(2, TimeSpan.FromMinutes(1))
  .WhenBroken(
  () => throw new BrokenCircuitException(); //Or another fallback method specific to my app.
)

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
reisenbergercommented, Jul 12, 2016

@mfjerome @SeanFarrow Great question from both of you, that I’ve kind-of answered here . We could expose the Exception1 in your example in several places:

[1] in the captured PolicyResult somehow (but complicated) [2] as part of an event stream (later) [3] in a simple onFallback delegate (perfect for logging) [4] to the fallbackFunc as well

In detail:

[1] PolicyResult<TResult> could contain an IEnumerable<Exception> property as @SeanFarrow says, that details other exceptions before the fallback (or anything else if part of a bigger wrap) was invoked. A few issues I see with this as stated here: there may be duplicates, it’s hard (in a broader wrap context) to connect which exception occurred with which policy-in-the-wrap, and it’s actually quite hard to implement in the wrap unless I’m missing something (you kind-of want both-at-the-same-time to capture the exception into some PolicyResult you pass back to capture in the enumerable, but also throw the exception to outer layers at the same time so they can handle it … you can’t capture and throw at the same time??)

[2] Eventually we can capture all manner of policy behaviour as an event stream as suggested here (but not soon).

[3] We offer an additional overload taking a simple onFallback delegate, so:

FallbackPolicy<TResult> fallbackPolicy = Policy
  .Handle<Whatever>(...)
  .Fallback<TResult>(Func<TResult> fallbackFunc, Action<DelegateResult<TResult>> onFallback); // for async variants, Func<DelegateResult<TResult>, Task> onFallbackAsync

This is my preferred option: simple, available now, and parallels the onRetry and onBreak delegates already well known to users on the retry and circuit-breaker policies. The classic solution in Polly for logging faults as the policy operated. Shall we go with this?

[4] A final option is to combine [3] with what @mfjerome suggested, ie offer the DelegateResult to the fallbackFunc too, ie offer additional overloads:

FallbackPolicy<TResult> fallbackPolicy = Policy
  .Handle<Whatever>(...)
  .Fallback<TResult>(Func<DelegateResult<TResult>, TResult> fallbackFunc);
// and
FallbackPolicy<TResult> fallbackPolicy = Policy
  .Handle<Whatever>(...)
  .Fallback<TResult>(Func<DelegateResult<TResult>, TResult> fallbackFunc, Action<DelegateResult<TResult>> onFallback); // for async variants, Func<DelegateResult<TResult>, Task> onFallbackAsync

I’m not sure at the moment if this adds value (maybe somebody really wants to include details of the exception in the fallback value…), or just confusion (too many overloads). Either way, I propose we keep [3]. Do you think [4] adds anything?

0reactions
mfjeromecommented, Jul 13, 2016

Agreed the idea will be uniform across policies such as onRetry, onBreak, etc

Read more comments on GitHub >

github_iconTop Results From Across the Web

AuthorizationOptions.FallbackPolicy Property
Gets or sets the fallback authorization policy used by CombineAsync(IAuthorizationPolicyProvider, IEnumerable<IAuthorizeData>) when no IAuthorizeData have ...
Read more >
Globally Require Authenticated Users By Default Using ...
A Fallback Policy means that if no other policy or attribute is specified on a Controller or Razor Page, the Authorization middleware will...
Read more >
Setting global authorization policies using the ...
The FallbackPolicy is applied when no authorization requirements are specified, including the [Authorize] attribute or equivalent. By default, ...
Read more >
Building Polly Fallbacks for Resilient .NET Service-to- ...
The Fallback policy allows you to perform any action when a request fails, this could be restarting a service, messaging an admin, scaling...
Read more >
Why is my authorization fallback policy overriding ...
Microsoft docs say that the fallback policy is for pages without any authorize or AllowAnonymous attribute.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found