backoff-timeout.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. /**
  67. * The approximate time that the currently running timer will end. Only valid
  68. * if running is true.
  69. */
  70. this.endTime = new Date();
  71. if (options) {
  72. if (options.initialDelay) {
  73. this.initialDelay = options.initialDelay;
  74. }
  75. if (options.multiplier) {
  76. this.multiplier = options.multiplier;
  77. }
  78. if (options.jitter) {
  79. this.jitter = options.jitter;
  80. }
  81. if (options.maxDelay) {
  82. this.maxDelay = options.maxDelay;
  83. }
  84. }
  85. this.nextDelay = this.initialDelay;
  86. this.timerId = setTimeout(() => { }, 0);
  87. clearTimeout(this.timerId);
  88. }
  89. runTimer(delay) {
  90. var _a, _b;
  91. this.endTime = this.startTime;
  92. this.endTime.setMilliseconds(this.endTime.getMilliseconds() + this.nextDelay);
  93. clearTimeout(this.timerId);
  94. this.timerId = setTimeout(() => {
  95. this.callback();
  96. this.running = false;
  97. }, delay);
  98. if (!this.hasRef) {
  99. (_b = (_a = this.timerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
  100. }
  101. }
  102. /**
  103. * Call the callback after the current amount of delay time
  104. */
  105. runOnce() {
  106. this.running = true;
  107. this.startTime = new Date();
  108. this.runTimer(this.nextDelay);
  109. const nextBackoff = Math.min(this.nextDelay * this.multiplier, this.maxDelay);
  110. const jitterMagnitude = nextBackoff * this.jitter;
  111. this.nextDelay =
  112. nextBackoff + uniformRandom(-jitterMagnitude, jitterMagnitude);
  113. }
  114. /**
  115. * Stop the timer. The callback will not be called until `runOnce` is called
  116. * again.
  117. */
  118. stop() {
  119. clearTimeout(this.timerId);
  120. this.running = false;
  121. }
  122. /**
  123. * Reset the delay time to its initial value. If the timer is still running,
  124. * retroactively apply that reset to the current timer.
  125. */
  126. reset() {
  127. this.nextDelay = this.initialDelay;
  128. if (this.running) {
  129. const now = new Date();
  130. const newEndTime = this.startTime;
  131. newEndTime.setMilliseconds(newEndTime.getMilliseconds() + this.nextDelay);
  132. clearTimeout(this.timerId);
  133. if (now < newEndTime) {
  134. this.runTimer(newEndTime.getTime() - now.getTime());
  135. }
  136. else {
  137. this.running = false;
  138. }
  139. }
  140. }
  141. /**
  142. * Check whether the timer is currently running.
  143. */
  144. isRunning() {
  145. return this.running;
  146. }
  147. /**
  148. * Set that while the timer is running, it should keep the Node process
  149. * running.
  150. */
  151. ref() {
  152. var _a, _b;
  153. this.hasRef = true;
  154. (_b = (_a = this.timerId).ref) === null || _b === void 0 ? void 0 : _b.call(_a);
  155. }
  156. /**
  157. * Set that while the timer is running, it should not keep the Node process
  158. * running.
  159. */
  160. unref() {
  161. var _a, _b;
  162. this.hasRef = false;
  163. (_b = (_a = this.timerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
  164. }
  165. /**
  166. * Get the approximate timestamp of when the timer will fire. Only valid if
  167. * this.isRunning() is true.
  168. */
  169. getEndTime() {
  170. return this.endTime;
  171. }
  172. }
  173. exports.BackoffTimeout = BackoffTimeout;
  174. //# sourceMappingURL=backoff-timeout.js.map