Hi,
How can we use HttpUtils
with Polly to implement retries/backoff? We’re having issues calling an API on Azure giving 429 Too many requests
.
We’re thinking of the methods such as GetJsonFromUrl
. It is HttpClient under the hood, so it should be doable, right?
mythz
January 19, 2023, 4:20pm
2
HTTP Utils uses HttpClient from .NET 6+. I’ve not used Polly to know how to best integrate it with HTTP Utils, but if needed you can change the HttpClientHandler
or HttpClient
used by overriding these factory functions:
HttpUtils.HttpClientHandlerFactory = () => new HttpClientHandler {
UseDefaultCredentials = true,
AutomaticDecompression = DecompressionMethods.Brotli
| DecompressionMethods.Deflate | DecompressionMethods.GZip,
};
HttpUtils.CreateClient = () => {
var httpClient = new HttpClient();
return httpClient;
};
1 Like
This is how I ended up doing it, after I found out that Polly actually catches exceptions, and therefore is compatible with HttpUtils:
Define the retry policy, in this example we only catch 429, then wait for a relatively long time (a bit randomized) before retrying.
var delay = Backoff
.DecorrelatedJitterBackoffV2(
medianFirstRetryDelay: TimeSpan.FromSeconds(10),
retryCount
);
policyS = Policy
.Handle<HttpRequestException>(ex => ex.Message.Contains("429"))
.WaitAndRetry(
delay,
(result, timespan, retryNo, context) => {
Console.WriteLine($"{context.OperationKey}: Retry number {retryNo} within " +
$"{timespan.TotalMilliseconds}ms. Original status code: 429");
}
);
The actual API call is here:
var response = policyS.Execute(ctx => {
var response2 = lookupUrl.GetJsonFromUrl(
requestFilter: x => {
x.AddHeader("x-authorization", $"Bearer {ccToken}");
});
return response2.FromJson<LookupDTO>();
}, pollyContext);
2 Likes