1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | 'use strict';
|
15 |
|
16 | const BasePlugin = require('./base.js');
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | class RetryPlugin extends BasePlugin {
|
22 | constructor(client, cfg) {
|
23 | cfg = Object.assign({
|
24 | retryDelayMultiplier: 2,
|
25 | retryErrors: true,
|
26 | retryInitialDelayMsecs: 500,
|
27 | retryStatusCodes: [
|
28 | 429,
|
29 | 500,
|
30 | 501,
|
31 | 502,
|
32 | 503,
|
33 | 504
|
34 | ]
|
35 | }, cfg);
|
36 | super(client, cfg);
|
37 | }
|
38 |
|
39 | onResponse(state, response, callback) {
|
40 | if (this._cfg.retryStatusCodes.indexOf(response.statusCode) !== -1) {
|
41 | state.retry = true;
|
42 | if (state.attempt === 1) {
|
43 | state.retryDelayMsecs = this._cfg.retryInitialDelayMsecs;
|
44 | } else {
|
45 | state.retryDelayMsecs *= this._cfg.retryDelayMultiplier;
|
46 | }
|
47 | }
|
48 | callback(state);
|
49 | }
|
50 |
|
51 | onError(state, error, callback) {
|
52 | if (this._cfg.retryErrors) {
|
53 | state.retry = true;
|
54 | if (state.attempt === 1) {
|
55 | state.retryDelayMsecs = this._cfg.retryInitialDelayMsecs;
|
56 | } else {
|
57 | state.retryDelayMsecs *= this._cfg.retryDelayMultiplier;
|
58 | }
|
59 | }
|
60 | callback(state);
|
61 | }
|
62 | }
|
63 |
|
64 | RetryPlugin.id = 'retry';
|
65 |
|
66 | module.exports = RetryPlugin;
|