1234567891011121314151617181920212223242526272829303132333435363738394041 |
- // 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;
|