RetryStrategies Class
- Namespace
- Kampute.Resilience
- Assembly
- Kampute.Resilience.dll
Definition
Provides factory methods for the built-in retry strategies.
public static class RetryStrategies- Inheritance
- object
- RetryStrategies
Remarks
Examples
This strategy waits about one second before the first retry and doubles the delay for each further retry, up to 30 seconds. It allows at most five retries, and none after two minutes:
var retry = RetryStrategies
.Exponential(TimeSpan.FromSeconds(1))
.WithMaxDelay(TimeSpan.FromSeconds(30))
.WithJitter(0.2)
.WithMaxRetries(5)
.WithMaxElapsedTime(TimeSpan.FromMinutes(2));Properties
None
Gets a strategy that never retries.
public static IRetryStrategy None { get; }Property Value
- IRetryStrategy
- A strategy that never retries.
Methods
Constant(TimeSpan)
Creates a strategy that waits the same delay before every retry.
public static IRetryStrategy Constant(TimeSpan delay)Parameters
delayTimeSpan- The delay before each retry.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
delayis negative.
Constant(int)
Creates a strategy that waits the same number of milliseconds before every retry.
public static IRetryStrategy Constant(int millisecondsDelay)Parameters
millisecondsDelayint- The number of milliseconds to wait before each retry.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
millisecondsDelayis negative.
Exponential(TimeSpan, double)
Creates a strategy whose delay is multiplied by a fixed factor before each further retry.
public static IRetryStrategy Exponential(TimeSpan initialDelay, double multiplier = 2)Parameters
initialDelayTimeSpan- The delay before the first retry.
multiplierdouble optional- The factor by which the delay is multiplied for each further retry (optional). The default is 2, which doubles the delay.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
initialDelayis negative, ormultiplieris less than 1 or is double.NaN.
Exponential(int, double)
Creates a strategy whose delay, in milliseconds, is multiplied by a fixed factor before each further retry.
public static IRetryStrategy Exponential(int millisecondsInitialDelay, double multiplier = 2)Parameters
millisecondsInitialDelayint- The number of milliseconds to wait before the first retry.
multiplierdouble optional- The factor by which the delay is multiplied for each further retry (optional). The default is 2, which doubles the delay.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
millisecondsInitialDelayis negative, ormultiplieris less than 1 or is double.NaN.
Fibonacci(TimeSpan)
Creates a strategy whose delay grows with the Fibonacci sequence, scaled by the initial delay.
public static IRetryStrategy Fibonacci(TimeSpan initialDelay)Parameters
initialDelayTimeSpan- The delay before the first retry, which is also the amount scaled by the Fibonacci sequence for each further retry.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
initialDelayis negative.
Fibonacci(int)
Creates a strategy whose delay, in milliseconds, grows with the Fibonacci sequence, scaled by the initial delay.
public static IRetryStrategy Fibonacci(int millisecondsInitialDelay)Parameters
millisecondsInitialDelayint- The number of milliseconds to wait before the first retry, which is also the amount scaled by the Fibonacci sequence for each further retry.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
millisecondsInitialDelayis negative.
Fibonacci(TimeSpan, TimeSpan)
Creates a strategy whose delay grows with the Fibonacci sequence, scaled by a fixed step.
public static IRetryStrategy Fibonacci(TimeSpan initialDelay, TimeSpan delayStep)Parameters
initialDelayTimeSpan- The delay before the first retry.
delayStepTimeSpan- The amount scaled by the Fibonacci sequence and added to the initial delay for each further retry.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
initialDelayordelayStepis negative.
Fibonacci(int, int)
Creates a strategy whose delay, in milliseconds, grows with the Fibonacci sequence, scaled by a fixed step.
public static IRetryStrategy Fibonacci(int millisecondsInitialDelay, int millisecondsDelayStep)Parameters
millisecondsInitialDelayint- The number of milliseconds to wait before the first retry.
millisecondsDelayStepint- The number of milliseconds scaled by the Fibonacci sequence and added to the initial delay for each further retry.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
millisecondsInitialDelayormillisecondsDelayStepis negative.
Linear(TimeSpan)
Creates a strategy whose delay grows by the initial delay before each further retry.
public static IRetryStrategy Linear(TimeSpan initialDelay)Parameters
initialDelayTimeSpan- The delay before the first retry, which is also the amount added for each further retry.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
initialDelayis negative.
Linear(int)
Creates a strategy whose delay, in milliseconds, grows by the initial delay before each further retry.
public static IRetryStrategy Linear(int millisecondsInitialDelay)Parameters
millisecondsInitialDelayint- The number of milliseconds to wait before the first retry, which is also the amount added for each further retry.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
millisecondsInitialDelayis negative.
Linear(TimeSpan, TimeSpan)
Creates a strategy whose delay grows by a fixed step before each further retry.
public static IRetryStrategy Linear(TimeSpan initialDelay, TimeSpan delayStep)Parameters
initialDelayTimeSpan- The delay before the first retry.
delayStepTimeSpan- The amount added to the delay for each further retry.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
initialDelayordelayStepis negative.
Linear(int, int)
Creates a strategy whose delay, in milliseconds, grows by a fixed step before each further retry.
public static IRetryStrategy Linear(int millisecondsInitialDelay, int millisecondsDelayStep)Parameters
millisecondsInitialDelayint- The number of milliseconds to wait before the first retry.
millisecondsDelayStepint- The number of milliseconds added to the delay for each further retry.
Returns
- IRetryStrategy
- A strategy that retries without limit.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
millisecondsInitialDelayormillisecondsDelayStepis negative.
Once(TimeSpan)
Creates a strategy that retries once, after the specified delay.
public static IRetryStrategy Once(TimeSpan delay)Parameters
delayTimeSpan- The delay before the retry.
Returns
- IRetryStrategy
- A strategy that allows a single retry.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
delayis negative.
Once(int)
Creates a strategy that retries once, after the specified number of milliseconds.
public static IRetryStrategy Once(int millisecondsDelay)Parameters
millisecondsDelayint- The number of milliseconds to wait before the retry.
Returns
- IRetryStrategy
- A strategy that allows a single retry.
Exceptions
- ArgumentOutOfRangeException
- Thrown if
millisecondsDelayis negative.
Once(DateTimeOffset)
Creates a strategy that retries once, after the time that remains until the specified time when this method is called.
public static IRetryStrategy Once(DateTimeOffset after)Parameters
afterDateTimeOffset- The time to retry at. A past time produces a zero delay.
Returns
- IRetryStrategy
- A strategy that allows a single retry.
Remarks
The delay is fixed when this method is called, not when the operation fails. Create the strategy when the retry time becomes known, such as from a response that suggests it; a strategy that is stored and used later waits the same delay after each failure, whatever the time then.

Except for None and
Once, all of the factory methods return strategies that retry without limit. Chain WithMaxRetries(this IRetryStrategy, uint) and WithMaxElapsedTime(this IRetryStrategy, TimeSpan) to limit them, WithMaxDelay(this IRetryStrategy, TimeSpan) to cap their delays, and WithJitter(this IRetryStrategy, double) to spread their delays, in any combination.Each method that takes a TimeSpan has an overload that takes the same duration as a number of milliseconds. Both forms require nonnegative durations.