all files / bull/lib/ backoffs.js

75% Statements 15/20
70% Branches 7/10
71.43% Functions 5/7
75% Lines 15/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51                                               443×       442×                    
var _ = require('lodash');
 
var builtinStrategies = {
  fixed: function(delay) {
    return function() {
      return delay;
    };
  },
 
  exponential: function(delay) {
    return function(attemptsMade) {
      return Math.round((Math.pow(2, attemptsMade) - 1) * delay);
    };
  }
};
 
function lookupStrategy(backoff, customStrategies) {
  Iif (backoff.type in customStrategies) {
    return customStrategies[backoff.type];
  } else Eif (backoff.type in builtinStrategies) {
    return builtinStrategies[backoff.type](backoff.delay);
  } else {
    throw new Error(
      'Unknown backoff strategy ' +
        backoff.type +
        '. If a custom backoff strategy is used, specify it when the queue is created.'
    );
  }
}
 
module.exports = {
  normalize: function(backoff) {
    if (_.isFinite(backoff)) {
      return {
        type: 'fixed',
        delay: backoff
      };
    } else Iif (backoff) {
      return backoff;
    }
  },
 
  calculate: function(backoff, attemptsMade, customStrategies) {
    if (backoff) {
      var strategy = lookupStrategy(backoff, customStrategies);
 
      return strategy(attemptsMade);
    }
  }
};