-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbluebird.retry.js
157 lines (135 loc) · 5.03 KB
/
bluebird.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
const util = require('util');
const bluebird = require('bluebird');
function constant(delayInSeconds = 1) {
if (this instanceof constant) {
this.maxDelayInSeconds = delayInSeconds;
this.delayInSeconds = delayInSeconds;
this.get_delay_ms = function(retry_attempt) {
return this.delayInSeconds * 1000;
}
} else {
return new constant(delayInSeconds);
}
}
function linear(delayInSeconds = 1, maxDelayInSeconds = 60) {
if (this instanceof linear) {
this.delayInSeconds = delayInSeconds;
this.maxDelayInSeconds = maxDelayInSeconds;
this.get_delay_ms = function(retry_attempt) {
return Math.min(this.delayInSeconds * retry_attempt, maxDelayInSeconds) * 1000;
}
} else {
return new linear(delayInSeconds, maxDelayInSeconds);
}
}
function quadratic(delayInSeconds = 1, maxDelayInSeconds = 60) {
if (this instanceof quadratic) {
this.delayInSeconds = delayInSeconds;
this.maxDelayInSeconds = maxDelayInSeconds;
this.get_delay_ms = function(retry_attempt) {
return retry_attempt != 0 ? Math.min(Math.pow(retry_attempt, 2) * this.delayInSeconds, this.maxDelayInSeconds) * 1000 : 0;
}
} else {
return new quadratic(delayInSeconds, maxDelayInSeconds);
}
}
function exponential(delayInSeconds = 1, maxDelayInSeconds = 60) {
if (this instanceof exponential) {
this.delayInSeconds = delayInSeconds;
this.maxDelayInSeconds = maxDelayInSeconds;
this.get_delay_ms = function(retry_attempt) {
return retry_attempt != 0 ? Math.min(Math.pow(retry_attempt, retry_attempt) * this.delayInSeconds, this.maxDelayInSeconds) * 1000 : 0;
}
} else {
return new exponential(delayInSeconds, maxDelayInSeconds);
}
}
/**
* basic retry with scalable backoff and max attempts
*
* @param {any} methodOrPromise
* @param {number} [maxRetryAttempts=1]
* @param {Object} [backoff=constant]
* @returns {bluebird}
*/
function retry(methodOrPromise, maxRetryAttempts = 1, backoff = constant()) {
return _retry(methodOrPromise, backoff, (zero_based_attempt, attempt, errors, start_time) => {
if (zero_based_attempt != 0 && attempt > maxRetryAttempts)
return bluebird.reject(Object.freeze(new RetryAttemptsExceeded(maxRetryAttempts, errors, start_time, Date.now())));
return null;
})
}
/**
* basic retry with scalable backoff and predicate that receives the attempt # via parameter, fail if predicate evaluates to true
*
* @param {any} methodOrPromise
* @param {Function} [predicate]
* @param {Object} [backoff=constant]
* @returns {bluebird}
*/
function predicatedRetry(methodOrPromise, predicate = (retryAttemptIndex) => retryAttemptIndex >= 1, backoff = constant()) {
return _retry(methodOrPromise, backoff, (zero_based_attempt, attempt, errors, start_time) => {
if (zero_based_attempt != 0 && predicate(zero_based_attempt))
return bluebird.reject(Object.freeze(new PredicateViolation(zero_based_attempt, errors, start_time, Date.now())));
return null;
})
}
function _retry(methodOrPromise, backoff, terminationFunction) {
const errors = [];
const start_time = Date.now();
function check(attempt) {
var zero_based_attempt = attempt - 1;
var delay = backoff.get_delay_ms(zero_based_attempt);
var terminate = terminationFunction(zero_based_attempt, attempt, errors, start_time);
if (terminate)
return terminate;
var isPromise = bluebird.is(methodOrPromise);
var isFunction = !isPromise && (methodOrPromise instanceof Function) && methodOrPromise.length === 0;
if (!(isPromise || isFunction))
return bluebird.reject(new TypeError("methodOrPromise must be a Bluebird promise or parameterless javascript Function"));
return bluebird.resolve(isFunction ? bluebird.try(methodOrPromise) : methodOrPromise)
.catch((err) => {
errors.push(err);
return bluebird.delay(delay).then(() => check(attempt + 1));
})
}
return check(1);
}
/**
*
*
* @param {Error[]} errors
*/
function RetryAttemptsExceeded(max_retry_attempts, errors, start_time, end_time) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.duration = end_time - start_time;
this.attempts = max_retry_attempts;
this.message = util.format("Exceeded %d retry attempts in %d milliseconds", this.attempts, this.duration);
this.nested = errors;
}
util.inherits(RetryAttemptsExceeded, Error);
/**
*
*
* @param {Error[]} errors
*/
function PredicateViolation(attempt, errors, start_time, end_time) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.duration = end_time - start_time;
this.attempts = attempt;
this.message = util.format("Predicate failed after %d attempt(s) in %d milliseconds", this.attempts, this.duration);
this.nested = errors;
}
util.inherits(PredicateViolation, Error);
module.exports = {
retry: retry,
predicatedRetry: predicatedRetry,
backoff: { constant, linear, quadratic, exponential },
exceptions: {
RetryAttemptsExceeded: RetryAttemptsExceeded,
PredicateViolation: PredicateViolation
}
}