Honouring retries from 429 response
See original GitHub issueI am attempting to capture a 429 response, and the advised retry interval from the HTTPResponse, as outlined in the blog post here
I am using .Net Core, with version 5.1 (current latest) of Polly.
I have started trying to get a simple HTTPResponse captured, but ultimately I want to use Polly to handle retries for the .Net Core DocumentDB client.
The example code in the blog post is as follows:
var honoursRetryAfter = Policy<HttpResponseMessage>
.Handle<HttpException>
.OrResult(r => statusCodesToRetry.Contains(r.StatusCode)) // detail elided
.RetryAsync(retryCount: /* your preferred retry count */,
sleepDurationProvider: (retryAttempt, context) =>
context.ContainsKey("RetryAfter") && context["RetryAfter"] != null
? (TimeSpan)context["RetryAfter"]
: defaultSleepProvider(retryAttempt) // detail elided
);
Unfortunately, this particular item seems to be more pseudocode than a usable example - there is no such named parameter as sleepDurationProvider
for RetryAsync. So, I have tried WaitAndRetry as follows:
var honoursRetryAfterAsync = Policy<HttpResponseMessage>
.Handle<Exception>()
.OrResult(r => r.StatusCode.ToString().Contains("429"))
.WaitAndRetryAsync(
retryCount: 3,
sleepDurationProvider: (retryAttempt, context) => context.ContainsKey("RetryAfter") && context["RetryAfter"] != null
? (TimeSpan)context["RetryAfter"]
: TimeSpan.FromSeconds(0.5),
onRetryAsync: async (message, timespan, retryAttempt, context) => await DoSomething(context["httpClient"]));
However, it is becoming confusing, as there are so many overrides with different signatures, and their availability depends on the complete signature of the complete Policy, and some of the examples in the documentation do not compile. e.g:
Policy
.HandleResult<HttpResponseMessage>(r => r.StatusCode == 500) // Error - cannot use equality between r.StatusCode and int
.OrResult<HttpResponseMessage>(r => r.StatusCode == 502) // Error - should not specify <HttpResponseMessage> again
So, I am feeling as If I am just hacking things together in an inappropriate way 😉
I would really appreciate it if someone can point me in the right direction to get this feature working, as intended.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:32 (17 by maintainers)
Top GitHub Comments
Hi,
Thanks @reisenberger.
For next one who are looking for a sample code:
Let me know if you see something to improve 😃
Thanks @reisenberger for clarity! I had missed that point. Just wanna also say thanks for the quality of your responses throughout this thread, helped me learn better how Polly works.
What I learned from studying this thread, and the call out to Microsoft Support, was that 30 secs of retry is fine for our use case. Any more than that and we need to just kick down and pay for more RU’s 😃