App-vNext / Polly

Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. From version 6.0.1, Polly targets .NET Standard 1.1 and 2.0+.

Home Page:https://www.thepollyproject.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question]:

Dionisos94 opened this issue · comments

What are you wanting to achieve?

I am calling a service that often replies with HTTP code 429: Too Many Request
I want to be able to retry calls to this service if I ever get a 429, up to a maximum of 3 retries with an increasing wait time between each call.

If the last retry still send 429, I want to get the result of the call (even if the call is still 429).
I do not want exceptions nor make up a default return value.

My base code before any modification looked like this:

public async Task<HttpResponseMessage> GetMyObject()
{
return Http429RetryPolicy().ExecuteAsync(() => _externalService.Call());
}

private AsyncRetryPolicy<HttpResponseMessage> Http429RetryPolicy()
{
return Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode == (HttpStatusCode) 429)
.WaitAndRetryAsync(3, attempt => TimeSpan.FromMilliseconds(1000 * Math.Pow(2, attempt-1)));
}

What code or approach do you have so far?

I tried first to use .ExecuteAndCaptureAsync() instead of .ExecuteAsync()
Issue I faced is that the .Result value returned is null if last retry fails.

I also tried to wrap a FallBackPolicy. But from what I've tried, I have no way to take the last retry value to do a pass-through on the Fallback.

i.e.


var fallback = Policy.HandleResult<HttpResponseMessage>(r => r.StatusCode == (HttpStatusCode) 429)
.FallbackAsync((action) => ... I do not find a way to just pass the last retry answer)

Additional context

No response

I am dumb, I simply forgot to put a last .SetupSequence in my unit test...
It works if I put a 4th mocked reply