Source: util/exponential-backoff.ts

  1. /**
  2. * @fileoverview Calculate exponential backoff time
  3. */
  4. // ------------------------------------------------------------------------------
  5. // Private
  6. // ------------------------------------------------------------------------------
  7. // Retry intervals are between 50% and 150% of the exponentially increasing base amount
  8. const RETRY_RANDOMIZATION_FACTOR = 0.5;
  9. /**
  10. * Calculate the exponential backoff time with randomized jitter
  11. * @param {int} numRetries Which retry number this one will be
  12. * @param {int} baseInterval The base retry interval set in config
  13. * @returns {int} The number of milliseconds after which to retry
  14. */
  15. export = function getRetryTimeout(numRetries: number, baseInterval: number) {
  16. var minRandomization = 1 - RETRY_RANDOMIZATION_FACTOR;
  17. var maxRandomization = 1 + RETRY_RANDOMIZATION_FACTOR;
  18. var randomization =
  19. Math.random() * (maxRandomization - minRandomization) + minRandomization;
  20. var exponential = Math.pow(2, numRetries - 1);
  21. return Math.ceil(exponential * baseInterval * randomization);
  22. };