I am implementing Circuit Breaker pattern with HttpClientFactory and Polly using this tutorial and here's the code below that i wrote based on my understanding of the tutorial. RetryHttpRequest
class is the class where HttpClient is used.
services.AddHttpClient<IRetryHttpRequest, RetryHttpRequest>()
.SetHandlerLifetime(TimeSpan.FromSeconds(3))
.AddPolicyHandler(GetCircuitBreakerPolicy());
static IAsyncPolicy<HttpResponseMessage> GetCircuitBreakerPolicy()
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.CircuitBreakerAsync(5, TimeSpan.FromSeconds(30));
}
For
SetHandlerLifetime
, does the lifetime here mean per API request? So if I do retry for 3 times, each retry should take no more than 3 seconds. It is interesting that the default lifetime is 2 minutes which I think is too long.How does
SetHandlerLifetime(TimeSpan.FromSeconds(3))
andCircuitBreakerAsync(5, TimeSpan.FromSeconds(30))
related to each other and work with each other?