HttpError429Handler Class
- Namespace
- Kampute.HttpClient.ErrorHandlers
- Assembly
- Kampute.HttpClient.dll
Definition
Handles '429 Too Many Requests' HTTP responses by attempting to back off and retry the request according to a specified or default backoff strategy.
public class HttpError429Handler : IHttpErrorHandler- Inheritance
- object
- HttpError429Handler
- Implements
Remarks
This handler provides a mechanism to respond to HTTP 429 errors by retrying the request after a delay. The delay duration and retry logic can be customized through the OnBackoffStrategy delegate. If the delegate is not provided, or does not specify a strategy, the handler will look for a rate limit reset header in the response. If the header is present, its value is used to determine the backoff duration. If the header is not present, no retries will be attempted.
Constructors
HttpError429Handler()
Initializes a new instance of HttpError429Handler class.
public HttpError429Handler()Properties
OnBackoffStrategy
A delegate that allows customization of the backoff strategy when a 429 Too Many Requests' response is received.
public Func<HttpResponseErrorContext, Nullable<DateTimeOffset>, IHttpBackoffProvider> OnBackoffStrategy { get; set; }Property Value
- Func<HttpResponseErrorContext, Nullable<DateTimeOffset>, IHttpBackoffProvider>
- A function that takes an HttpResponseErrorContext and an optional DateTimeOffset representing the rate limit reset time, and returns an IHttpBackoffProvider to define the backoff strategy.
Remarks
If this delegate is set and returns an IHttpBackoffProvider, the returned strategy is used for the retry operation. If it is not set, or returns null, the handler will defer to the Retry-After header in the response.
The delegate receives the following parameters:
- context– Provides context about the HTTP response indicating a '429 Too Many Requests' error. It is encapsulated within an HttpResponseErrorContext instance, allowing for an informed decision on the retry strategy.
- resetTime– Indicates the time when the rate limit will be lifted as a DateTimeOffset value. If the server specifies a reset time via response headers, this parameter provides that time, allowing the client to know when to resume requests. If the server does not specify a reset time, the value will be
null.
Methods
CanHandle(HttpStatusCode)
Determines whether this handler can process the specified HTTP status code.
public bool CanHandle(HttpStatusCode statusCode)Parameters
statusCodeHttpStatusCode- The HTTP status code to evaluate.
Returns
Remarks
This implementation specifically handles the HTTP '429 Too Many Requests' status code.
CreateScheduler(HttpResponseErrorContext)
Creates a scheduler for retrying the failed request based on the error context.
protected virtual IRetryScheduler CreateScheduler(HttpResponseErrorContext ctx)Parameters
ctxHttpResponseErrorContext- The context containing information about the HTTP response that indicates a failure.
Returns
- IRetryScheduler
- An IRetryScheduler that schedules the retry attempts.
Exceptions
- ArgumentNullException
- Thrown if
ctxisnull.
Remarks
This method first attempts to use the OnBackoffStrategy delegate to obtain a retry strategy. If the delegate is not provided or returns
null, and a rate limit reset header is present, the value of this header is used to create a retry delay. If neither condition is met, no retries will be attempted.Explicit Interface Implementations
IHttpErrorHandler.DecideOnRetryAsync(HttpResponseErrorContext, CancellationToken)
Evaluates whether a failed request should be retried based on the error context.
Task<HttpErrorHandlerResult> IHttpErrorHandler.DecideOnRetryAsync(HttpResponseErrorContext ctx, CancellationToken cancellationToken)Parameters
ctxHttpResponseErrorContext- The context containing information about the HTTP response that indicates a failure.
cancellationTokenCancellationToken- A token for canceling the operation.
Returns
- Task<HttpErrorHandlerResult>
- A task that resolves to an HttpErrorHandlerResult.
Exceptions
- ArgumentNullException
- Thrown if
ctxisnull.
