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
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

source IRetryStrategy
The underlying retry strategy to which jitter will be added.
jitterFactor double
The factor to apply to the delay to introduce jitter, represented as a value between 0 and 1.

Exceptions

ArgumentNullException
Thrown if source is null.
ArgumentOutOfRangeException
Thrown if jitterFactor is 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

elapsed TimeSpan
The total time elapsed since the start of retry attempts.
attempts uint
The number of retry attempts made so far.
delay TimeSpan
When this method returns, contains the calculated delay for the next retry attempt. This parameter is passed uninitialized.

Returns

bool
true if the underlying strategy indicates that a retry should be attempted; otherwise, false.

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.