backoff-timeout.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. /*
  3. * Copyright 2019 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.BackoffTimeout = void 0;
  20. const INITIAL_BACKOFF_MS = 1000;
  21. const BACKOFF_MULTIPLIER = 1.6;
  22. const MAX_BACKOFF_MS = 120000;
  23. const BACKOFF_JITTER = 0.2;
  24. /**
  25. * Get a number uniformly at random in the range [min, max)
  26. * @param min
  27. * @param max
  28. */
  29. function uniformRandom(min, max) {
  30. return Math.random() * (max - min) + min;
  31. }
  32. class BackoffTimeout {
  33. constructor(callback, options) {
  34. this.callback = callback;
  35. /**
  36. * The delay time at the start, and after each reset.
  37. */
  38. this.initialDelay = INITIAL_BACKOFF_MS;
  39. /**
  40. * The exponential backoff multiplier.
  41. */
  42. this.multiplier = BACKOFF_MULTIPLIER;
  43. /**
  44. * The maximum delay time
  45. */
  46. this.maxDelay = MAX_BACKOFF_MS;
  47. /**
  48. * The maximum fraction by which the delay time can randomly vary after
  49. * applying the multiplier.
  50. */
  51. this.jitter = BACKOFF_JITTER;
  52. /**
  53. * Indicates whether the timer is currently running.
  54. */
  55. this.running = false;
  56. /**
  57. * Indicates whether the timer should keep the Node process running if no
  58. * other async operation is doing so.
  59. */
  60. this.hasRef = true;
  61. /**
  62. * The time that the currently running timer was started. Only valid if
  63. * running is true.
  64. */
  65. this.startTime = new Date();
  66. if (options) {
  67. if (options.initialDelay) {
  68. this.initialDelay = options.initialDelay;
  69. }
  70. if (options.multiplier) {
  71. this.multiplier = options.multiplier;
  72. }
  73. if (options.jitter) {
  74. this.jitter = options.jitter;
  75. }
  76. if (options.maxDelay) {
  77. this.maxDelay = options.maxDelay;
  78. }
  79. }
  80. this.nextDelay = this.initialDelay;
  81. this.timerId = setTimeout(() => { }, 0);
  82. clearTimeout(this.timerId);
  83. }
  84. runTimer(delay) {
  85. var _a, _b;
  86. clearTimeout(this.timerId);
  87. this.timerId = setTimeout(() => {
  88. this.callback();
  89. this.running = false;
  90. }, delay);
  91. if (!this.hasRef) {
  92. (_b = (_a = this.timerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
  93. }
  94. }
  95. /**
  96. * Call the callback after the current amount of delay time
  97. */
  98. runOnce() {
  99. this.running = true;
  100. this.startTime = new Date();
  101. this.runTimer(this.nextDelay);
  102. const nextBackoff = Math.min(this.nextDelay * this.multiplier, this.maxDelay);
  103. const jitterMagnitude = nextBackoff * this.jitter;
  104. this.nextDelay =
  105. nextBackoff + uniformRandom(-jitterMagnitude, jitterMagnitude);
  106. }
  107. /**
  108. * Stop the timer. The callback will not be called until `runOnce` is called
  109. * again.
  110. */
  111. stop() {
  112. clearTimeout(this.timerId);
  113. this.running = false;
  114. }
  115. /**
  116. * Reset the delay time to its initial value. If the timer is still running,
  117. * retroactively apply that reset to the current timer.
  118. */
  119. reset() {
  120. this.nextDelay = this.initialDelay;
  121. if (this.running) {
  122. const now = new Date();
  123. const newEndTime = this.startTime;
  124. newEndTime.setMilliseconds(newEndTime.getMilliseconds() + this.nextDelay);
  125. clearTimeout(this.timerId);
  126. if (now < newEndTime) {
  127. this.runTimer(newEndTime.getTime() - now.getTime());
  128. }
  129. else {
  130. this.running = false;
  131. }
  132. }
  133. }
  134. /**
  135. * Check whether the timer is currently running.
  136. */
  137. isRunning() {
  138. return this.running;
  139. }
  140. /**
  141. * Set that while the timer is running, it should keep the Node process
  142. * running.
  143. */
  144. ref() {
  145. var _a, _b;
  146. this.hasRef = true;
  147. (_b = (_a = this.timerId).ref) === null || _b === void 0 ? void 0 : _b.call(_a);
  148. }
  149. /**
  150. * Set that while the timer is running, it should not keep the Node process
  151. * running.
  152. */
  153. unref() {
  154. var _a, _b;
  155. this.hasRef = false;
  156. (_b = (_a = this.timerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
  157. }
  158. }
  159. exports.BackoffTimeout = BackoffTimeout;
  160. //# sourceMappingURL=backoff-timeout.js.map