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
Implements

Remarks

This handler provides a mechanism to respond to HTTP 503 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 Retry-After header in the response. If the Retry-After header 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.

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-After header, 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-After header, this parameter reflects its value, suggesting an optimal time to retry. If the header is missing, the value is null, 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

statusCode HttpStatusCode
The HTTP status code to evaluate.

Returns

bool
true if the handler can process the status code; otherwise, false.

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

ctx HttpResponseErrorContext
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 ctx is null.

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

ctx HttpResponseErrorContext
The context containing information about the HTTP response that indicates a failure.
cancellationToken CancellationToken
A token for canceling the operation.

Returns

Task<HttpErrorHandlerResult>
A task that resolves to an HttpErrorHandlerResult.

Exceptions

ArgumentNullException
Thrown if ctx is null.

See Also