I'm new to Polly, but want to implement it as it seems like a good option for handling exponential backoff if an HTTP request fails.
What I'd like to occur is that it tries using the original URL and if that request fails, it tries again but with the URL manipulated so that it's routed through a proxy service.
So, for example, the original request would have:
var requestUrl = "https://requestedurl.com";
If that fails, the request will be retried in a different format, such as:
requestUrl = $"https://proxy.com?url={requestUrl}";
Ideally, I would be able to provide a list of proxy URLs that it would retry. So, if the above example failed, it would move onto the next:
requestUrl = $"https://proxy2.com?url={requestUrl}";
I'm not sure if this should be in the RetryPolicy or as a FallbackPolicy.
For completeness, this is what I have thus far:
In a helper class, I have two methods:
public static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
{
var delay = Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay: TimeSpan.FromSeconds(1), retryCount: 5);
return Policy
.Handle<HttpRequestException>()
.Or<TimeoutRejectedException>()
.OrTransientHttpError()
.Or<BrokenCircuitException>()
.Or<OperationCanceledException>()
.OrInner<OperationCanceledException>()
.WaitAndRetryAsync(delay);
}
public static IAsyncPolicy<HttpResponseMessage> GetTimeoutPolicy()
{
return Policy.TimeoutAsync<HttpResponseMessage>(10);
}
In my Startup.cs, I have the following (this is for an Azure Function by the way):
builder.Services.AddHttpClient<IRssQueueRequestService, RssQueueRequestService>()
.AddPolicyHandler(HttpClientHelpers.GetRetryPolicy())
.AddPolicyHandler(HttpClientHelpers.GetTimeoutPolicy())
.SetHandlerLifetime(TimeSpan.FromMinutes(10));
As mentioned, I haven't used Polly before. Typically I'd just have the first line to insatiate the HTTP client service, so if there's anything I'm doing wrong, I'd appreciate the feedback.
The Question is... Going back the beginning, how to retry a request but with a different URL?