|
"use strict"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.BackoffTimeout = void 0; |
|
const constants_1 = require("./constants"); |
|
const logging = require("./logging"); |
|
const TRACER_NAME = 'backoff'; |
|
const INITIAL_BACKOFF_MS = 1000; |
|
const BACKOFF_MULTIPLIER = 1.6; |
|
const MAX_BACKOFF_MS = 120000; |
|
const BACKOFF_JITTER = 0.2; |
|
|
|
|
|
|
|
|
|
|
|
function uniformRandom(min, max) { |
|
return Math.random() * (max - min) + min; |
|
} |
|
class BackoffTimeout { |
|
constructor(callback, options) { |
|
this.callback = callback; |
|
|
|
|
|
|
|
this.initialDelay = INITIAL_BACKOFF_MS; |
|
|
|
|
|
|
|
this.multiplier = BACKOFF_MULTIPLIER; |
|
|
|
|
|
|
|
this.maxDelay = MAX_BACKOFF_MS; |
|
|
|
|
|
|
|
|
|
this.jitter = BACKOFF_JITTER; |
|
|
|
|
|
|
|
this.running = false; |
|
|
|
|
|
|
|
|
|
this.hasRef = true; |
|
|
|
|
|
|
|
|
|
this.startTime = new Date(); |
|
|
|
|
|
|
|
|
|
this.endTime = new Date(); |
|
this.id = BackoffTimeout.getNextId(); |
|
if (options) { |
|
if (options.initialDelay) { |
|
this.initialDelay = options.initialDelay; |
|
} |
|
if (options.multiplier) { |
|
this.multiplier = options.multiplier; |
|
} |
|
if (options.jitter) { |
|
this.jitter = options.jitter; |
|
} |
|
if (options.maxDelay) { |
|
this.maxDelay = options.maxDelay; |
|
} |
|
} |
|
this.trace('constructed initialDelay=' + this.initialDelay + ' multiplier=' + this.multiplier + ' jitter=' + this.jitter + ' maxDelay=' + this.maxDelay); |
|
this.nextDelay = this.initialDelay; |
|
this.timerId = setTimeout(() => { }, 0); |
|
clearTimeout(this.timerId); |
|
} |
|
static getNextId() { |
|
return this.nextId++; |
|
} |
|
trace(text) { |
|
logging.trace(constants_1.LogVerbosity.DEBUG, TRACER_NAME, '{' + this.id + '} ' + text); |
|
} |
|
runTimer(delay) { |
|
var _a, _b; |
|
this.trace('runTimer(delay=' + delay + ')'); |
|
this.endTime = this.startTime; |
|
this.endTime.setMilliseconds(this.endTime.getMilliseconds() + delay); |
|
clearTimeout(this.timerId); |
|
this.timerId = setTimeout(() => { |
|
this.trace('timer fired'); |
|
this.running = false; |
|
this.callback(); |
|
}, delay); |
|
if (!this.hasRef) { |
|
(_b = (_a = this.timerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a); |
|
} |
|
} |
|
|
|
|
|
|
|
runOnce() { |
|
this.trace('runOnce()'); |
|
this.running = true; |
|
this.startTime = new Date(); |
|
this.runTimer(this.nextDelay); |
|
const nextBackoff = Math.min(this.nextDelay * this.multiplier, this.maxDelay); |
|
const jitterMagnitude = nextBackoff * this.jitter; |
|
this.nextDelay = |
|
nextBackoff + uniformRandom(-jitterMagnitude, jitterMagnitude); |
|
} |
|
|
|
|
|
|
|
|
|
stop() { |
|
this.trace('stop()'); |
|
clearTimeout(this.timerId); |
|
this.running = false; |
|
} |
|
|
|
|
|
|
|
|
|
reset() { |
|
this.trace('reset() running=' + this.running); |
|
this.nextDelay = this.initialDelay; |
|
if (this.running) { |
|
const now = new Date(); |
|
const newEndTime = this.startTime; |
|
newEndTime.setMilliseconds(newEndTime.getMilliseconds() + this.nextDelay); |
|
clearTimeout(this.timerId); |
|
if (now < newEndTime) { |
|
this.runTimer(newEndTime.getTime() - now.getTime()); |
|
} |
|
else { |
|
this.running = false; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
isRunning() { |
|
return this.running; |
|
} |
|
|
|
|
|
|
|
|
|
ref() { |
|
var _a, _b; |
|
this.hasRef = true; |
|
(_b = (_a = this.timerId).ref) === null || _b === void 0 ? void 0 : _b.call(_a); |
|
} |
|
|
|
|
|
|
|
|
|
unref() { |
|
var _a, _b; |
|
this.hasRef = false; |
|
(_b = (_a = this.timerId).unref) === null || _b === void 0 ? void 0 : _b.call(_a); |
|
} |
|
|
|
|
|
|
|
|
|
getEndTime() { |
|
return this.endTime; |
|
} |
|
} |
|
exports.BackoffTimeout = BackoffTimeout; |
|
BackoffTimeout.nextId = 0; |
|
|