Polly to change query string on retry

Viewed 626

I'm using .NET 5 and want to use Polly to change the query string of a request on a retry. Background - I have a fixed quota of requests per minute which is allowed from my IP address. If I exceed the limit, I get a specific 4xx status code. In this case I want to add a query string argument ?key=xxx to handle peaks. The requests counting towards the API key are more expensive and should only apply if reached the quota temporary.

I use the named client many times in different places.

Is this a scenario for which Polly is suitable? Or from a design perspective, is handling this in business logic the clean way? Then I need to wrap this logic to avoid repeating myself.

var response = await client.GetStringAsync("https://test.com");
if (!response.IsSuccessStatusCode && response.StatusCode == 4xx)
  response = await client.GetStringAsync("https://test.com?key=XXX")

// continue with regular workflow - handling errors or process response
1 Answers

The GetStringAsync returns a Task<string> so you can't examine the reponse's StatusCode.
So, you need to use GetAsync which returns a Task<HttpResponseMessage>.

Because the request uri is the only thing which is changing between the calls that's why you need to receive that as a parameter:

private static HttpClient client = new HttpClient(); //or use IHttpClientFactory
static async Task<HttpResponseMessage> PerformRequest(string uri)
{
    Console.WriteLine(uri);
    return await client.GetAsync(uri);
}

In order to have a parameter-less action which can be performed by the retry policy we need an address iterator and a wrapper around the PerformRequest:

static IEnumerable<string> GetAddresses()
{
    yield return "https://test.com";
    yield return "https://test.com?key=XXX";
    ...
}
private static readonly IEnumerator<string> UrlIterator = GetAddresses().GetEnumerator();
static async Task<HttpResponseMessage> GetNewAddressAndPerformRequest()
{
    if (UrlIterator.MoveNext())
        return await PerformRequest(UrlIterator.Current);
    return null;
}

Each time when you call the GetNewAddressAndPerformRequest it retrieves the next fallback url and then executes the request against that.

What's left is the retry policy itself:

var retryPolicyForNotSuccessAnd4xx = Policy
    .HandleResult<HttpResponseMessage>(response => response != null && !response.IsSuccessStatusCode)
    .OrResult(response => response != null && (int)response.StatusCode > 400 && (int)response.StatusCode < 500)
    .WaitAndRetryForeverAsync(_ => TimeSpan.FromSeconds(1));
  • If the GetNewAddressAndPerformRequest returns null because we have run out of fallback urls then we exit from the retry
  • If the statusCode is between 200 and 299 then we exit from the retry
  • If the statusCode is between 300 and 400 or greater than 500 then we exit from the retry
  • In every other case we perform a retry

The usage could look like this:

var response = await retryPolicyForNotSuccessAnd4xx.ExecuteAsync(async () => await GetNewAddressAndPerformRequest());
if (response == null)
{
    Console.WriteLine("All requests failed");
    Environment.Exit(1);
}
    
Console.WriteLine(await response.Content.ReadAsStringAsync());

For the sake of completeness here is the full source code:

class Program
{
    private static HttpClient client = new HttpClient();
    static async Task Main(string[] args)
    {
        var retryPolicyForNotSuccessAnd4xx = Policy
            .HandleResult<HttpResponseMessage>(response => response != null && !response.IsSuccessStatusCode)
            .OrResult(response => response != null && (int)response.StatusCode > 400 && (int)response.StatusCode < 500)
            .WaitAndRetryForeverAsync(_ => TimeSpan.FromSeconds(1));

        var response = await retryPolicyForNotSuccessAnd4xx.ExecuteAsync(async () => await GetNewAddressAndPerformRequest());
        if (response == null)
        {
            Console.WriteLine("All requests failed");
            Environment.Exit(1);
        }

        Console.WriteLine(await response.Content.ReadAsStringAsync());
    }

    static IEnumerable<string> GetAddresses()
    {
        yield return "https://test.com";
        yield return "https://test.com?key=XXX";
    }

    private static readonly IEnumerator<string> UrlIterator = GetAddresses().GetEnumerator();
    
    static async Task<HttpResponseMessage> GetNewAddressAndPerformRequest()
        => UrlIterator.MoveNext() ? await PerformRequest(UrlIterator.Current) : null;
    
    static async Task<HttpResponseMessage> PerformRequest(string uri)
    {
        Console.WriteLine(uri);
        return await client.GetAsync(uri);
    }
}
Related