2

I'm replacing my currenty retry policies with the Polly framework. I used to define my retry strategy like this:

var strategy = new ExponentialBackoff(RetryStrategy.DefaultClientRetryCount, RetryStrategy.DefaultMinBackoff, RetryStrategy.DefaultMaxBackoff, RetryStrategy.DefaultClientBackoff);

where the DefaultClientRetryCount is the number of retries and then the exponentialBackoff calculates the sleep time between the retries based on the variables: DefaultMinBackoff is the min timespan between retries; DefaultMaxBackoff is the maximum timespan; and DefaultClientBackoff is a default value used to calculate a random delta between retries.

On Polly I define my retryPolicy like this:

var retryPolicy = Policy.Handle<Exception>(ex => _transientErrorDetectionStrategy.IsTransient(ex)).WaitAndRetryAsync(int retryCount, Func<int,timespan> sleepDuration);

Where the retryCount is the number of retries and the sleepDuration is the time between retries.

How can I replicate the Timespan between retries generated on the previous policy to send as parameter to the WaitAndRetryAsync method on Polly?

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
pedrodotnet
  • 788
  • 3
  • 16
  • 34
  • Polly documents various ways of implementing exponential backoff [here](https://github.com/App-vNext/Polly/wiki/Retry#exponential-backoff). For some randomisation, Polly documentation gives examples with jitter [here](https://github.com/App-vNext/Polly/wiki/Retry-with-jitter). – mountain traveller Nov 01 '18 at 21:14
  • To answer the question "How to replicate the Timespan between retries generated by the previous policy?", we need to know exactly how the parameter `DefaultClientBackoff` was used to calculate a random delta between retries. – mountain traveller Nov 01 '18 at 21:15

1 Answers1

2

Check out Polly.Contrib.WaitAndRetry library... DecorrelatedJitterBackoffV2 specifically, which can be described as follows (per their documentation):

Generates sleep durations in an exponentially backing-off, jittered manner, making sure to mitigate any correlations. For example: 850ms, 1455ms, 3060ms. Per discussion in Polly issue 530, the jitter of this implementation exhibits fewer spikes and a smoother distribution than the AWS jitter formula.

This helper will generate the delay that your looking for with an added benefit of having all the kinks and bugs worked out beforehand thanks to the awesome Polly community! Once you have the delay, you can turn around and define your policy with it:

var maxDelay = TimeSpan.FromSeconds(45);
var delay = Backoff.DecorrelatedJitterBackoffV2(
    medianFirstRetryDelay: TimeSpan.FromSeconds(1), retryCount: 50)
        .Select(s => TimeSpan.FromTicks(Math.Min(s.Ticks, maxDelay.Ticks)));

var retryPolicy = Policy
    .Handle<FooException>()
    .WaitAndRetryAsync(delay);

P.S. This helper method has 2 other optional parameters:

seed: An optional System.Random seed to use. If not specified, will use a shared instance with a random seed, per Microsoft recommendation for maximum randomness.

fastFirst: Whether the first retry will be immediate or not.

kiddagger
  • 331
  • 3
  • 9