Polly icon indicating copy to clipboard operation
Polly copied to clipboard

WaitAndRetry fixed wait overload

Open NetMage opened this issue 3 years ago • 3 comments

It seems like the only way to create a fixed wait retry is to use the sleepDurationProvider lambda instead of just passing in a TimeSpan.

Describe your proposed or preferred solution: An overload that takes a fixed TimeSpan would seem to be clearer and more efficient for these cases.

NetMage avatar Feb 24 '22 19:02 NetMage

While there is no overload available to do this that accepts a single TimeSpan parameter, this can be achieved in the simplest way via the following two overloads:

Policy.Handle<Exception>().WaitAndRetry(new[] { TimeSpan.FromSeconds(1) });
Policy.Handle<Exception>().WaitAndRetryAsync(new[] { TimeSpan.FromSeconds(1) });

WaitAndRetry() currently has 12 overloads and WaitAndRetryAsync() has 18, so at this stage adding more increases complexity while adding syntactic sugar as the lack of such an overload does not preclude equivalent functionality with what's available today.

Such an overload would ultimately call through to either RetryPolicy or AsyncRetryPolicy, which would require the allocation of either a delegate to return the TimeSpan value, or an IEnumerable<TimeSpan> that always returns the same value. This means that while adding a new overload may look more efficient to user code, the end result is the same with the allocation of either an enumerator or delegate, it's just that it's hidden from view inside the internals of Polly.

We can maybe review this use case to look for improvements if/when Polly v8 development resumes, where any large changes to how to configure policies might happen.

While I can see the utility in a new overload to make user's code look simpler/cleaner, at this stage I don't think it's worth adding yet more overloads to the WaitAndRetry*() methods in Polly v7.

martincostello avatar Feb 25 '22 15:02 martincostello

How do you control the number of retries for these overloads? Won’t this just retry once?

NetMage avatar Feb 26 '22 03:02 NetMage

Ah, I misunderstood what you were trying to achieve. In that case, it's these overloads:

Policy.Handle<Exception>().WaitAndRetry(123, _ => TimeSpan.FromSeconds(1));
Policy.Handle<Exception>().WaitAndRetryAsync(123, _ => TimeSpan.FromSeconds(1));

martincostello avatar Feb 26 '22 07:02 martincostello