C
C#5mo ago
Raki

Can anyone explain Bulkhead policy in polly

Can anyone explain Bulkhead policy in polly I want to send API calls to an external API that only allows 10 calls per second. Therefore, I need my program to be limited to making only 10 HTTP calls per second. Additionally, if I encounter a 429 error (too many requests), I want to retry that call. I learned about the Bulkhead pattern from this article: https://medium.com/@ynskrn54/using-polly-and-the-bulkhead-pattern-in-net-f4a9639e2fcd, which helps in preventing overwhelming the external API. I have written some code based on this pattern. My understanding is that this code will manage 10 calls at a time in a queue. For instance, if I have 30 HTTP calls to make, they will be queued and processed at a rate of 10 calls per second. Is the below code serve my purpose
C#
/Setting the policy
var bulkHeadPolicy = Policy.BulkheadAsync < HttpResponseMessage > (10, 1);

//Defining it in Startup.cs
services
.AddHttpClient < IContentfulManagementService, ContentfulManagementService > ((s, c) => {
c.BaseAddress = new Uri(managementApiURL);
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", managementApiKey);
c.Timeout = timeout;
})
.AddTransientHttpErrorPolicy(ErrorPolicy) .AddPolicyHandler(timeoutPolicy).AddTransientHttpErrorPolicy(retryErrorPolicy).AddPolicyHandler(bulkHeadPolicy);

//Retry when it errors out
private static Func < PolicyBuilder < HttpResponseMessage > , IAsyncPolicy < HttpResponseMessage >> retryErrorPolicy =>
p => p.Or < BulkheadRejectedException > ().WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
C#
/Setting the policy
var bulkHeadPolicy = Policy.BulkheadAsync < HttpResponseMessage > (10, 1);

//Defining it in Startup.cs
services
.AddHttpClient < IContentfulManagementService, ContentfulManagementService > ((s, c) => {
c.BaseAddress = new Uri(managementApiURL);
c.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", managementApiKey);
c.Timeout = timeout;
})
.AddTransientHttpErrorPolicy(ErrorPolicy) .AddPolicyHandler(timeoutPolicy).AddTransientHttpErrorPolicy(retryErrorPolicy).AddPolicyHandler(bulkHeadPolicy);

//Retry when it errors out
private static Func < PolicyBuilder < HttpResponseMessage > , IAsyncPolicy < HttpResponseMessage >> retryErrorPolicy =>
p => p.Or < BulkheadRejectedException > ().WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
Medium
2 Replies
Sir Rufo
Sir Rufo5mo ago
c#
var bulkheadPolicy = Policy.BulkheadAsync(5, int.MaxValue);
c#
var bulkheadPolicy = Policy.BulkheadAsync(5, int.MaxValue);
In this example, we allow up to 5 concurrent executions and an unlimited queue. Adjust these values based on your application’s requirements and resource constraints. Your code will allow up to 10 concurrent executions and a limited queue to 1. So making 30 request at once, 10 will be executed, 1 is added to the queue and 19 will go to walhalla 😉
Sir Rufo
Sir Rufo5mo ago
But Polly has such a rate-limiter build in https://github.com/App-vNext/Polly#rate-limiter
GitHub
GitHub - App-vNext/Polly: Polly is a .NET resilience and transient-...
Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and ...