Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exponential.js 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright (c) 2012 Mathieu Turcotte
  2. // Licensed under the MIT license.
  3. var util = require('util');
  4. var precond = require('precond');
  5. var BackoffStrategy = require('./strategy');
  6. // Exponential backoff strategy.
  7. function ExponentialBackoffStrategy(options) {
  8. BackoffStrategy.call(this, options);
  9. this.backoffDelay_ = 0;
  10. this.nextBackoffDelay_ = this.getInitialDelay();
  11. this.factor_ = ExponentialBackoffStrategy.DEFAULT_FACTOR;
  12. if (options && options.factor !== undefined) {
  13. precond.checkArgument(options.factor > 1,
  14. 'Exponential factor should be greater than 1 but got %s.',
  15. options.factor);
  16. this.factor_ = options.factor;
  17. }
  18. }
  19. util.inherits(ExponentialBackoffStrategy, BackoffStrategy);
  20. // Default multiplication factor used to compute the next backoff delay from
  21. // the current one. The value can be overridden by passing a custom factor as
  22. // part of the options.
  23. ExponentialBackoffStrategy.DEFAULT_FACTOR = 2;
  24. ExponentialBackoffStrategy.prototype.next_ = function() {
  25. this.backoffDelay_ = Math.min(this.nextBackoffDelay_, this.getMaxDelay());
  26. this.nextBackoffDelay_ = this.backoffDelay_ * this.factor_;
  27. return this.backoffDelay_;
  28. };
  29. ExponentialBackoffStrategy.prototype.reset_ = function() {
  30. this.backoffDelay_ = 0;
  31. this.nextBackoffDelay_ = this.getInitialDelay();
  32. };
  33. module.exports = ExponentialBackoffStrategy;