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,
HttpClient
to make more than 3 concurrent HTTP calls and I set the queue size to "infinite"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));
In case of Polly there are many ways to combine/chain policies. Here I have demonstrated the most common ones. I would highly encourage you to use Policy.WrapAsync
static method because:
var resilienceStrategy = Policy.WrapAsync(sharedBulkhead, localRetry, localTimeout);
I've used the prefix shared
in case of Bulkhead policy because it needs to be shared between HttpClient
calls otherwise it is useless. In case of Polly most of the policies are stateless but there are a few stateful (like Circuit Breaker or Bulkhead).
I would also suggest to read this other post of mine which details how to enforce throttling in case of HttpClient
. Long story short: Bulkhead is mainly designed for CPU based computations, not for I/O bound operations. RateLimit policy might be a better fit for this.