JitterStrategyModifier Class
- Namespace
- Kampute.HttpClient.RetryManagement.Strategies.Modifiers
- Assembly
- Kampute.HttpClient.dll
Definition
A retry strategy that adds random jitter to the delay durations provided by another retry strategy.
public sealed class JitterStrategyModifier : IRetryStrategy- Inheritance
- object
- JitterStrategyModifier
- Implements
Remarks
Jitter is added to the delays to prevent thundering herd problems and to provide a more distributed set of retry attempts over time. This can be beneficial in high-load scenarios where many clients are retrying operations simultaneously.
Constructors
JitterStrategyModifier(IRetryStrategy, double)
Initializes a new instance of the JitterStrategyModifier class with a specified source retry strategy and jitter factor.
public JitterStrategyModifier(IRetryStrategy source, double jitterFactor)Parameters
sourceIRetryStrategy- The underlying retry strategy to which jitter will be added.
jitterFactordouble- The factor to apply to the delay to introduce jitter, represented as a value between 0 and 1.
Exceptions
- ArgumentNullException
- Thrown if
sourceisnull. - ArgumentOutOfRangeException
- Thrown if
jitterFactoris not between 0 and 1.
Remarks
The jitter factor allows fine-tuning of the randomness applied to the retry delay, enabling a balance between predictability and the benefits of desynchronization. It is a double value between 0 and 1 that determines the maximum proportion of the delay that can be adjusted randomly to introduce jitter. A value of 0 means no jitter, while 1 allows the delay to vary by up to ±100% of the base delay.
Properties
JitterFactor
Gets the factor to apply to the delay to introduce jitter.
public double JitterFactor { get; }Property Value
- double
- The factor to apply to the delay to introduce jitter. It is a floating-point number between 0 and 1, inclusive.
Source
Gets the underlying retry strategy to which jitter is added.
public IRetryStrategy Source { get; }Property Value
- IRetryStrategy
- The underlying IRetryStrategy to which jitter is added.
Methods
TryGetRetryDelay(TimeSpan, uint, out TimeSpan)
Calculates the delay for the next retry attempt, adding random jitter based on the jitter factor to the delay provided by the underlying strategy.
public bool TryGetRetryDelay(TimeSpan elapsed, uint attempts, out TimeSpan delay)Parameters
elapsedTimeSpan- The total time elapsed since the start of retry attempts.
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. |
