0

I've defined a Polly policy to perform retry when an exception occurs. The policy is defined as

policy = Policy.Handle<ReconnectException>().Retry(retryCount);

and I call my method with

policy.Execute(()=>SendMessageWithRetryPolicy(message));

How do I perform a reset so that if my MaxRetryCount is set to 5 and the message has successfully beign dispacted at the 3 step I have again 5 step to perform?

Thanks in advance

advapi
  • 3,661
  • 4
  • 38
  • 73

1 Answers1

2

You do not need to reset the retry count between invocations through a Polly retry policy. Each separate execution through the policy is entitled to the full retry count configured on the policy.

If you configure:

policy = Policy.Handle<ReconnectException>().Retry(retryCount); // where retryCount == 5

then each execution through the policy:

policy.Execute(()=>SendMessageWithRetryPolicy(message));

will be entitled to 6 tries (1 initial try + 5 retries). If a first execution uses 3 retries, the next execution is still entitled to 6 tries (1 initial try + 5 retries).

mountain traveller
  • 7,591
  • 33
  • 38
  • Thanks for your reply, in my case polly is responsable of starting a always on, endless looping action that performs listening on an IBM mq queue, so I need to reset the counter or to look @ a different approach – advapi Sep 06 '19 at 03:34
  • A single execution through a policy is not designed to govern (from the outside) long-running multiple iterations of an endless loop. If the scenario is the timer-triggered lambda in [this question](https://stackoverflow.com/questions/57805044/), the code posted in the question does not work as a policy to govern each iteration triggered by the timer. The appropriate fix would be to move the policy closer to (use it more tightly around) the code throwing the exception, within each iteration. – mountain traveller Sep 06 '19 at 18:24
  • @advapi If you need a Polly retry policy which never runs out of tries, you can use [RetryForever](https://github.com/App-vNext/Polly#retry-forever-until-succeeds) or WaitAndRetryForever. – mountain traveller Sep 08 '19 at 09:12