index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. var util = require('util');
  3. var format = util.format;
  4. function TimeoutError(message, err) {
  5. Error.call(this);
  6. Error.captureStackTrace(this, TimeoutError);
  7. this.name = 'TimeoutError';
  8. this.message = message;
  9. this.previous = err;
  10. }
  11. util.inherits(TimeoutError, Error);
  12. function matches(match, err) {
  13. if (match === true) return true;
  14. if (typeof match === 'function') {
  15. try {
  16. if (err instanceof match) return true;
  17. } catch (_) {
  18. return !!match(err);
  19. }
  20. }
  21. if (match === err.toString()) return true;
  22. if (match === err.message) return true;
  23. return match instanceof RegExp
  24. && (match.test(err.message) || match.test(err.toString()));
  25. }
  26. module.exports = function retryAsPromised(callback, options) {
  27. if (!callback || !options) {
  28. throw new Error(
  29. 'retry-as-promised must be passed a callback and a options set or a number'
  30. );
  31. }
  32. if (typeof options === 'number') {
  33. options = {
  34. max: options
  35. };
  36. }
  37. // Super cheap clone
  38. options = {
  39. $current: options.$current || 1,
  40. max: options.max,
  41. timeout: options.timeout || undefined,
  42. match: options.match || [],
  43. backoffBase: options.backoffBase === undefined ? 100 : options.backoffBase,
  44. backoffExponent: options.backoffExponent || 1.1,
  45. report: options.report || function () {},
  46. name: options.name || callback.name || 'unknown'
  47. };
  48. if (!Array.isArray(options.match)) options.match = [options.match];
  49. options.report('Trying ' + options.name + ' #' + options.$current + ' at ' + new Date().toLocaleTimeString(), options);
  50. return new Promise(function(resolve, reject) {
  51. var timeout, backoffTimeout, lastError;
  52. if (options.timeout) {
  53. timeout = setTimeout(function() {
  54. if (backoffTimeout) clearTimeout(backoffTimeout);
  55. reject(new TimeoutError(options.name + ' timed out', lastError));
  56. }, options.timeout);
  57. }
  58. Promise.resolve(callback({ current: options.$current }))
  59. .then(resolve)
  60. .then(function() {
  61. if (timeout) clearTimeout(timeout);
  62. if (backoffTimeout) clearTimeout(backoffTimeout);
  63. })
  64. .catch(function(err) {
  65. if (timeout) clearTimeout(timeout);
  66. if (backoffTimeout) clearTimeout(backoffTimeout);
  67. lastError = err;
  68. options.report((err && err.toString()) || err, options);
  69. // Should not retry if max has been reached
  70. var shouldRetry = options.$current < options.max;
  71. if (!shouldRetry) return reject(err);
  72. shouldRetry = options.match.length === 0 || options.match.some(function (match) {
  73. return matches(match, err)
  74. });
  75. if (!shouldRetry) return reject(err);
  76. var retryDelay = options.backoffBase * Math.pow(options.backoffExponent, options.$current - 1);
  77. // Do some accounting
  78. options.$current++;
  79. options.report(format('Retrying %s (%s)', options.name, options.$current), options);
  80. if (retryDelay) {
  81. // Use backoff function to ease retry rate
  82. options.report(format('Delaying retry of %s by %s', options.name, retryDelay), options);
  83. backoffTimeout = setTimeout(function() {
  84. retryAsPromised(callback, options)
  85. .then(resolve)
  86. .catch(reject);
  87. }, retryDelay);
  88. } else {
  89. retryAsPromised(callback, options)
  90. .then(resolve)
  91. .catch(reject);
  92. }
  93. });
  94. });
  95. };
  96. module.exports.TimeoutError = TimeoutError;