RetryStrategies Class

Namespace
Kampute.Resilience
Assembly
  • Kampute.Resilience.dll

Definition

Provides factory methods for the built-in retry strategies.
public static class RetryStrategies
Inheritance

Remarks

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.

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

delay TimeSpan
The delay before each retry.

Returns

IRetryStrategy
A strategy that retries without limit.

Exceptions

ArgumentOutOfRangeException
Thrown if delay is negative.

Constant(int)

Creates a strategy that waits the same number of milliseconds before every retry.
public static IRetryStrategy Constant(int millisecondsDelay)

Parameters

millisecondsDelay int
The number of milliseconds to wait before each retry.

Returns

IRetryStrategy
A strategy that retries without limit.

Exceptions

ArgumentOutOfRangeException
Thrown if millisecondsDelay is 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

initialDelay TimeSpan
The delay before the first retry.
multiplier double 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 initialDelay is negative, or multiplier is 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

millisecondsInitialDelay int
The number of milliseconds to wait before the first retry.
multiplier double 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 millisecondsInitialDelay is negative, or multiplier is 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

initialDelay TimeSpan
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 initialDelay is 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

millisecondsInitialDelay int
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 millisecondsInitialDelay is 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

initialDelay TimeSpan
The delay before the first retry.
delayStep TimeSpan
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 initialDelay or delayStep is 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

millisecondsInitialDelay int
The number of milliseconds to wait before the first retry.
millisecondsDelayStep int
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 millisecondsInitialDelay or millisecondsDelayStep is negative.

Linear(TimeSpan)

Creates a strategy whose delay grows by the initial delay before each further retry.
public static IRetryStrategy Linear(TimeSpan initialDelay)

Parameters

initialDelay TimeSpan
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 initialDelay is 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

millisecondsInitialDelay int
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 millisecondsInitialDelay is 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

initialDelay TimeSpan
The delay before the first retry.
delayStep TimeSpan
The amount added to the delay for each further retry.

Returns

IRetryStrategy
A strategy that retries without limit.

Exceptions

ArgumentOutOfRangeException
Thrown if initialDelay or delayStep is 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

millisecondsInitialDelay int
The number of milliseconds to wait before the first retry.
millisecondsDelayStep int
The number of milliseconds added to the delay for each further retry.

Returns

IRetryStrategy
A strategy that retries without limit.

Exceptions

ArgumentOutOfRangeException
Thrown if millisecondsInitialDelay or millisecondsDelayStep is negative.

Once(TimeSpan)

Creates a strategy that retries once, after the specified delay.
public static IRetryStrategy Once(TimeSpan delay)

Parameters

delay TimeSpan
The delay before the retry.

Returns

IRetryStrategy
A strategy that allows a single retry.

Exceptions

ArgumentOutOfRangeException
Thrown if delay is negative.

Once(int)

Creates a strategy that retries once, after the specified number of milliseconds.
public static IRetryStrategy Once(int millisecondsDelay)

Parameters

millisecondsDelay int
The number of milliseconds to wait before the retry.

Returns

IRetryStrategy
A strategy that allows a single retry.

Exceptions

ArgumentOutOfRangeException
Thrown if millisecondsDelay is 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

after DateTimeOffset
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.