ObiEff
ObiEff

Reputation: 650

AutoRest custom retry policy with status code transform

I'm working on a system with a few Microservices and an Orchestrator who communicate via AutoRest Generated Clients. I'm running into an issue when a call is made to the Orchestrator, and the Orchestrator makes a dependency call, if the dependency fails it is retried by auto rest 3 times. Now this would normally be fine but because the Orchestrator receives a failed response it will then retry 3 times, which will retry the downstream services 3 times each leading to 9 downstream calls.

I've thought of the following solution, allow downstream services to fail and return a 500 Internal Server Error as normal. Should the retries also fail and final response code we receive be a Transient Response code, we turn this into a 502. I've created a custom ITransientErrorDetectionStrategy which does not retry on 502 therefore when the orchestrator bubbles back the 502 response code, the orchestrator request is not retried.

My ITransientErrorDetectionStrategy looks as follows:

public class DownStreamFailureStrategy : ITransientErrorDetectionStrategy
{
    public bool IsTransient(Exception ex)
    {
        HttpRequestWithStatusException httpException;
        if ((httpException = ex as HttpRequestWithStatusException) != null)
        {
            if (httpException.StatusCode == HttpStatusCode.RequestTimeout||
                (httpException.StatusCode >= HttpStatusCode.InternalServerError &&
                httpException.StatusCode != HttpStatusCode.NotImplemented &&
                httpException.StatusCode != HttpStatusCode.HttpVersionNotSupported &&
                 httpException.StatusCode != HttpStatusCode.BadGateway))
            {
                return true;
            }
        }

        return false;
    }
}

The issue I am having is with transforming the existing 500 response into a 502, I don't want to have to leave it up to the calling code to do this, but rather build it into the Client. I have been looking at building a custom RetryDelegatingHandler, however I am not sure how I would use it in my client factory.

Upvotes: 1

Views: 1211

Answers (1)

Ivan R.
Ivan R.

Reputation: 1915

Also you could change number of retrying attempts through changing RetryPolicy.

public partial class YourAutoGeneratedClient
{
    partial void CustomInitialize()
    {
        var retryPolicy = new RetryPolicy<TransientErrorIgnoreStrategy>(0);
        SetRetryPolicy(retryPolicy);
    }
}

Upvotes: 1

Related Questions