Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static RetryPolicy RetryExponential(int retryCount, TimeSpan minBackoff, TimeSpan maxBackoff, TimeSpan deltaBackoff)
- {
- // Do any argument Pre-validation here, i.e. enforce max retry count etc.
- return () =>
- {
- return (int currentRetryCount, Exception lastException, out TimeSpan retryInterval) =>
- {
- if (currentRetryCount < retryCount)
- {
- Random r = new Random();
- // Calculate Exponential backoff with +/- 20% tolerance
- int increment = (int)((Math.Pow(2, currentRetryCount) - 1) * r.Next((int)(deltaBackoff.TotalMilliseconds * 0.8), (int)(deltaBackoff.TotalMilliseconds * 1.2)));
- // Enforce backoff boundaries
- int timeToSleepMsec = (int)Math.Min(minBackoff.TotalMilliseconds + increment, maxBackoff.TotalMilliseconds);
- retryInterval = TimeSpan.FromMilliseconds(timeToSleepMsec);
- return true;
- }
- retryInterval = TimeSpan.Zero;
- return false;
- };
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement