All files / copacetic/lib/backoff-strategies exponential.js

100% Statements 9/9
100% Branches 5/5
100% Functions 2/2
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 361x   1x                             62x 62x 62x   62x 62x               17x       1x  
const precond = require('precond')
 
const BackoffStrategy = require('./strategy')
 
/**
 * @class
 * @classdesc implements exponential backoff
 * @extends BackoffStrategy
 */
class ExponentialBackoffStrategy extends BackoffStrategy {
  /**
   * @param {Number} [constant=2]
   * @param {Number} [multiplier=1]
   * @param {Number} [retries=3]
   * @param {Number} [maxDelay=0]
   */
  constructor ({ constant = 2, multiplier = 1, retries = 3, maxDelay = 0 } = {}) {
    super(retries, maxDelay)
    precond.checkArgument(constant > 0, 'startDelay must be an integer > 0')
    precond.checkArgument(multiplier > 0, 'multiplier must be an integer > 0')
 
    this.constant = constant
    this.multiplier = multiplier
  }
 
  /**
   * @param {Integer} attempt
   * @return {Number} The backoff time
   */
  _intervalFunc (attempt) {
    return Math.pow(this.constant, attempt) * this.multiplier
  }
}
 
module.exports = ExponentialBackoffStrategy