Provides a flexible retry policy system for handling rate limits and transient errors. Easily configure automatic retries with customizable delays, backoff strategies, and error handling for more resilient applications.
$ dotnet add package Incendium.RetryPolicyIncendium.RetryPolicy is a lightweight .NET standard 2.1 library that provides robust HTTP request retry functionality and rate limiting capabilities through two main components:
RetryHttpClientHandler: Handles automatic retry of failed HTTP requestsRateGate: Manages rate limiting for your API callsUsing Package Manager:
PM> Install-Package Incendium.RetryPolicy
Using .NET CLI:
dotnet add package Incendium.RetryPolicy
The handler automatically retries requests in the following scenarios:
HttpRequestException)Basic setup:
var handler = new RetryHttpClientHandler(new HttpClientHandler())
{
RetryCount = 5, // sets 5 retry attempts
RetryOnHttpRequestException = true, // sets the retry flag in case of an HttpRequestException
FirstRetryDelay = TimeSpan.FromMilliseconds(100), // sets the median starting delay between requests
}
var client = new HttpClient(handler);
Integrate rate limiting using RateGate:
var handler = new RetryHttpClientHandler(new HttpClientHandler())
{
RateGate = new RateGate(
occurrences: 10, // Maximum requests
timeUnit: TimeSpan.FromSeconds(60) // Time window
)
};
The library provides three built-in delay strategies:
handler.RetryDelaysFactory = () => Delays.Constant(
delay: TimeSpan.FromSeconds(1),
count: 5
);
handler.RetryDelaysFactory = () => Delays.Exponential(
firstDelay: TimeSpan.FromMilliseconds(100),
count: 5
);
handler.RetryDelaysFactory = () => Delays.DecorrelatedJitterBackoffV2(
firstDelay: TimeSpan.FromMilliseconds(100),
count: 5
);
You can also implement custom delay strategies by providing your own IEnumerable<TimeSpan> through the RetryDelaysFactory property.
This project is licensed under the MIT License - see the LICENSE file for details.