C
C#6mo ago
Raki

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
Raki
Raki6mo ago
c#
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);

private static Func < PolicyBuilder < HttpResponseMessage > , IAsyncPolicy < HttpResponseMessage >> ErrorPolicy =>
p => p.Or < TimeoutRejectedException > ().WaitAndRetryAsync(2, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
}
c#
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);

private static Func < PolicyBuilder < HttpResponseMessage > , IAsyncPolicy < HttpResponseMessage >> ErrorPolicy =>
p => p.Or < TimeoutRejectedException > ().WaitAndRetryAsync(2, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)));
}
Pobiega
Pobiega6mo ago
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
Raki
Raki6mo ago
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);
Pobiega
Pobiega6mo ago
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
Want results from more Discord servers?
Add your server
More Posts