Table of Contents

Class RandomIntervalStrategy

Namespace
WorkflowForge.Extensions.Resilience.Strategies
Assembly
WorkflowForge.Extensions.Resilience.dll

Implements a resilience strategy with random intervals between retry attempts to reduce thundering herd problems.

[SuppressMessage("Security", "S2245:Random is used for non-security-sensitive retry jitter", Justification = "System.Random is intentionally used for retry delay jitter; cryptographic randomness is not required")]
public sealed class RandomIntervalStrategy : ResilienceStrategyBase, IWorkflowResilienceStrategy
Inheritance
RandomIntervalStrategy
Implements
Inherited Members

Constructors

RandomIntervalStrategy(int, TimeSpan, TimeSpan, Func<Exception, bool>?, IWorkflowForgeLogger?)

Initializes a new instance of the RandomIntervalStrategy class.

public RandomIntervalStrategy(int maxAttempts, TimeSpan minInterval, TimeSpan maxInterval, Func<Exception, bool>? retryPredicate = null, IWorkflowForgeLogger? logger = null)

Parameters

maxAttempts int

The maximum number of attempts (including the initial attempt).

minInterval TimeSpan

The minimum interval between retry attempts.

maxInterval TimeSpan

The maximum interval between retry attempts.

retryPredicate Func<Exception, bool>

Optional predicate to determine if an exception should be retried.

logger IWorkflowForgeLogger

Optional logger for the strategy.

Methods

Create(TimeSpan, TimeSpan)

Creates a random interval strategy that reduces thundering herd effects by introducing random delays between min and max values.

public static RandomIntervalStrategy Create(TimeSpan minInterval, TimeSpan maxInterval)

Parameters

minInterval TimeSpan

Minimum interval between operations.

maxInterval TimeSpan

Maximum interval between operations.

Returns

RandomIntervalStrategy

A random interval strategy.

Default(int, IWorkflowForgeLogger?)

Creates a default random interval strategy with reasonable defaults.

public static RandomIntervalStrategy Default(int maxAttempts = 3, IWorkflowForgeLogger? logger = null)

Parameters

maxAttempts int

Maximum number of retry attempts.

logger IWorkflowForgeLogger

Optional logger for strategy events.

Returns

RandomIntervalStrategy

A new random interval strategy.

ExecuteAsync(Func<Task>, CancellationToken)

Executes an operation with resilience handling.

public override Task ExecuteAsync(Func<Task> operation, CancellationToken cancellationToken = default)

Parameters

operation Func<Task>

The operation to execute.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

A task representing the asynchronous operation.

GetRetryDelay(int, Exception?)

Gets the delay before the next retry attempt.

public override TimeSpan GetRetryDelay(int attemptNumber, Exception? exception)

Parameters

attemptNumber int

The current attempt number (1-based).

exception Exception

The exception that occurred, if any.

Returns

TimeSpan

The delay before the next retry attempt.

HighThroughput(int, IWorkflowForgeLogger?)

Creates a random interval strategy optimized for high-throughput scenarios.

public static RandomIntervalStrategy HighThroughput(int maxAttempts = 5, IWorkflowForgeLogger? logger = null)

Parameters

maxAttempts int

Maximum number of retry attempts.

logger IWorkflowForgeLogger

Optional logger for strategy events.

Returns

RandomIntervalStrategy

A new random interval strategy.

WithJitter(TimeSpan, double, int, IWorkflowForgeLogger?)

Creates a random interval strategy with jitter for exponential backoff scenarios.

public static RandomIntervalStrategy WithJitter(TimeSpan baseInterval, double jitterPercent = 0.2, int maxAttempts = 3, IWorkflowForgeLogger? logger = null)

Parameters

baseInterval TimeSpan

The base interval for jitter calculation.

jitterPercent double

The percentage of jitter to apply (0.0 to 1.0).

maxAttempts int

The maximum number of attempts (default: 3).

logger IWorkflowForgeLogger

Optional logger for the strategy.

Returns

RandomIntervalStrategy

A new random interval strategy instance.