HttpError503Handler Class
- Namespace
- Kampute.HttpClient.ErrorHandlers
- Assembly
- Kampute.HttpClient.dll
Definition
Handles '503 Service Unavailable' HTTP responses by attempting to back off and retry the request according to a specified or default backoff strategy.
public class HttpError503Handler : IHttpErrorHandler- Inheritance
- object
- HttpError503Handler
- Implements
Remarks
Constructors
HttpError503Handler()
Initializes a new instance of HttpError503Handler class.
public HttpError503Handler()Properties
OnBackoffStrategy
A delegate that allows customization of the backoff strategy when a '503 Service Unavailable' 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 suggested retry time from the
Retry-Afterheader, and returns an IHttpBackoffProvider to be used for the retry operation.
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 or the client's default backoff strategy.
The delegate receives the following parameters:
- context– Provides context about the HTTP response indicating a '503 Service Unavailable' error. It is encapsulated within an HttpResponseErrorContext instance, allowing for an informed decision on the retry strategy.
- retryAfter– Advises on the next retry attempt timing as a DateTimeOffset value. If the response includes a
Retry-Afterheader, this parameter reflects its value, suggesting an optimal time to retry. If the header is missing, the value isnull, indicating no specific suggestion from the server.
Methods
CanHandle(HttpStatusCode)
Determines whether this handler can process the specified HTTP status code.
public virtual bool CanHandle(HttpStatusCode statusCode)Parameters
statusCodeHttpStatusCode- The HTTP status code to evaluate.
Returns
Remarks
This implementation specifically handles the HTTP '503 Service Unavailable' 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 Retry-After header is present, the value of this header is used to create a retry delay. If neither condition is met, the client's default backoff strategy is utilized.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.

Retry-Afterheader in the response. If theRetry-Afterheader is present, its value is used to determine the backoff duration. If the header is not present, the default backoff strategy of the HttpRestClient is used.