|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const INITIAL_BACKOFF_MS = 1000; |
|
const BACKOFF_MULTIPLIER = 1.6; |
|
const MAX_BACKOFF_MS = 120000; |
|
const BACKOFF_JITTER = 0.2; |
|
|
|
|
|
|
|
|
|
|
|
|
|
function uniformRandom(min: number, max: number) { |
|
return Math.random() * (max - min) + min; |
|
} |
|
|
|
export interface BackoffOptions { |
|
initialDelay?: number; |
|
multiplier?: number; |
|
jitter?: number; |
|
maxDelay?: number; |
|
} |
|
|
|
export class BackoffTimeout { |
|
|
|
|
|
|
|
private readonly initialDelay: number = INITIAL_BACKOFF_MS; |
|
|
|
|
|
|
|
private readonly multiplier: number = BACKOFF_MULTIPLIER; |
|
|
|
|
|
|
|
private readonly maxDelay: number = MAX_BACKOFF_MS; |
|
|
|
|
|
|
|
|
|
private readonly jitter: number = BACKOFF_JITTER; |
|
|
|
|
|
|
|
private nextDelay: number; |
|
|
|
|
|
|
|
|
|
|
|
private timerId: NodeJS.Timeout; |
|
|
|
|
|
|
|
private running = false; |
|
|
|
|
|
|
|
|
|
private hasRef = true; |
|
|
|
|
|
|
|
|
|
private startTime: Date = new Date(); |
|
|
|
|
|
|
|
|
|
private endTime: Date = new Date(); |
|
|
|
constructor(private callback: () => void, options?: BackoffOptions) { |
|
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.nextDelay = this.initialDelay; |
|
this.timerId = setTimeout(() => {}, 0); |
|
clearTimeout(this.timerId); |
|
} |
|
|
|
private runTimer(delay: number) { |
|
this.endTime = this.startTime; |
|
this.endTime.setMilliseconds(this.endTime.getMilliseconds() + this.nextDelay); |
|
clearTimeout(this.timerId); |
|
this.timerId = setTimeout(() => { |
|
this.callback(); |
|
this.running = false; |
|
}, delay); |
|
if (!this.hasRef) { |
|
this.timerId.unref?.(); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
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() { |
|
clearTimeout(this.timerId); |
|
this.running = false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
reset() { |
|
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() { |
|
this.hasRef = true; |
|
this.timerId.ref?.(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
unref() { |
|
this.hasRef = false; |
|
this.timerId.unref?.(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
getEndTime() { |
|
return this.endTime; |
|
} |
|
} |
|
|