Simulator first commit
This commit is contained in:
41
node_modules/backoff/lib/strategy/exponential.js
generated
vendored
Normal file
41
node_modules/backoff/lib/strategy/exponential.js
generated
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright (c) 2012 Mathieu Turcotte
|
||||
// Licensed under the MIT license.
|
||||
|
||||
var util = require('util');
|
||||
var precond = require('precond');
|
||||
|
||||
var BackoffStrategy = require('./strategy');
|
||||
|
||||
// Exponential backoff strategy.
|
||||
function ExponentialBackoffStrategy(options) {
|
||||
BackoffStrategy.call(this, options);
|
||||
this.backoffDelay_ = 0;
|
||||
this.nextBackoffDelay_ = this.getInitialDelay();
|
||||
this.factor_ = ExponentialBackoffStrategy.DEFAULT_FACTOR;
|
||||
|
||||
if (options && options.factor !== undefined) {
|
||||
precond.checkArgument(options.factor > 1,
|
||||
'Exponential factor should be greater than 1 but got %s.',
|
||||
options.factor);
|
||||
this.factor_ = options.factor;
|
||||
}
|
||||
}
|
||||
util.inherits(ExponentialBackoffStrategy, BackoffStrategy);
|
||||
|
||||
// Default multiplication factor used to compute the next backoff delay from
|
||||
// the current one. The value can be overridden by passing a custom factor as
|
||||
// part of the options.
|
||||
ExponentialBackoffStrategy.DEFAULT_FACTOR = 2;
|
||||
|
||||
ExponentialBackoffStrategy.prototype.next_ = function() {
|
||||
this.backoffDelay_ = Math.min(this.nextBackoffDelay_, this.getMaxDelay());
|
||||
this.nextBackoffDelay_ = this.backoffDelay_ * this.factor_;
|
||||
return this.backoffDelay_;
|
||||
};
|
||||
|
||||
ExponentialBackoffStrategy.prototype.reset_ = function() {
|
||||
this.backoffDelay_ = 0;
|
||||
this.nextBackoffDelay_ = this.getInitialDelay();
|
||||
};
|
||||
|
||||
module.exports = ExponentialBackoffStrategy;
|
28
node_modules/backoff/lib/strategy/fibonacci.js
generated
vendored
Normal file
28
node_modules/backoff/lib/strategy/fibonacci.js
generated
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
// Copyright (c) 2012 Mathieu Turcotte
|
||||
// Licensed under the MIT license.
|
||||
|
||||
var util = require('util');
|
||||
|
||||
var BackoffStrategy = require('./strategy');
|
||||
|
||||
// Fibonacci backoff strategy.
|
||||
function FibonacciBackoffStrategy(options) {
|
||||
BackoffStrategy.call(this, options);
|
||||
this.backoffDelay_ = 0;
|
||||
this.nextBackoffDelay_ = this.getInitialDelay();
|
||||
}
|
||||
util.inherits(FibonacciBackoffStrategy, BackoffStrategy);
|
||||
|
||||
FibonacciBackoffStrategy.prototype.next_ = function() {
|
||||
var backoffDelay = Math.min(this.nextBackoffDelay_, this.getMaxDelay());
|
||||
this.nextBackoffDelay_ += this.backoffDelay_;
|
||||
this.backoffDelay_ = backoffDelay;
|
||||
return backoffDelay;
|
||||
};
|
||||
|
||||
FibonacciBackoffStrategy.prototype.reset_ = function() {
|
||||
this.nextBackoffDelay_ = this.getInitialDelay();
|
||||
this.backoffDelay_ = 0;
|
||||
};
|
||||
|
||||
module.exports = FibonacciBackoffStrategy;
|
80
node_modules/backoff/lib/strategy/strategy.js
generated
vendored
Normal file
80
node_modules/backoff/lib/strategy/strategy.js
generated
vendored
Normal file
@ -0,0 +1,80 @@
|
||||
// Copyright (c) 2012 Mathieu Turcotte
|
||||
// Licensed under the MIT license.
|
||||
|
||||
var events = require('events');
|
||||
var util = require('util');
|
||||
|
||||
function isDef(value) {
|
||||
return value !== undefined && value !== null;
|
||||
}
|
||||
|
||||
// Abstract class defining the skeleton for the backoff strategies. Accepts an
|
||||
// object holding the options for the backoff strategy:
|
||||
//
|
||||
// * `randomisationFactor`: The randomisation factor which must be between 0
|
||||
// and 1 where 1 equates to a randomization factor of 100% and 0 to no
|
||||
// randomization.
|
||||
// * `initialDelay`: The backoff initial delay in milliseconds.
|
||||
// * `maxDelay`: The backoff maximal delay in milliseconds.
|
||||
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;
|
||||
}
|
||||
|
||||
// Gets the maximal backoff delay.
|
||||
BackoffStrategy.prototype.getMaxDelay = function() {
|
||||
return this.maxDelay_;
|
||||
};
|
||||
|
||||
// Gets the initial backoff delay.
|
||||
BackoffStrategy.prototype.getInitialDelay = function() {
|
||||
return this.initialDelay_;
|
||||
};
|
||||
|
||||
// Template method that computes and returns the next backoff delay in
|
||||
// milliseconds.
|
||||
BackoffStrategy.prototype.next = function() {
|
||||
var backoffDelay = this.next_();
|
||||
var randomisationMultiple = 1 + Math.random() * this.randomisationFactor_;
|
||||
var randomizedDelay = Math.round(backoffDelay * randomisationMultiple);
|
||||
return randomizedDelay;
|
||||
};
|
||||
|
||||
// Computes and returns the next backoff delay. Intended to be overridden by
|
||||
// subclasses.
|
||||
BackoffStrategy.prototype.next_ = function() {
|
||||
throw new Error('BackoffStrategy.next_() unimplemented.');
|
||||
};
|
||||
|
||||
// Template method that resets the backoff delay to its initial value.
|
||||
BackoffStrategy.prototype.reset = function() {
|
||||
this.reset_();
|
||||
};
|
||||
|
||||
// Resets the backoff delay to its initial value. Intended to be overridden by
|
||||
// subclasses.
|
||||
BackoffStrategy.prototype.reset_ = function() {
|
||||
throw new Error('BackoffStrategy.reset_() unimplemented.');
|
||||
};
|
||||
|
||||
module.exports = BackoffStrategy;
|
Reference in New Issue
Block a user