ExponentialBackoff.cs 596 B

123456789101112131415161718
  1. using System;
  2. namespace SingularityGroup.HotReload.Editor {
  3. static class ExponentialBackoff {
  4. public static TimeSpan GetTimeout(int attempt, int minBackoff = 250, int maxBackoff = 60000, int deltaBackoff = 400) {
  5. attempt = Math.Min(25, attempt); // safety to avoid overflow below
  6. var delta = (uint)(
  7. (Math.Pow(2.0, attempt) - 1.0)
  8. * deltaBackoff
  9. );
  10. var interval = Math.Min(checked(minBackoff + delta), maxBackoff);
  11. return TimeSpan.FromMilliseconds(interval);
  12. }
  13. }
  14. }