Table of Contents

Class JitterStrategyModifier

Namespace
Kampute.HttpClient.RetryManagement.Strategies.Modifiers
Assembly
Kampute.HttpClient.dll

A retry strategy that adds random jitter to the delay durations provided by another retry strategy.

public sealed class JitterStrategyModifier : IRetryStrategy
Inheritance
JitterStrategyModifier
Implements
Inherited Members
Extension Methods

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.

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.

Exceptions

ArgumentNullException

Thrown if source is null.

ArgumentOutOfRangeException

Thrown if jitterFactor is not between 0 and 1.

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.