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.

How to retry logic for up to N minutes?

See original GitHub issue

Hi,

I would like to write a policy to retry my logic until it succeeds or until a “retry timeout” happen (not a “logic timeout”). With Policy.Handle<Exception>().Retry(3) I can tell Polly to retry up to 3 times. What I would like is to Policy.Handle<Exception>().Retry(TimeSpan.FromMinutes(5)).

Is there a clean way of doing this using Polly API? Thanks!

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
reisenbergercommented, Jun 18, 2017

Hi @darxis !

  • TimeoutPolicy provides the ability to time out any execution.
  • PolicyWrap provides the ability to compose multiple policies into a single policy for later use.

For your Use Case, wrap a timeout policy around any retry policy you like. For example, the following should retry with 10 second delays between tries, up to a 5-minute limit:

Policy timeoutAfterFiveMinutes = Policy.Timeout(TimeSpan.FromMinutes(5));
Policy retryEveryTenSeconds = Policy.WaitAndRetryForever(iteration => TimeSpan.FromSeconds(10));
Policy tryEvery10SecondsUpTo5Minutes = timeoutAfterFiveMinutes.Wrap(retryEveryTenSeconds);
// You could create the above all in one statement.  I used long-named interim variables just to make the components clearer.

// Usage
tryEvery10SecondsUpTo5Minutes.Execute(...);

(EDIT: For a concise syntax, you can of course create that compound policy with an extension method.)

Comments to this stackoverflow question include various working dotnetfiddle examples and discussion of further nuances. They are async examples, and for much shorter timescales (because dotnetfiddle limits overall execution time), but the principles are the same.

Let us know if this gives you what you need!

1reaction
TylerNielsencommented, Apr 9, 2019

I had a follow up question to this thread. If I’m reading the documentation on Timeout Policy correctly, this may be slightly different than what some users may expect based on OP ask.

Based on the Timeout Policy documentation, it seems that Timeout Policy will cause a synchronous task to be put on a separate thread and abandoned if the timeout is hit. I suspect OP might have been asking for a Policy that would simply not try again if timeout was reached, but not necessarily “walk away” from the executing process if it’s still going. If OP wasn’t interested in this behavior, I am 😃.

Would there be anyway to let the execution finish but simply not start another retry if the timeout had expired?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Cleanest way to write retry logic?
30 Answers​​ You can now use this utility method to perform retry logic: Retry.Do(() => SomeFunctionThatCanFail(), TimeSpan. FromSeconds(1));
Read more >
How do I write a retry logic in script to keep retrying to run it ...
I want to write logic in shell script which will retry it to run again after 15 sec upto 5 times based on...
Read more >
Learn about the Retry Pattern in 5 minutes
When using a third-party service or external tool, make sure that you're not layering retry logic. Layering retries at different levels or ...
Read more >
Proper Retry in JavaScript - Solutional
In this post I'm going to show how to handle these situations in JavaScript. Jarmo Pertman — 2020-11-19 (7 minute read).
Read more >
Configure your Retry Policies in Logic Apps
One of the basic mechanisms of Enterprise Integration is retry policies. Check out this blog to configure your retry policies in Logic Apps....
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