Skip to content

Commit

Permalink
Add service provider to AddResiliencePolicies
Browse files Browse the repository at this point in the history
  • Loading branch information
themoretheless committed Feb 26, 2024
1 parent 34dea9b commit 8cb4a2c
Showing 1 changed file with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ public static IHttpClientBuilder AddResiliencePolicies(
.AddTimeoutPolicy(settings.TimeoutPerTry);
}

public static IHttpClientBuilder AddResiliencePolicies(
this IHttpClientBuilder clientBuilder,
Func<IServiceProvider, ResiliencePoliciesSettings> configure)
{
return clientBuilder
.AddTimeoutPolicy(sp => configure(sp).OverallTimeout)
.AddRetryPolicy(sp => configure(sp).RetryPolicySettings)
.AddCircuitBreakerPolicy(sp => configure(sp).CircuitBreakerPolicySettings)
.AddTimeoutPolicy(sp => configure(sp).TimeoutPerTry);
}

private static IHttpClientBuilder AddRetryPolicy(
this IHttpClientBuilder clientBuilder,
RetryPolicySettings settings)
Expand All @@ -55,6 +66,17 @@ private static IHttpClientBuilder AddRetryPolicy(
.WaitAndRetryAsync(settings));
}

private static IHttpClientBuilder AddRetryPolicy(
this IHttpClientBuilder clientBuilder,
Func<IServiceProvider, RetryPolicySettings> configure)
{
return clientBuilder
.AddPolicyHandler((sp, _) => HttpPolicyExtensions
.HandleTransientHttpError()
.Or<TimeoutRejectedException>()
.WaitAndRetryAsync(configure(sp)));
}

private static IHttpClientBuilder AddCircuitBreakerPolicy(
this IHttpClientBuilder clientBuilder,
CircuitBreakerPolicySettings settings)
Expand All @@ -73,6 +95,24 @@ private static IHttpClientBuilder AddCircuitBreakerPolicy(
});
}

private static IHttpClientBuilder AddCircuitBreakerPolicy(
this IHttpClientBuilder clientBuilder,
Func<IServiceProvider, CircuitBreakerPolicySettings> configure)
{
// This implementation takes into consideration situations
// when you use the only HttpClient against different hosts.
// In this case we want to have separate CircuitBreaker metrics for each host.
// It allows us avoid situations when all requests to all hosts
// will be stopped by CircuitBreaker due to single host is not available.
var registry = new PolicyRegistry();
return clientBuilder.AddPolicyHandler((sp, message) =>
{
var policyKey = message.RequestUri.Host;
var policy = registry.GetOrAdd(policyKey, BuildCircuitBreakerPolicy(configure(sp)));
return policy;
});
}

private static IAsyncPolicy<HttpResponseMessage> BuildCircuitBreakerPolicy(
CircuitBreakerPolicySettings settings)
{
Expand All @@ -89,5 +129,12 @@ private static IHttpClientBuilder AddTimeoutPolicy(
{
return httpClientBuilder.AddPolicyHandler(Policy.TimeoutAsync<HttpResponseMessage>(timeout));
}

private static IHttpClientBuilder AddTimeoutPolicy(
this IHttpClientBuilder httpClientBuilder,
Func<IServiceProvider, TimeSpan> configure)
{
return httpClientBuilder.AddPolicyHandler((sp, _) => Policy.TimeoutAsync<HttpResponseMessage>(configure(sp)));
}
}
}
}

0 comments on commit 8cb4a2c

Please sign in to comment.