Code coverage report for lib/backoff.js

Statements: 100% (13 / 13)      Branches: 100% (10 / 10)      Functions: 100% (2 / 2)      Lines: 100% (13 / 13)      Ignored: none     

All files » lib/ » backoff.js
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                            1   9 9 9 9 9                       9 18   18 2     16     16   16      
'use strict';
 
/**
 *  Given options, returns a function can that be used to eventually call a
 *  function with an exponential backoff.
 *
 *  @param {object} opts - options to configure the backoff function
 *  @param {number} opts.delay - the initial delay
 *  @param {number} opts.maxDelay - the maximum delay
 *  @param {number} opts.maxRetries - the maximum retries
 *
 *  @returns {function} func - func that accepts a function to be called
 *                             eventually
 */
module.exports.create = function(opts) {
 
  opts = opts || {};
  var delay = opts.delay || 10;
  var maxDelay = opts.maxDelay || 10000;
  var maxRetries = opts.maxRetries || 10;
  var retries = 0;
 
  /**
   *  Calls the provided function after delay and increments retries and
   *  returns true.
   *
   *  If max retries has been reached, returns false and does
   *  not schedule the provided function to be called after delay.
   *
   *  @param {function} fn - the function to call after delay
   *  @returns {boolean} scheduled - true if fn was scheduled, false otherwise
   */
  return function (fn) {
    retries += 1;
 
    if (retries > maxRetries) {
      return false;
    }
 
    setTimeout(fn, delay);
 
    // calculate next delay
    delay = Math.min(delay * 2, maxDelay);
 
    return true;
  };
};