Name: js-handler/node_modules/restify/node_modules/backoff/lib/strategy/strategy.js 
1:
/*
2:
 * Copyright (c) 2012 Mathieu Turcotte
3:
 * Licensed under the MIT license.
4:
 */
5:
 
6:
var events = require('events');
7:
var util = require('util');
8:
 
9:
function isDef(value) {
10:
    return value !== undefined && value !== null;
11:
}
12:
 
13:
/**
14:
 * Abstract class defining the skeleton for all backoff strategies.
15:
 * @param options Backoff strategy options.
16:
 * @param options.randomisationFactor The randomisation factor, must be between
17:
 * 0 and 1.
18:
 * @param options.initialDelay The backoff initial delay, in milliseconds.
19:
 * @param options.maxDelay The backoff maximal delay, in milliseconds.
20:
 * @constructor
21:
 */
22:
function BackoffStrategy(options) {
23:
    options = options || {};
24:
 
25:
    if (isDef(options.initialDelay) && options.initialDelay < 1) {
26:
        throw new Error('The initial timeout must be greater than 0.');
27:
    } else if (isDef(options.maxDelay) && options.maxDelay < 1) {
28:
        throw new Error('The maximal timeout must be greater than 0.');
29:
    }
30:
 
31:
    this.initialDelay_ = options.initialDelay || 100;
32:
    this.maxDelay_ = options.maxDelay || 10000;
33:
 
34:
    if (this.maxDelay_ <= this.initialDelay_) {
35:
        throw new Error('The maximal backoff delay must be ' +
36:
                        'greater than the initial backoff delay.');
37:
    }
38:
 
39:
    if (isDef(options.randomisationFactor) &&
40:
        (options.randomisationFactor < 0 || options.randomisationFactor > 1)) {
41:
        throw new Error('The randomisation factor must be between 0 and 1.');
42:
    }
43:
 
44:
    this.randomisationFactor_ = options.randomisationFactor || 0;
45:
}
46:
 
47:
/**
48:
 * Retrieves the maximal backoff delay.
49:
 * @return The maximal backoff delay, in milliseconds.
50:
 */
51:
BackoffStrategy.prototype.getMaxDelay = function() {
52:
    return this.maxDelay_;
53:
};
54:
 
55:
/**
56:
 * Retrieves the initial backoff delay.
57:
 * @return The initial backoff delay, in milliseconds.
58:
 */
59:
BackoffStrategy.prototype.getInitialDelay = function() {
60:
    return this.initialDelay_;
61:
};
62:
 
63:
/**
64:
 * Template method that computes the next backoff delay.
65:
 * @return The backoff delay, in milliseconds.
66:
 */
67:
BackoffStrategy.prototype.next = function() {
68:
    var backoffDelay = this.next_();
69:
    var randomisationMultiple = 1 + Math.random() * this.randomisationFactor_;
70:
    var randomizedDelay = Math.round(backoffDelay * randomisationMultiple);
71:
    return randomizedDelay;
72:
};
73:
 
74:
/**
75:
 * Computes the next backoff delay.
76:
 * @return The backoff delay, in milliseconds.
77:
 * @protected
78:
 */
79:
BackoffStrategy.prototype.next_ = function() {
80:
    throw new Error('BackoffStrategy.next_() unimplemented.');
81:
};
82:
 
83:
/**
84:
 * Template method that resets the backoff delay to its initial value.
85:
 */
86:
BackoffStrategy.prototype.reset = function() {
87:
    this.reset_();
88:
};
89:
 
90:
/**
91:
 * Resets the backoff delay to its initial value.
92:
 * @protected
93:
 */
94:
BackoffStrategy.prototype.reset_ = function() {
95:
    throw new Error('BackoffStrategy.reset_() unimplemented.');
96:
};
97:
 
98:
module.exports = BackoffStrategy;