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.

strategy.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright (c) 2012 Mathieu Turcotte
  2. // Licensed under the MIT license.
  3. var events = require('events');
  4. var util = require('util');
  5. function isDef(value) {
  6. return value !== undefined && value !== null;
  7. }
  8. // Abstract class defining the skeleton for the backoff strategies. Accepts an
  9. // object holding the options for the backoff strategy:
  10. //
  11. // * `randomisationFactor`: The randomisation factor which must be between 0
  12. // and 1 where 1 equates to a randomization factor of 100% and 0 to no
  13. // randomization.
  14. // * `initialDelay`: The backoff initial delay in milliseconds.
  15. // * `maxDelay`: The backoff maximal delay in milliseconds.
  16. function BackoffStrategy(options) {
  17. options = options || {};
  18. if (isDef(options.initialDelay) && options.initialDelay < 1) {
  19. throw new Error('The initial timeout must be greater than 0.');
  20. } else if (isDef(options.maxDelay) && options.maxDelay < 1) {
  21. throw new Error('The maximal timeout must be greater than 0.');
  22. }
  23. this.initialDelay_ = options.initialDelay || 100;
  24. this.maxDelay_ = options.maxDelay || 10000;
  25. if (this.maxDelay_ <= this.initialDelay_) {
  26. throw new Error('The maximal backoff delay must be ' +
  27. 'greater than the initial backoff delay.');
  28. }
  29. if (isDef(options.randomisationFactor) &&
  30. (options.randomisationFactor < 0 || options.randomisationFactor > 1)) {
  31. throw new Error('The randomisation factor must be between 0 and 1.');
  32. }
  33. this.randomisationFactor_ = options.randomisationFactor || 0;
  34. }
  35. // Gets the maximal backoff delay.
  36. BackoffStrategy.prototype.getMaxDelay = function() {
  37. return this.maxDelay_;
  38. };
  39. // Gets the initial backoff delay.
  40. BackoffStrategy.prototype.getInitialDelay = function() {
  41. return this.initialDelay_;
  42. };
  43. // Template method that computes and returns the next backoff delay in
  44. // milliseconds.
  45. BackoffStrategy.prototype.next = function() {
  46. var backoffDelay = this.next_();
  47. var randomisationMultiple = 1 + Math.random() * this.randomisationFactor_;
  48. var randomizedDelay = Math.round(backoffDelay * randomisationMultiple);
  49. return randomizedDelay;
  50. };
  51. // Computes and returns the next backoff delay. Intended to be overridden by
  52. // subclasses.
  53. BackoffStrategy.prototype.next_ = function() {
  54. throw new Error('BackoffStrategy.next_() unimplemented.');
  55. };
  56. // Template method that resets the backoff delay to its initial value.
  57. BackoffStrategy.prototype.reset = function() {
  58. this.reset_();
  59. };
  60. // Resets the backoff delay to its initial value. Intended to be overridden by
  61. // subclasses.
  62. BackoffStrategy.prototype.reset_ = function() {
  63. throw new Error('BackoffStrategy.reset_() unimplemented.');
  64. };
  65. module.exports = BackoffStrategy;