1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- var events = require('events');
- var util = require('util');
- function isDef(value) {
- return value !== undefined && value !== null;
- }
- function BackoffStrategy(options) {
- options = options || {};
- if (isDef(options.initialDelay) && options.initialDelay < 1) {
- throw new Error('The initial timeout must be greater than 0.');
- } else if (isDef(options.maxDelay) && options.maxDelay < 1) {
- throw new Error('The maximal timeout must be greater than 0.');
- }
- this.initialDelay_ = options.initialDelay || 100;
- this.maxDelay_ = options.maxDelay || 10000;
- if (this.maxDelay_ <= this.initialDelay_) {
- throw new Error('The maximal backoff delay must be ' +
- 'greater than the initial backoff delay.');
- }
- if (isDef(options.randomisationFactor) &&
- (options.randomisationFactor < 0 || options.randomisationFactor > 1)) {
- throw new Error('The randomisation factor must be between 0 and 1.');
- }
- this.randomisationFactor_ = options.randomisationFactor || 0;
- }
- BackoffStrategy.prototype.getMaxDelay = function() {
- return this.maxDelay_;
- };
- BackoffStrategy.prototype.getInitialDelay = function() {
- return this.initialDelay_;
- };
- BackoffStrategy.prototype.next = function() {
- var backoffDelay = this.next_();
- var randomisationMultiple = 1 + Math.random() * this.randomisationFactor_;
- var randomizedDelay = Math.round(backoffDelay * randomisationMultiple);
- return randomizedDelay;
- };
- BackoffStrategy.prototype.next_ = function() {
- throw new Error('BackoffStrategy.next_() unimplemented.');
- };
- BackoffStrategy.prototype.reset = function() {
- this.reset_();
- };
- BackoffStrategy.prototype.reset_ = function() {
- throw new Error('BackoffStrategy.reset_() unimplemented.');
- };
- module.exports = BackoffStrategy;
|