1
Rate limiting controls the consumption rate of resources by limiting the number of operations that can execute within a specific time frame. This protects downstream dependencies from traffic spikes.
Using Rate Limiter in Polly v8
Polly v8 integrates directly with System.Threading.RateLimiting primitives.
Code Example
var rateLimiterOptions = new SlidingWindowRateLimiterOptions
{
PermitLimit = 100,
Window = TimeSpan.FromMinutes(1),
SegmentsPerWindow = 4,
QueueLimit = 10
};
var pipeline = new ResiliencePipelineBuilder()
.AddRateLimiter(new RateLimiterStrategyOptions
{
DefaultRateLimiter = new SlidingWindowRateLimiter(rateLimiterOptions)
})
.Build();
await pipeline.ExecuteAsync(async cancellationToken =>
{
await SendApiRequestAsync(cancellationToken);
});