RetryableHttpErrorHandler Class

Namespace
Kampute.HttpClient.ErrorHandlers.Abstracts
Assembly
  • Kampute.HttpClient.dll

Definition

Provides the base functionality for handling HTTP responses with transient error status codes by attempting to back off and retry the request according to a specified or default backoff strategy.
public abstract class RetryableHttpErrorHandler : IHttpErrorHandler
Inheritance
  • object
  • RetryableHttpErrorHandler
Implements
Inherited by

Remarks

This handler class is designed to be extended for specific transient error status codes. It offers a mechanism to respond to transient HTTP errors by retrying the request after a delay. The delay duration and retry logic can be customized through the OnBackoffStrategy delegate.

Properties

OnBackoffStrategy

A delegate that allows customization of the backoff strategy when responses with transient error status codes are 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, 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, a default behavior is applied.

The delegate receives the following parameters:

  • context Provides context about the HTTP response that indicates a transient error. It is encapsulated within an HttpResponseErrorContext instance, allowing for an informed decision on the retry strategy.
  • retryTime Advises on the next retry attempt timing as a DateTimeOffset value if the response suggests one. If the response does not include a suggested retry time, the value will be null.

Methods

CanHandle(HttpStatusCode)

Determines whether this handler can process the specified HTTP status code.
public abstract 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.

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

The method uses OnBackoffStrategy when available. If the delegate is not provided or returns null, and the response includes a suggested retry time, a single retry at that time is used. Otherwise the client's default backoff strategy is used.

GetDefaultStrategy(HttpResponseErrorContext, Nullable<DateTimeOffset>)

Provides the default backoff strategy when no custom strategy is specified.
protected virtual IHttpBackoffProvider GetDefaultStrategy(HttpResponseErrorContext ctx, Nullable<DateTimeOffset> retryTime)

Parameters

ctx HttpResponseErrorContext
The context containing information about the HTTP response.
retryTime Nullable<DateTimeOffset>
The suggested retry time, if any.

Returns

IHttpBackoffProvider
An IHttpBackoffProvider representing the default backoff strategy.

Exceptions

ArgumentNullException
Thrown if ctx is null.

GetSuggestedRetryTime(HttpResponseErrorContext)

Extracts the suggested retry time from the HTTP response's header, if present.
protected virtual Nullable<DateTimeOffset> GetSuggestedRetryTime(HttpResponseErrorContext ctx)

Parameters

ctx HttpResponseErrorContext
The context containing information about the HTTP response.

Returns

Nullable<DateTimeOffset>
The suggested DateTimeOffset to retry the request, or null if the header is not present.

Exceptions

ArgumentNullException
Thrown if ctx is null.

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