ExponentialStrategy Class
- Namespace
- Kampute.HttpClient.RetryManagement.Strategies
- Assembly
- Kampute.HttpClient.dll
Definition
A retry strategy that exponentially increases the delay before each retry attempt.
public sealed class ExponentialStrategy : IRetryStrategy- Inheritance
- object
- ExponentialStrategy
- Implements
Remarks
The ExponentialStrategy class calculates the delay between retry attempts by starting with an initial delay and then increasing it exponentially with each subsequent retry. This strategy is effective in scenarios where the load on the underlying systems needs to be progressively reduced in the face of ongoing failures, or where it is beneficial to wait longer between attempts to increase the chance of success.
Constructors
ExponentialStrategy(TimeSpan, double)
Initializes a new instance of the ExponentialStrategy class with a specified initial delay and exponential rate.
public ExponentialStrategy(TimeSpan initialDelay, double rate)Parameters
initialDelayTimeSpan- The initial delay duration before the first retry attempt.
ratedouble- The rate at which the delay duration increases exponentially between retries.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
rateis less than 1.
Properties
InitialDelay
Gets the initial delay duration before the first retry attempt.
public TimeSpan InitialDelay { get; }Property Value
- TimeSpan
- The initial delay duration before the first retry attempt.
Rate
Gets the rate at which the delay duration increases exponentially between retries.
public double Rate { get; }Property Value
- double
- The rate at which the delay duration increases exponentially between retries.
Methods
TryGetRetryDelay(TimeSpan, uint, out TimeSpan)
Calculates the delay for the next retry attempt, exponentially increasing based on the number of attempts made so far.
public bool TryGetRetryDelay(TimeSpan elapsed, uint attempts, out TimeSpan delay)Parameters
elapsedTimeSpan- The total time elapsed since the start of retry attempts. This parameter is ignored in this implementation.
attemptsuint- The number of retry attempts made so far.
delayTimeSpan- When this method returns, contains the calculated delay for the next retry attempt. This parameter is passed uninitialized.
Returns
Extension Methods
| ToBackoffStrategy(this IRetryStrategy) | Converts an IRetryStrategy into a BackoffStrategy, creating a factory capable of producing schedulers based on the provided strategy. |
| ToScheduler(this IRetryStrategy) | Converts an IRetryStrategy into a RetryScheduler, creating a scheduler instance based on the provided strategy. |
| WithJitter(this IRetryStrategy, double) | Enhances a retry strategy with jitter to add randomness to the retry delay. |
| WithMaxAttempts(this IRetryStrategy, uint) | Enhances a retry strategy with a maximum number of retry attempts. |
| WithTimeout(this IRetryStrategy, TimeSpan) | Enhances a retry strategy with a timeout, limiting the total duration allowed for retry attempts. |
