root / HServer / 00.Server / 00.Program / node_modules / async / retry.js
이력 | 보기 | 이력해설 | 다운로드 (5.39 KB)
| 1 |
'use strict';
|
|---|---|
| 2 |
|
| 3 |
Object.defineProperty(exports, "__esModule", {
|
| 4 |
value: true |
| 5 |
}); |
| 6 |
exports.default = retry;
|
| 7 |
|
| 8 |
var _noop = require('lodash/noop'); |
| 9 |
|
| 10 |
var _noop2 = _interopRequireDefault(_noop);
|
| 11 |
|
| 12 |
var _constant = require('lodash/constant'); |
| 13 |
|
| 14 |
var _constant2 = _interopRequireDefault(_constant);
|
| 15 |
|
| 16 |
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
| 17 |
|
| 18 |
/**
|
| 19 |
* Attempts to get a successful response from `task` no more than `times` times
|
| 20 |
* before returning an error. If the task is successful, the `callback` will be
|
| 21 |
* passed the result of the successful task. If all attempts fail, the callback
|
| 22 |
* will be passed the error and result (if any) of the final attempt.
|
| 23 |
*
|
| 24 |
* @name retry
|
| 25 |
* @static
|
| 26 |
* @memberOf module:ControlFlow
|
| 27 |
* @method
|
| 28 |
* @category Control Flow
|
| 29 |
* @param {Object|number} [opts = {times: 5, interval: 0}| 5] - Can be either an
|
| 30 |
* object with `times` and `interval` or a number.
|
| 31 |
* * `times` - The number of attempts to make before giving up. The default
|
| 32 |
* is `5`.
|
| 33 |
* * `interval` - The time to wait between retries, in milliseconds. The
|
| 34 |
* default is `0`. The interval may also be specified as a function of the
|
| 35 |
* retry count (see example).
|
| 36 |
* * `errorFilter` - An optional synchronous function that is invoked on
|
| 37 |
* erroneous result. If it returns `true` the retry attempts will continue;
|
| 38 |
* if the function returns `false` the retry flow is aborted with the current
|
| 39 |
* attempt's error and result being returned to the final callback.
|
| 40 |
* Invoked with (err).
|
| 41 |
* * If `opts` is a number, the number specifies the number of times to retry,
|
| 42 |
* with the default interval of `0`.
|
| 43 |
* @param {Function} task - A function which receives two arguments: (1) a
|
| 44 |
* `callback(err, result)` which must be called when finished, passing `err`
|
| 45 |
* (which can be `null`) and the `result` of the function's execution, and (2)
|
| 46 |
* a `results` object, containing the results of the previously executed
|
| 47 |
* functions (if nested inside another control flow). Invoked with
|
| 48 |
* (callback, results).
|
| 49 |
* @param {Function} [callback] - An optional callback which is called when the
|
| 50 |
* task has succeeded, or after the final failed attempt. It receives the `err`
|
| 51 |
* and `result` arguments of the last attempt at completing the `task`. Invoked
|
| 52 |
* with (err, results).
|
| 53 |
* @example
|
| 54 |
*
|
| 55 |
* // The `retry` function can be used as a stand-alone control flow by passing
|
| 56 |
* // a callback, as shown below:
|
| 57 |
*
|
| 58 |
* // try calling apiMethod 3 times
|
| 59 |
* async.retry(3, apiMethod, function(err, result) {
|
| 60 |
* // do something with the result
|
| 61 |
* });
|
| 62 |
*
|
| 63 |
* // try calling apiMethod 3 times, waiting 200 ms between each retry
|
| 64 |
* async.retry({times: 3, interval: 200}, apiMethod, function(err, result) {
|
| 65 |
* // do something with the result
|
| 66 |
* });
|
| 67 |
*
|
| 68 |
* // try calling apiMethod 10 times with exponential backoff
|
| 69 |
* // (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
|
| 70 |
* async.retry({
|
| 71 |
* times: 10,
|
| 72 |
* interval: function(retryCount) {
|
| 73 |
* return 50 * Math.pow(2, retryCount);
|
| 74 |
* }
|
| 75 |
* }, apiMethod, function(err, result) {
|
| 76 |
* // do something with the result
|
| 77 |
* });
|
| 78 |
*
|
| 79 |
* // try calling apiMethod the default 5 times no delay between each retry
|
| 80 |
* async.retry(apiMethod, function(err, result) {
|
| 81 |
* // do something with the result
|
| 82 |
* });
|
| 83 |
*
|
| 84 |
* // try calling apiMethod only when error condition satisfies, all other
|
| 85 |
* // errors will abort the retry control flow and return to final callback
|
| 86 |
* async.retry({
|
| 87 |
* errorFilter: function(err) {
|
| 88 |
* return err.message === 'Temporary error'; // only retry on a specific error
|
| 89 |
* }
|
| 90 |
* }, apiMethod, function(err, result) {
|
| 91 |
* // do something with the result
|
| 92 |
* });
|
| 93 |
*
|
| 94 |
* // It can also be embedded within other control flow functions to retry
|
| 95 |
* // individual methods that are not as reliable, like this:
|
| 96 |
* async.auto({
|
| 97 |
* users: api.getUsers.bind(api),
|
| 98 |
* payments: async.retry(3, api.getPayments.bind(api))
|
| 99 |
* }, function(err, results) {
|
| 100 |
* // do something with the results
|
| 101 |
* });
|
| 102 |
*
|
| 103 |
*/
|
| 104 |
function retry(opts, task, callback) { |
| 105 |
var DEFAULT_TIMES = 5; |
| 106 |
var DEFAULT_INTERVAL = 0; |
| 107 |
|
| 108 |
var options = {
|
| 109 |
times: DEFAULT_TIMES,
|
| 110 |
intervalFunc: (0, _constant2.default)(DEFAULT_INTERVAL) |
| 111 |
}; |
| 112 |
|
| 113 |
function parseTimes(acc, t) { |
| 114 |
if (typeof t === 'object') { |
| 115 |
acc.times = +t.times || DEFAULT_TIMES; |
| 116 |
|
| 117 |
acc.intervalFunc = typeof t.interval === 'function' ? t.interval : (0, _constant2.default)(+t.interval || DEFAULT_INTERVAL); |
| 118 |
|
| 119 |
acc.errorFilter = t.errorFilter; |
| 120 |
} else if (typeof t === 'number' || typeof t === 'string') { |
| 121 |
acc.times = +t || DEFAULT_TIMES; |
| 122 |
} else {
|
| 123 |
throw new Error("Invalid arguments for async.retry"); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if (arguments.length < 3 && typeof opts === 'function') { |
| 128 |
callback = task || _noop2.default;
|
| 129 |
task = opts; |
| 130 |
} else {
|
| 131 |
parseTimes(options, opts); |
| 132 |
callback = callback || _noop2.default;
|
| 133 |
} |
| 134 |
|
| 135 |
if (typeof task !== 'function') { |
| 136 |
throw new Error("Invalid arguments for async.retry"); |
| 137 |
} |
| 138 |
|
| 139 |
var attempt = 1; |
| 140 |
function retryAttempt() { |
| 141 |
task(function (err) {
|
| 142 |
if (err && attempt++ < options.times && (typeof options.errorFilter != 'function' || options.errorFilter(err))) { |
| 143 |
setTimeout(retryAttempt, options.intervalFunc(attempt)); |
| 144 |
} else {
|
| 145 |
callback.apply(null, arguments); |
| 146 |
} |
| 147 |
}); |
| 148 |
} |
| 149 |
|
| 150 |
retryAttempt(); |
| 151 |
} |
| 152 |
module.exports = exports['default'];
|