|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Text; |
|
using System.Threading; |
|
using System.Threading.Tasks; |
|
|
|
namespace Demos |
|
{ |
|
public static class RetryHelpers |
|
{ |
|
/// <summary> |
|
/// Retries an action at a specified interval. |
|
/// </summary> |
|
/// <param name="action">The action to retry.</param> |
|
/// <param name="retryInterval">The retry interval in milliseconds.</param> |
|
/// <param name="maxAttempts">The maximum number of times to retry before throwing the exception.</param> |
|
public static void LinearRetry( Action action, int retryInterval = 2000, int maxAttempts = 5 ) |
|
{ |
|
for ( int i = 1; i <= maxAttempts; i++ ) |
|
{ |
|
try |
|
{ |
|
action(); |
|
return; |
|
} |
|
catch |
|
{ |
|
if ( i == maxAttempts ) |
|
throw; |
|
Thread.Sleep( retryInterval ); |
|
} |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// Retries an action at a specified interval and returns the result of that action (if successful). |
|
/// </summary> |
|
/// <typeparam name="T">The expected return type.</typeparam> |
|
/// <param name="action">The action to retry.</param> |
|
/// <param name="retryInterval">The retry interval in milliseconds.</param> |
|
/// <param name="maxAttempts">The maximum number of times to retry before throwing the exception.</param> |
|
/// <returns>The result of the action (if successful).</returns> |
|
public static T LinearRetry<T>( Func<T> action, int retryInterval = 2000, int maxAttempts = 5 ) |
|
{ |
|
T result = default( T ); |
|
LinearRetry( () => { result = action(); }, maxAttempts, retryInterval ); |
|
return result; |
|
} |
|
|
|
/// <summary> |
|
/// Retries an action at an exponentially-increasing interval. |
|
/// </summary> |
|
/// <param name="action">The action to retry.</param> |
|
/// <param name="initialInterval">The initial retry interval.</param> |
|
/// <param name="maxAttempts">The maximum number of times to retry before throwing the exception.</param> |
|
/// <param name="exponent">The exponent used to increase the initial interval between retries.</param> |
|
public static void ExponentialBackoff( Action action, int initialInterval = 2000, int maxAttempts = 5, int exponent = 2 ) |
|
{ |
|
int retryInterval = initialInterval; |
|
for ( int i = 1; i <= maxAttempts; i++ ) |
|
{ |
|
try |
|
{ |
|
action(); |
|
return; |
|
} |
|
catch |
|
{ |
|
if ( i == maxAttempts ) |
|
throw; |
|
Thread.Sleep( retryInterval ); |
|
retryInterval *= exponent; |
|
} |
|
} |
|
} |
|
|
|
/// <summary> |
|
/// Retries an action at an exponentially-increasing interval and returns the result of that action (if successful). |
|
/// </summary> |
|
/// <typeparam name="T">The expected return type.</typeparam> |
|
/// <param name="action">The action to retry.</param> |
|
/// <param name="initialInterval">The initial retry interval.</param> |
|
/// <param name="maxAttempts">The maximum number of times to retry before throwing the exception.</param> |
|
/// <param name="exponent">The exponent used to increase the initial interval between retries.</param> |
|
/// <returns>The result of the action (if successful).</returns> |
|
public static T ExponentialBackoff<T>( Func<T> action, int initialInterval = 2000, int maxAttempts = 5, int exponent = 2 ) |
|
{ |
|
T result = default( T ); |
|
ExponentialBackoff( () => { result = action(); }, initialInterval, maxAttempts, exponent ); |
|
return result; |
|
} |
|
} |
|
} |