I am trying to make some HTTP calls and I want to have retry, timeout, and bulkhead for maximum concurrent connections. This is my attempt. I don't know if the order of the WrapAsync
function calls is correct. More specifically,
- I may sometimes get 4xx or 5xx response from the server and I want Polly to handle those
- I don't want a
HttpClient
to make more than 3 concurrent HTTP calls and I set the queue size to "infinite" - If the server did not respond in 20 seconds then timeout that attempt
- For retries use an exponential timeout policy by exponentially increasing the timeout
My question is does this code do what I think it will do?
var betterPolicy = HttpPolicyExtensions.HandleTransientHttpError().OrTransientHttpStatusCode()
.WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
.WrapAsync(Policy.BulkheadAsync(3, int.MaxValue /* queue size */))
.WrapAsync(Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(20)));
var httpClient = new HttpClient(new PolicyHttpMessageHandler(betterPolicy));