Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use a specific timeout connected to a retrypolicy

Tags:

c#

polly

I'm creating a retry policy the following way:

var policy = Policy.Handle<Exception>().WaitAndRetryAsync...

How to chail/build a timeout for the retrypolicy above? Policy.TimeoutAsync returns a TimeoutPolicy, hence I'm not able to do something like

var policy = Policy.TimeoutAsync(30).Handle<Exception>().WaitAndRetryAsync....

Does the timeout become a common setting for all my retry policies?

like image 654
Johan Avatar asked Apr 04 '17 08:04

Johan


People also ask

How do I use waitandretryasync with timeout?

Once the conditions are setup, we can apply the policy WaitAndRetryAsync where we retry for five times and wait in an exponential manner between each retry. Timeout is easier as we only need to wait to a certain timespan: And we are done implementing the two policies:

How to set timeout for httpclient retries?

The default for HttpClient is 100 seconds, so if your retries and waits exceed that then Polly will throw the TimeoutException. Set HttpClient.Timeout to the max length of time you'd expect it to take for all your retries.

When to apply the retry mechanism?

When to apply the Retry mechanism In most scenarios at least, important read operations can be marked to engage the server with Retry operation for specific errors like network timeout, connection unreachable, etc.

What is retrypolicy in C++?

Code Explanation: The RetryPolicy is a class from the TransientFaultHandling namespace. The above snippet is an interesting one. Here we’ve used the Generic constructor of RetryPolicy. The class Incremental is RetryStrategy which takes similar arguments as defined in the RetryPolicy class constructor.


Video Answer


1 Answers

To combine policies, you build each policy separately, then combine them using PolicyWrap.

To build an overall timeout which applies across all retries as a whole (ie across the entire operation):

var overallTimeoutPolicy = Policy.TimeoutAsync(60); 
var waitAndRetryPolicy = Policy
    .Handle<WhateverException>()
    .WaitAndRetryAsync(/* your wait-and-retry specification*/);
var combinedPolicy = overallTimeoutPolicy.WrapAsync(waitAndRetryPolicy);

await combinedPolicy.ExecuteAsync(cancellationToken => ...)

To impose a timeout on each specific try, wrap the retry and timeout policies in the other order:

var timeoutPerTry = Policy.TimeoutAsync(10); 
var waitAndRetryPolicy = Policy
    .Handle<WhateverException>()
    .WaitAndRetryAsync(/* your wait-and-retry specification*/);
var combinedPolicy = waitAndRetryPolicy.WrapAsync(timeoutPerTry);

await combinedPolicy.ExecuteAsync(cancellationToken => ...);

Or even use both kinds of timeout (per-try, per-overall-operation):

var overallTimeout = Policy.TimeoutAsync(60); 
var timeoutPerTry = Policy.TimeoutAsync(10); 
var waitAndRetryPolicy = Policy
    .Handle<WhateverException>()
    .WaitAndRetryAsync(/* your wait-and-retry specification*/);

var combinedPolicy = Policy
    .WrapAsync(overallTimeout, waitAndRetryPolicy, timeoutPerTry); // demonstrates alternative PolicyWrap syntax

await combinedPolicy.ExecuteAsync(cancellationToken => ...);

The PolicyWrap wiki gives full syntax details, and advice on the effects of different orderings, when combining policies within a wrap.


To answer:

Does the timeout become a common setting for all my retry policies?

Policies apply wherever you use them (whether used individually, or as part of a PolicyWrap).

You can thread-safely use any TimeoutPolicy instance you have configured at multiple call sites. So, to apply that timeout as a common setting for all your retry policies, simply include that same TimeoutPolicy instance in the PolicyWrap for each call site. The single TimeoutPolicy instance can safely be wrapped with different retry policy instances, if desired.

If both your wait-and-retry specification, and timeout specification, are common for all call sites, simply make one PolicyWrap instance encompassing both (per above code examples), and re-use that PolicyWrap instance everywhere. Again - thread safe.

like image 100
mountain traveller Avatar answered Oct 04 '22 19:10

mountain traveller