Advertisement
Lauda

Untitled

Dec 11th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. public static RetryPolicy RetryExponential(int retryCount, TimeSpan minBackoff, TimeSpan maxBackoff, TimeSpan deltaBackoff)
  2. {
  3.       // Do any argument Pre-validation here, i.e. enforce max retry count etc.
  4.       return () =>
  5.       {    
  6.             return (int currentRetryCount, Exception lastException, out TimeSpan retryInterval) =>
  7.             {
  8.                  if (currentRetryCount < retryCount)
  9.                  {
  10.                       Random r = new Random();
  11.  
  12.                       // Calculate Exponential backoff with +/- 20% tolerance
  13.                       int increment = (int)((Math.Pow(2, currentRetryCount) - 1) * r.Next((int)(deltaBackoff.TotalMilliseconds * 0.8), (int)(deltaBackoff.TotalMilliseconds * 1.2)));
  14.                      
  15.                       // Enforce backoff boundaries
  16.                       int timeToSleepMsec = (int)Math.Min(minBackoff.TotalMilliseconds + increment, maxBackoff.TotalMilliseconds);
  17.  
  18.                       retryInterval = TimeSpan.FromMilliseconds(timeToSleepMsec);
  19.  
  20.                       return true;
  21.                  }
  22.  
  23.                  retryInterval = TimeSpan.Zero;
  24.                  return false;
  25.             };
  26.       };
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement