-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
87 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use strict'; | ||
|
||
/** | ||
* ExponentialBackoff class handles the logic for increasing or decreasing the delay between | ||
* multiple requests. See https://en.wikipedia.org/wiki/Exponential_backoff | ||
* | ||
* @param {number} minimum_backoff the lowest permissible delay between requests. defaults to 50ms | ||
* @param {number} exponent the factor by which to increse or decrease the backoff. defaults to 2 | ||
* @param {number} starting_backoff the backoff between the first and second request. defaults to * 50ms | ||
*/ | ||
var ExponentialBackoff = function(minimum_backoff, exponent, starting_backoff) { | ||
this.minimum_backoff = minimum_backoff || 50; | ||
this.exponent = exponent || 2.0; | ||
this.starting_backoff = starting_backoff || this.minimum_backoff; | ||
|
||
this.backoff = this.starting_backoff; | ||
}; | ||
|
||
/** | ||
* Get the current backoff as a simple number | ||
*/ | ||
ExponentialBackoff.prototype.getBackoff = function getBackoff() { | ||
return this.backoff; | ||
}; | ||
|
||
|
||
/** | ||
* Increase the backoff for the next request. This method should be called after something happens | ||
* that would indicate a slowdown is needed, such as a failed request. | ||
*/ | ||
ExponentialBackoff.prototype.increaseBackoff = function increaseBackoff() { | ||
this.backoff *= this.exponent; | ||
}; | ||
|
||
/** | ||
* Decrease the backoff for the next request. The backoff will never go below the miniumum backoff | ||
* value. This should be called when things are going smoothly, such as after a successful request. | ||
*/ | ||
ExponentialBackoff.prototype.decreaseBackoff = function decreaseBackoff() { | ||
if (this.backoff <= this.minimum_backoff) { | ||
this.backoff = this.minimum_backoff; | ||
} else { | ||
this.backoff /= this.exponent; | ||
} | ||
}; | ||
|
||
module.exports = ExponentialBackoff; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
var tape = require( 'tape' ); | ||
var ExponentialBackoff = require( '../lib/ExponentialBackoff' ); | ||
|
||
|
||
tape( 'ExponentialBackoff', function(test) { | ||
test.test( 'calling increaseBackoff() makes backoff larger', function(t) { | ||
var eb = new ExponentialBackoff(); | ||
var startBackoff = eb.getBackoff(); | ||
eb.increaseBackoff(); | ||
t.ok(startBackoff < eb.getBackoff(), 'backoff was not higher'); | ||
t.end(); | ||
}); | ||
|
||
test.test( 'calling decreaseBackoff() lowers backoff', function(t) { | ||
var eb = new ExponentialBackoff(); | ||
eb.increaseBackoff(); | ||
var startBackoff = eb.getBackoff(); | ||
eb.decreaseBackoff(); | ||
t.ok(startBackoff > eb.getBackoff(), 'backoff was not lower'); | ||
t.end(); | ||
}); | ||
|
||
test.test( 'calling decreaseBackoff() will not lower backoff below minimum', function(t) { | ||
var eb = new ExponentialBackoff(); | ||
var startBackoff = eb.getBackoff(); | ||
eb.decreaseBackoff(); | ||
t.ok(startBackoff === eb.getBackoff(), 'backoff was not equal'); | ||
t.end(); | ||
}); | ||
|
||
test.end(); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ | |
'use strict'; | ||
|
||
require('./fuzzy-tester'); | ||
require('./ExponentialBackoff'); |