HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpCritical

Cleanest way to write retry logic?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
cleanestwritewaylogicretry

Problem

Occasionally I have a need to retry an operation several times before giving up. My code is like:

int retries = 3;
while(true) {
  try {
    DoSomething();
    break; // success!
  } catch {
    if(--retries == 0) throw;
    else Thread.Sleep(1000);
  }
}


I would like to rewrite this in a general retry function like:

TryThreeTimes(DoSomething);


Is it possible in C#? What would be the code for the TryThreeTimes() method?

Solution

Blanket catch statements that simply retry the same call can be dangerous if used as a general exception handling mechanism. Having said that, here's a lambda-based retry wrapper that you can use with any method. I chose to factor the number of retries and the retry timeout out as parameters for a bit more flexibility:

public static class Retry
{
    public static void Do(
        Action action,
        TimeSpan retryInterval,
        int maxAttemptCount = 3)
    {
        Do(() =>
        {
            action();
            return null;
        }, retryInterval, maxAttemptCount);
    }

    public static T Do(
        Func action,
        TimeSpan retryInterval,
        int maxAttemptCount = 3)
    {
        var exceptions = new List();

        for (int attempted = 0; attempted  0)
                {
                    Thread.Sleep(retryInterval);
                }
                return action();
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
        }
        throw new AggregateException(exceptions);
    }
}


You can now use this utility method to perform retry logic:

Retry.Do(() => SomeFunctionThatCanFail(), TimeSpan.FromSeconds(1));


or:

Retry.Do(SomeFunctionThatCanFail, TimeSpan.FromSeconds(1));


or:

int result = Retry.Do(SomeFunctionWhichReturnsInt, TimeSpan.FromSeconds(1), 4);


Or you could even make an async overload.

Code Snippets

public static class Retry
{
    public static void Do(
        Action action,
        TimeSpan retryInterval,
        int maxAttemptCount = 3)
    {
        Do<object>(() =>
        {
            action();
            return null;
        }, retryInterval, maxAttemptCount);
    }

    public static T Do<T>(
        Func<T> action,
        TimeSpan retryInterval,
        int maxAttemptCount = 3)
    {
        var exceptions = new List<Exception>();

        for (int attempted = 0; attempted < maxAttemptCount; attempted++)
        {
            try
            {
                if (attempted > 0)
                {
                    Thread.Sleep(retryInterval);
                }
                return action();
            }
            catch (Exception ex)
            {
                exceptions.Add(ex);
            }
        }
        throw new AggregateException(exceptions);
    }
}
Retry.Do(() => SomeFunctionThatCanFail(), TimeSpan.FromSeconds(1));
Retry.Do(SomeFunctionThatCanFail, TimeSpan.FromSeconds(1));
int result = Retry.Do(SomeFunctionWhichReturnsInt, TimeSpan.FromSeconds(1), 4);

Context

Stack Overflow Q#1563191, score: 650

Revisions (0)

No revisions yet.