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
maxAttemptsintThe maximum number of attempts (including the initial attempt).
minIntervalTimeSpanThe minimum interval between retry attempts.
maxIntervalTimeSpanThe maximum interval between retry attempts.
retryPredicateFunc<Exception, bool>Optional predicate to determine if an exception should be retried.
loggerIWorkflowForgeLoggerOptional 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
minIntervalTimeSpanMinimum interval between operations.
maxIntervalTimeSpanMaximum 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
maxAttemptsintMaximum number of retry attempts.
loggerIWorkflowForgeLoggerOptional 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
operationFunc<Task>The operation to execute.
cancellationTokenCancellationTokenThe 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
attemptNumberintThe current attempt number (1-based).
exceptionExceptionThe 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
maxAttemptsintMaximum number of retry attempts.
loggerIWorkflowForgeLoggerOptional 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
baseIntervalTimeSpanThe base interval for jitter calculation.
jitterPercentdoubleThe percentage of jitter to apply (0.0 to 1.0).
maxAttemptsintThe maximum number of attempts (default: 3).
loggerIWorkflowForgeLoggerOptional logger for the strategy.
Returns
- RandomIntervalStrategy
A new random interval strategy instance.