-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretry.js
59 lines (48 loc) · 2.01 KB
/
retry.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
49
50
51
52
53
54
55
56
57
58
59
/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))})
(['./io', './scaffold'], function (io, scaffold) {
'use strict';
// implement retries for unreliable I/O requests
function retry (options, prep, level) {
if (typeof options.retries != 'number' || !io.retry.optIn(options)) {
return null;
}
// pass the request, and retry conditionally
var retries = options.retries,
continueRetries = typeof options.continueRetries == 'function' ? options.continueRetries : continueRetriesIfNot2XX,
nextDelay = typeof options.nextDelay == 'function' ? options.nextDelay : io.retry.nextDelay,
delayMs = typeof options.initDelay == 'number' ? options.initDelay : io.retry.initDelay,
currentRetry = 0;
return io.request(options, prep, level - 1).then(retries > 0 ? loop : condLoop);
function loop(result) {
++currentRetry;
if (--retries >= 0 && continueRetries(result, currentRetry)) {
delayMs = nextDelay(delayMs, currentRetry, options);
return io.retry.delay(delayMs).then(function() { return io.request(options, prep, level - 1); }).then(loop);
}
return result;
}
function condLoop(result) {
++currentRetry;
if (continueRetries(result, currentRetry)) {
delayMs = nextDelay(delayMs, currentRetry, options);
return io.retry.delay(delayMs).then(function() { return io.request(options, prep, level - 1); }).then(condLoop);
}
return result;
}
}
function delay (ms) {
var d = new io.Deferred();
setTimeout(function () { d.resolve(ms); }, ms);
return d.promise || d;
}
function continueRetriesIfNot2XX (result) { return result.xhr && (result.xhr.status < 200 || result.xhr.status >= 300); }
function defaultOptIn (options) { return !options.transport; }
// export
io.retry = {
delay: delay,
initDelay: 50, //ms
nextDelay: function (delay, retry, options) { return delay; },
defaultOptIn: defaultOptIn
};
return scaffold(io, 'retry', 30, retry);
});