Created
April 11, 2018 00:34
-
-
Save ctigeek/d9ac9c046e8c930d1acf46974d2b39ca to your computer and use it in GitHub Desktop.
Retry pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class Retry | |
{ | |
public static void Do(Action action, TimeSpan retryInterval, int maxAttemptCount) | |
{ | |
try | |
{ | |
action(); | |
} | |
catch | |
{ | |
if (maxAttemptCount == 1) throw; | |
Thread.Sleep(retryInterval); | |
Do(action, retryInterval, maxAttemptCount - 1); | |
} | |
} | |
public static T Do<T>(Func<T> func, TimeSpan retryInterval, int maxAttemptCount) | |
{ | |
try | |
{ | |
return func(); | |
} | |
catch | |
{ | |
if (maxAttemptCount == 1) throw; | |
Thread.Sleep(retryInterval); | |
return Do(func, retryInterval, maxAttemptCount - 1); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment