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.

Cancel in Retry Action

See original GitHub issue

Is there a way to cancel additional retries within the retry action (synchronous)?

For example:

var policy = Policy
    .Handle<OfflineException>()
    .Retry(3, (ex, retryAttempt, ctx) =>
    {
        var result = MessageBox.Show("Please retry once the application is back online or cancel this request", "Application Offline", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
        if (result != DialogResult.Retry)
        {
            //ToDo: cancel further attempts.  ctx.AbortRetries(); ??
        }
    });

When executing a cancellation token can be passed but one would not necessarily have access to the token source during the retry action.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
reisenbergercommented, Feb 17, 2020

Thanks @rodion-m for the comment.

A better solution here (which I missed pointing out originally) is that onRetry is not really the place to decide whether an action should be retried. Polly already offers a well-defined, strongly-expressive API for whether exceptions should be retried: the .Handle<>() clauses.

So @Discofunk 's original problem could also be solved by:

var policy = Policy
    .Handle<OfflineException>(_ => 
     {
        var result = MessageBox.Show("Please retry once the application is back online or cancel this request", "Application Offline", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
        return (result == DialogResult.Retry);
    });
).Retry(3);

Thanks @rodion-m for the suggestion that sleepDurationProvider could return null to indicate “don’t retry”. Broadly, I am not in favour of having two places in an API to accomplish the same thing, as this tends to dilute the focus of the API overall (users will ask: “What is the difference?”). So, because Polly already has the strong API of the .Handle<>() clauses to indicate whether to retry exceptions, we will probably not modify sleepDurationProvider to also indicate this.

0reactions
rodion-mcommented, Jan 31, 2020

Hello guys! @reisenberger, sorry, but your solution looks like a hack. Please consider a solution with a nullable TimeSpan in the sleepDurationProvider and if it’s null, it means just Cancel (without any unnecessary exceptions).

Read more comments on GitHub >

github_iconTop Results From Across the Web

spring - How can I cancel a retry?
Using Spring Retry, I have a retry going on in 1 thread. In some circumstances, I would like to be able to cancel...
Read more >
Cancelling a Polly Retry policy - Chris Roberts
If we run the application now, start the policy, then click “Stop Policy on Next Retry” we'll notice that our label briefly changes...
Read more >
Cancel, Retry and Timeouts: Keep Your Sanity ... - YouTube
Have you ever had to orchestrate multiple tasks (some of which are asynchronous) and compose them together? As soon as latency comes into ......
Read more >
Retry pattern - Azure Architecture Center
Cancel. If the fault indicates that the failure isn't transient or is unlikely to be successful if repeated, the application should cancel the...
Read more >
Retry, Timeout and Cancel with fetch() : r/Frontend
Any network hiccup would likely cause requests to fail no matter what since the retry logic is too eager - fetch is called...
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