FEATURE: Fallback policy
See original GitHub issueAs 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:
- Created 8 years ago
- Comments:12 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 simpleonFallback
delegate (perfect for logging) [4] to thefallbackFunc
as wellIn detail:
[1]
PolicyResult<TResult>
could contain anIEnumerable<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 somePolicyResult
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:This is my preferred option: simple, available now, and parallels the
onRetry
andonBreak
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 thefallbackFunc
too, ie offer additional overloads: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?
Agreed the idea will be uniform across policies such as
onRetry
,onBreak
, etc