How to slow down my api call to a external service using polly
How to reduce or slow down my api to call external system. I want to call contentful cms but they have limited 10 calls per second. Now when there is a large number of changes. I believe it exceeded the limit and start to throw 429 rate limiting error. So I want to slow down my request to contentful so that only 10 calls are send to it per second. How to achieve it in .net polly.
4 Replies
https://github.com/App-vNext/Polly/blob/main/docs/strategies/rate-limiter.md/
you can set this up and configure the window, the permit limit and the queue size
I came across this code and tried to implement but I believe only 10 calls are processed, Even if 50 calls made. Is that the expected behaviour.
var rateLimitPolicy = Policy.RateLimitAsync<HttpResponseMessage>(10, TimeSpan.FromSeconds(1));
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).AddPolicyHandler(rateLimitPolicy);
No idea, but you are also not using what was suggested in the documentation I linked you
specifically, I dont see anything about a queue size in your code, and the default queue is 0