Created
July 6, 2017 03:00
-
-
Save railsstudent/5873f7b5b9c2a3653773ea07e231349c to your computer and use it in GitHub Desktop.
ExponentialBackoff based on https://godoc.org/github.com/cenkalti/backoff#pkg-examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reference: https://godoc.org/github.com/cenkalti/backoff#pkg-examples | |
// expect the following object to pass to constructor | |
// { | |
// retryInterval, | |
// randomizationFactor, | |
// multiplier, | |
// maxInterval, | |
// maxElapsedTime | |
// } | |
function ExponentialBackoff(data) { | |
this.originalRetryInterval = data.retryInterval || 0.5; | |
this.retryInterval = data.retryInterval || 500; // default value is 500 milliseconds. In milliseconds | |
this.randomizationFactor = data.randomizationFactor || 0.5; | |
this.multiplier = data.multiplier || 1.5; | |
if (this.multiplier <= 1) { | |
throw new Error("multiplier must be greater than 1."); | |
} | |
this.maxInterval = data.maxInterval || 60 * 1000; // 60 seconds /60000 milliseconds. In milliseconds | |
this.maxElapsedTime = data.maxElapsedTime || 15 * 60 * 1000; // 15 minutes/ 900000 milliseconds. In milliseconds | |
this.startTime = new Date().getTime(); | |
} | |
/* | |
Request # RetryInterval (seconds) Randomized Interval (seconds) | |
1 0.5 [0.25, 0.75] | |
2 0.75 [0.375, 1.125] | |
3 1.125 [0.562, 1.687] | |
4 1.687 [0.8435, 2.53] | |
5 2.53 [1.265, 3.795] | |
6 3.795 [1.897, 5.692] | |
7 5.692 [2.846, 8.538] | |
8 8.538 [4.269, 12.807] | |
9 12.807 [6.403, 19.210] | |
10 19.210 backoff.Stop | |
*/ | |
ExponentialBackoff.prototype.nextBackoff = function() { | |
// If the time elapsed since an ExponentialBackOff instance is created goes past the MaxElapsedTime, then the method NextBackOff() starts returning backoff.Stop. | |
if (this.getElapsedTime() > this.maxElapsedTime) { | |
return this.STOP; | |
} | |
this.retryInterval = this.retryInterval * this.multiplier; | |
if (this.retryInterval > this.maxInterval) { | |
this.retryInterval = this.maxInterval; | |
} | |
// Randomized interval = RetryInterval +/- (RandomizationFactor * RetryInterval) | |
var delta = this.randomizationFactor * this.retryInterval; | |
var minInterval = this.retryInterval - delta; | |
var maxInterval = this.retryInterval + delta; | |
var randValue = Math.floor(Math.random() * (maxInterval - minInterval) + minInterval); | |
console.log({backoffDelay: randValue}); | |
return randValue; | |
} | |
ExponentialBackoff.prototype.reset = function() { | |
// Reset the interval back to the initial retry interval and restarts the timer. | |
this.retryInterval = this.originalRetryInterval; | |
this.startTime = new Date().getTime(); | |
} | |
ExponentialBackoff.prototype.getElapsedTime = function() { | |
var currentTime = new Date().getTime(); | |
var elapsedTime = currentTime - this.startTime; | |
console.log( { elapsedTime: elapsedTime }); | |
return elapsedTime; | |
} | |
ExponentialBackoff.prototype.STOP = -1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment