Class BackoffStrategies
- Namespace
- Kampute.HttpClient
- Assembly
- Kampute.HttpClient.dll
Provides a collection of factory methods for creating instances of different retry strategies.
public static class BackoffStrategies
- Inheritance
-
BackoffStrategies
- Inherited Members
Remarks
This class offers various retry strategies to manage transient failures in distributed systems for flexible, use case-specific configuration.
Strategies are ordered by increasing delay approach:
- NoneNo retries. Best for critical operations where failures should be immediately addressed without retries.
- OnceA single retry after a delay. Suitable for operations where one additional attempt may resolve a transient issue.
- UniformMultiple retries with constant delays. Ideal for cases needing multiple attempts with predictable delays.
- LinearMultiple retries with delays increasing linearly. Optimal for reducing system load with gradually increasing wait times.
- FibonacciMultiple retries with delays following the Fibonacci sequence. A balanced choice between aggressive and cautious retry pacing.
- ExponentialMultiple retries with delays growing exponentially. For aggressively minimizing impact on systems by rapidly increasing wait times.
The Dynamic
strategy stands apart, as its delay can vary based on the context of the failure. It tailors retry attempts to specific conditions, such
as error type or system load, offering the flexibility to adapt retry logic for optimal outcomes. This approach is most useful in complex systems where a
static retry strategy may not adequately address the nuances of different failure scenarios.
Properties
None
Gets a strategy where no retry attempts are made.
public static IHttpBackoffProvider None { get; }
Property Value
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy of no retry attempts.
Remarks
This strategy schedules no retry attempts, making it ideal for operations where failure handling is immediate or managed through other means.
Methods
Dynamic(Func<HttpRequestErrorContext, IRetryScheduler>)
Creates an instance of DynamicBackoffStrategy with a dynamic scheduler factory based on the context of a failed HTTP request.
public static IHttpBackoffProvider Dynamic(Func<HttpRequestErrorContext, IRetryScheduler> schedulerFactory)
Parameters
schedulerFactory
Func<HttpRequestErrorContext, IRetryScheduler>A factory function that creates IRetryScheduler instances based on the failed HTTP request context.
Returns
- IHttpBackoffProvider
An instance of DynamicBackoffStrategy.
Remarks
This strategy offers the highest flexibility by dynamically scheduling retries based on the specific context of a failure. It adapts to the nature of encountered errors, making it ideal for complex systems with varied types of transient failures that cannot be effectively handled by a static retry strategy.
Exceptions
- ArgumentNullException
Thrown if
schedulerFactory
is null.
Dynamic(Func<HttpRequestErrorContext, IRetryStrategy>)
Creates an instance of DynamicBackoffStrategy with a dynamic strategy factory based on the context of a failed HTTP request.
public static IHttpBackoffProvider Dynamic(Func<HttpRequestErrorContext, IRetryStrategy> strategyFactory)
Parameters
strategyFactory
Func<HttpRequestErrorContext, IRetryStrategy>A factory function that creates IRetryStrategy instances based on the failed HTTP request context.
Returns
- IHttpBackoffProvider
An instance of DynamicBackoffStrategy.
Remarks
This strategy offers the highest flexibility by dynamically scheduling retries based on the specific context of a failure. It adapts to the nature of encountered errors, making it ideal for complex systems with varied types of transient failures that cannot be effectively handled by a static retry strategy.
Exceptions
- ArgumentNullException
Thrown if
strategyFactory
is null.
Exponential(TimeSpan, TimeSpan, double)
Creates a strategy that performs multiple retry attempts with delays increasing exponentially between each attempt, up to a specified timeout.
public static IHttpBackoffProvider Exponential(TimeSpan timeout, TimeSpan initialDelay, double rate = 2)
Parameters
timeout
TimeSpanThe maximum time to spend retrying.
initialDelay
TimeSpanThe delay before the first retry attempt.
rate
doubleThe rate at which the delay increases for each subsequent retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy with delays increasing exponentially between each attempt, up to a specified timeout.
Remarks
This strategy performs multiple retry attempts with delays increasing exponentially between each attempt, up to a specified timeout. It is suitable for aggressively minimizing the impact on systems by rapidly increasing wait times between retry attempts, while enforcing a maximum time limit for retrying.
Exceptions
- ArgumentOutOfRangeException
Thrown if
rate
is less than 1.
Exponential(uint, TimeSpan, double)
Creates a strategy that performs multiple retry attempts with delays increasing exponentially between each attempt, up to a specified maximum number of retry attempts.
public static IHttpBackoffProvider Exponential(uint maxAttempts, TimeSpan initialDelay, double rate = 2)
Parameters
maxAttempts
uintThe maximum number of retry attempts.
initialDelay
TimeSpanThe delay before the first retry attempt.
rate
doubleThe rate at which the delay increases for each subsequent retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy with delays increasing exponentially between each attempt, up to a specified maximum number of retry attempts.
Remarks
This strategy performs multiple retry attempts with delays increasing exponentially between each attempt. It is suitable for aggressively minimizing the impact on systems by rapidly increasing wait times between retry attempts.
Exceptions
- ArgumentOutOfRangeException
Thrown if
rate
is less than 1.
Fibonacci(TimeSpan, TimeSpan)
Creates a strategy that performs multiple retry attempts with delays following the Fibonacci sequence between each attempt, up to a specified timeout.
public static IHttpBackoffProvider Fibonacci(TimeSpan timeout, TimeSpan initialDelay)
Parameters
timeout
TimeSpanThe maximum time to spend retrying.
initialDelay
TimeSpanThe delay before the first retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy with delays following the Fibonacci sequence between each attempt, up to a specified timeout.
Remarks
This strategy performs multiple retry attempts with delays following the Fibonacci sequence between each attempt, up to a specified timeout. It provides a balanced choice between aggressive and cautious retry pacing, suitable for a wide range of scenarios, while enforcing a maximum time limit for retrying.
Fibonacci(TimeSpan, TimeSpan, TimeSpan)
Creates a strategy that performs multiple retry attempts with delays following the Fibonacci sequence between each attempt, up to a specified timeout.
public static IHttpBackoffProvider Fibonacci(TimeSpan timeout, TimeSpan initialDelay, TimeSpan delayStep)
Parameters
timeout
TimeSpanThe maximum time to spend retrying.
initialDelay
TimeSpanThe delay before the first retry attempt.
delayStep
TimeSpanThe fixed amount of time that is scaled by the Fibonacci sequence and added to the initial delay for each subsequent retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy with delays following the Fibonacci sequence between each attempt, up to a specified timeout.
Remarks
This strategy performs multiple retry attempts with delays following the Fibonacci sequence between each attempt, up to a specified timeout. It provides a balanced choice between aggressive and cautious retry pacing, suitable for a wide range of scenarios, while enforcing a maximum time limit for retrying.
Fibonacci(uint, TimeSpan)
Creates a strategy that performs multiple retry attempts with delays following the Fibonacci sequence between each attempt, up to a specified maximum number of retry attempts.
public static IHttpBackoffProvider Fibonacci(uint maxAttempts, TimeSpan initialDelay)
Parameters
maxAttempts
uintThe maximum number of retry attempts.
initialDelay
TimeSpanThe delay before the first retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy with delays following the Fibonacci sequence between each attempt, up to a specified maximum number of retry attempts.
Remarks
This strategy performs multiple retry attempts with delays following the Fibonacci sequence between each attempt. It provides a balanced choice between aggressive and cautious retry pacing, suitable for a wide range of scenarios.
Fibonacci(uint, TimeSpan, TimeSpan)
Creates a strategy that performs multiple retry attempts with delays following the Fibonacci sequence between each attempt, up to a specified maximum number of retry attempts.
public static IHttpBackoffProvider Fibonacci(uint maxAttempts, TimeSpan initialDelay, TimeSpan delayStep)
Parameters
maxAttempts
uintThe maximum number of retry attempts.
initialDelay
TimeSpanThe delay before the first retry attempt.
delayStep
TimeSpanThe fixed amount of time that is scaled by the Fibonacci sequence and added to the initial delay for each subsequent retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy with delays following the Fibonacci sequence between each attempt, up to a specified maximum number of retry attempts.
Remarks
This strategy performs multiple retry attempts with delays following the Fibonacci sequence between each attempt. It provides a balanced choice between aggressive and cautious retry pacing, suitable for a wide range of scenarios.
Linear(TimeSpan, TimeSpan)
Creates a strategy that performs multiple retry attempts with delays increasing linearly between each attempt, up to a specified timeout.
public static IHttpBackoffProvider Linear(TimeSpan timeout, TimeSpan initialDelay)
Parameters
timeout
TimeSpanThe maximum time to spend retrying.
initialDelay
TimeSpanThe delay before the first retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy with delays increasing linearly between each attempt, up to a specified timeout.
Remarks
This strategy performs multiple retry attempts with delays increasing linearly between each attempt, up to a specified timeout. It is optimal for reducing system load with gradually increasing wait times, while enforcing a maximum time limit for retrying.
Linear(TimeSpan, TimeSpan, TimeSpan)
Creates a strategy that performs multiple retry attempts with delays increasing linearly between each attempt, up to a specified timeout.
public static IHttpBackoffProvider Linear(TimeSpan timeout, TimeSpan initialDelay, TimeSpan delayStep)
Parameters
timeout
TimeSpanThe maximum time to spend retrying.
initialDelay
TimeSpanThe delay before the first retry attempt.
delayStep
TimeSpanThe amount by which the delay increases for each subsequent retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy with delays increasing linearly between each attempt, up to a specified maximum number of retry attempts.
Remarks
This strategy performs multiple retry attempts with delays increasing linearly between each attempt, up to a specified timeout. It is optimal for reducing system load with gradually increasing wait times, while enforcing a maximum time limit for retrying.
Linear(uint, TimeSpan)
Creates a strategy that performs multiple retry attempts with delays increasing linearly between each attempt, up to a specified maximum number of retry attempts.
public static IHttpBackoffProvider Linear(uint maxAttempts, TimeSpan initialDelay)
Parameters
maxAttempts
uintThe maximum number of retry attempts.
initialDelay
TimeSpanThe delay before the first retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy with delays increasing linearly between each attempt, up to a specified timeout.
Remarks
This strategy performs multiple retry attempts with delays increasing linearly between each attempt. It is optimal for reducing system load with gradually increasing wait times.
Linear(uint, TimeSpan, TimeSpan)
Creates a strategy that performs multiple retry attempts with delays increasing linearly between each attempt, up to a specified maximum number of retry attempts.
public static IHttpBackoffProvider Linear(uint maxAttempts, TimeSpan initialDelay, TimeSpan delayStep)
Parameters
maxAttempts
uintThe maximum number of retry attempts.
initialDelay
TimeSpanThe delay before the first retry attempt.
delayStep
TimeSpanThe amount by which the delay increases for each subsequent retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy of multiple attempts with a constant delay between each attempt, up to a specified timeout.
Remarks
This strategy performs multiple retry attempts with delays increasing linearly between each attempt. It is optimal for reducing system load with gradually increasing wait times.
Once(DateTimeOffset)
Creates a strategy that performs a single retry attempt after the specified date and time.
public static IHttpBackoffProvider Once(DateTimeOffset after)
Parameters
after
DateTimeOffsetThe date and time after which the single retry attempt will be made.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy of a single attempt after a specified date and time.
Remarks
This strategy schedules a single retry attempt for a specified future point in time, ensuring operations are retried when certain conditions are likely met. If the specified time has already passed, it immediately schedules the retry attempt.
Once(TimeSpan)
Creates a strategy that performs a single retry attempt after the specified delay.
public static IHttpBackoffProvider Once(TimeSpan delay)
Parameters
delay
TimeSpanThe delay before the single retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy of a single attempt after a specified delay.
Remarks
This strategy performs a single retry attempt after the specified delay. It is suitable for operations where one additional attempt may resolve a transient issue.
Uniform(TimeSpan, TimeSpan)
Creates a strategy that performs multiple retry attempts with a constant delay between each attempt, up to a specified timeout.
public static IHttpBackoffProvider Uniform(TimeSpan timeout, TimeSpan delay)
Parameters
timeout
TimeSpanThe maximum time to spend retrying.
delay
TimeSpanThe constant delay between each retry attempt.
Returns
Remarks
This strategy performs multiple retry attempts with a constant delay between each attempt, up to a specified timeout. It is ideal for cases needing multiple attempts with predictable delays, but with a maximum time limit for retrying.
Uniform(uint, TimeSpan)
Creates a strategy that performs multiple retry attempts with a constant delay between each attempt, up to a specified maximum number of retry attempts.
public static IHttpBackoffProvider Uniform(uint maxAttempts, TimeSpan delay)
Parameters
maxAttempts
uintThe maximum number of retry attempts.
delay
TimeSpanThe constant delay between each retry attempt.
Returns
- IHttpBackoffProvider
An IHttpBackoffProvider that defines a retry strategy of multiple attempts with a constant delay between each attempt, up to a specified maximum number of retry attempts.
Remarks
This strategy performs multiple retry attempts with a constant delay between each attempt. It is ideal for cases needing multiple attempts with predictable delays.