Skip to content

Instantly share code, notes, and snippets.

@jessebarocio
Last active June 7, 2016 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessebarocio/261f366a57410d44cf3bd05826e01b6b to your computer and use it in GitHub Desktop.
Save jessebarocio/261f366a57410d44cf3bd05826e01b6b to your computer and use it in GitHub Desktop.
var resource = new MyRemoteResource();
int initialRetryInterval = 2000; // 2 seconds
int maxAttempts = 5;
int currentRetryInterval = initialRetryInterval;
// If the connection fails then we'll try reconnecting a maximum number of 5 times
// before throwing the exception. The interval between retries will double each time.
for(int i = 1; i <= maxAttempts; i++)
{
try
{
resource.Connect();
break;
}
catch
{
if(i == maxAttempts)
throw;
Thread.Sleep(currentRetryInterval);
currentRetryInterval *= 2;
}
}
var resource = new MyRemoteResource();
int retryInterval = 2000; // 2 seconds
int maxAttempts = 5;
// If the connection fails then we'll try reconnecting every 2 seconds a maximum number
// of 5 times before throwing the exception.
for(int i = 1; i <= maxAttempts; i++)
{
try
{
resource.Connect();
break;
}
catch
{
if(i == maxAttempts)
throw;
Thread.Sleep(retryInterval);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Demos
{
class Program
{
static void Main( string[] args )
{
var service = new MyRemoteService();
// Retry simple operations that don't return anything.
RetryHelpers.LinearRetry( service.Connect );
RetryHelpers.ExponentialBackoff( service.Connect );
// Retry operations that do return values.
MyResource resource = RetryHelpers.LinearRetry( () =>
{
return service.GetResource( 5 );
} );
MyResource resource2 = RetryHelpers.ExponentialBackoff( () =>
{
return service.GetResource( 5 );
} );
}
}
}
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment