How to retry logic for up to N minutes?
See original GitHub issueHi,
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:
- Created 6 years ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top 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 >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
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:
(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!
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?