-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Is it possible to handle different exceptions differently with the same policy? #104
Comments
Hi @BertLamb // don't need to reconnect on a CommandException
// but need to reconnect before a retry when an IOException occurs
myPolicy = Policy
.Handle<CommandException>()
.Or<IOException>()
.Retry(10, (exception, retryCount, context) =>
{
if (exception is IOException) { Reconnect(); }
});
myPolicy.Execute(Command); Does this cover it? Let us know if you have any other questions! |
Hi @BertLamb Did this solve your problem? Can we close the issue? Or: Would you like any further assistance? |
Thanks! Yes and no, what if I wanted to have a CircuitBreaker for IOExceptions but just a Retry on CommandExceptions? |
Hi @BertLamb . To do that with Polly, you can define separate policies and nest them, as described in the wiki here or as shown below: var retryPolicy = Policy
.Handle<CommandException>()
.Retry(10, (exception, retryCount, context) => Reconnect());
var circuitBreaker = Policy
.Handle<IOException>
.CircuitBreaker(2, TimeSpan.FromMinutes(1));
retryPolicy.Execute(() => circuitBreaker.Execute(command)); There isn't currently a way to define a Policy that handles a variety of different exceptions in a variety of different ways, all in one single fluent statement. However, the Polly Roadmap envisages the Polly Pipeline, which would allow any number of functionally-composed policies to be reduced to one Policy, thus: var combinedPolicy = Policy.Pipeline(retryPolicy, circuitBreaker);
combinedPolicy.Execute(command); or (an alternative syntax under consideration):
I guess once the functionality for collapsing functionally-composed (wrapped) policies into one (as in the Polly Pipeline) was in place, it might be possible to create an on-going fluent syntax as follows - is this the kind of thing you had in mind? var myPolicy = Policy
.Handle<CommandException>()
.Retry(10, (exception, retryCount, context) => Reconnect());
.Handle<IOException>
.CircuitBreaker(2, TimeSpan.FromMinutes(1));
myPolicy.Execute(command); |
Ah, nice, I like that |
@reisenberger Any progress on this? Such a pipeline functionality would be sweet. |
@johnknoop Yes, this was delivered at Polly v5.0.0 and its eventual name was PolicyWrap. (We moved away from the |
@reisenberger Oh, great! I'll have a look at that. Thanks! |
Hi, is this solved by I would like to get the same behaviour as:
so if the error is exactly "error", it will do exponential backoff; if the error is "error, something unexpected happened" it will do a regular retry. if I try and use
am I not using it right or Wrap is not suitable for this scenario? |
Hi @StefDotmailer . This:
suggests the intention is two mutually exclusive cases. To get that effect, define the policy predicates to be mutually exclusive. Then, only one or the other policy (not both) will handle any return result:
To explain why your posted code generated 9 retries: both the predicates |
Hi @reisenberger, thank you for the explanation. would be nice if there was also a |
I just started digging into Polly and I really like it. One thing I can't quite seem to figure out how to do is to have a
Policy
that reacts differently to different exception types.I'd like to do something like this:
Is this possible?
The text was updated successfully, but these errors were encountered: