@smithy/middleware-retry 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +4 -0
- package/dist-cjs/AdaptiveRetryStrategy.js +24 -0
- package/dist-cjs/StandardRetryStrategy.js +95 -0
- package/dist-cjs/configurations.js +57 -0
- package/dist-cjs/defaultRetryQuota.js +32 -0
- package/dist-cjs/delayDecider.js +6 -0
- package/dist-cjs/index.js +10 -0
- package/dist-cjs/omitRetryHeadersMiddleware.js +27 -0
- package/dist-cjs/retryDecider.js +11 -0
- package/dist-cjs/retryMiddleware.js +110 -0
- package/dist-cjs/types.js +2 -0
- package/dist-cjs/util.js +13 -0
- package/dist-es/AdaptiveRetryStrategy.js +20 -0
- package/dist-es/StandardRetryStrategy.js +90 -0
- package/dist-es/configurations.js +52 -0
- package/dist-es/defaultRetryQuota.js +27 -0
- package/dist-es/delayDecider.js +2 -0
- package/dist-es/index.js +7 -0
- package/dist-es/omitRetryHeadersMiddleware.js +22 -0
- package/dist-es/retryDecider.js +7 -0
- package/dist-es/retryMiddleware.js +104 -0
- package/dist-es/types.js +1 -0
- package/dist-es/util.js +9 -0
- package/dist-types/AdaptiveRetryStrategy.d.ts +20 -0
- package/dist-types/StandardRetryStrategy.d.ts +30 -0
- package/dist-types/configurations.d.ts +37 -0
- package/dist-types/defaultRetryQuota.d.ts +18 -0
- package/dist-types/delayDecider.d.ts +4 -0
- package/dist-types/index.d.ts +7 -0
- package/dist-types/omitRetryHeadersMiddleware.d.ts +4 -0
- package/dist-types/retryDecider.d.ts +2 -0
- package/dist-types/retryMiddleware.d.ts +6 -0
- package/dist-types/types.d.ts +53 -0
- package/dist-types/util.d.ts +2 -0
- package/package.json +67 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
# @smithy/middleware-retry
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@smithy/middleware-retry)
|
|
4
|
+
[](https://www.npmjs.com/package/@smithy/middleware-retry)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AdaptiveRetryStrategy = void 0;
|
|
4
|
+
const util_retry_1 = require("@smithy/util-retry");
|
|
5
|
+
const StandardRetryStrategy_1 = require("./StandardRetryStrategy");
|
|
6
|
+
class AdaptiveRetryStrategy extends StandardRetryStrategy_1.StandardRetryStrategy {
|
|
7
|
+
constructor(maxAttemptsProvider, options) {
|
|
8
|
+
const { rateLimiter, ...superOptions } = options !== null && options !== void 0 ? options : {};
|
|
9
|
+
super(maxAttemptsProvider, superOptions);
|
|
10
|
+
this.rateLimiter = rateLimiter !== null && rateLimiter !== void 0 ? rateLimiter : new util_retry_1.DefaultRateLimiter();
|
|
11
|
+
this.mode = util_retry_1.RETRY_MODES.ADAPTIVE;
|
|
12
|
+
}
|
|
13
|
+
async retry(next, args) {
|
|
14
|
+
return super.retry(next, args, {
|
|
15
|
+
beforeRequest: async () => {
|
|
16
|
+
return this.rateLimiter.getSendToken();
|
|
17
|
+
},
|
|
18
|
+
afterRequest: (response) => {
|
|
19
|
+
this.rateLimiter.updateClientSendingRate(response);
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
exports.AdaptiveRetryStrategy = AdaptiveRetryStrategy;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.StandardRetryStrategy = void 0;
|
|
4
|
+
const protocol_http_1 = require("@smithy/protocol-http");
|
|
5
|
+
const service_error_classification_1 = require("@smithy/service-error-classification");
|
|
6
|
+
const util_retry_1 = require("@smithy/util-retry");
|
|
7
|
+
const uuid_1 = require("uuid");
|
|
8
|
+
const defaultRetryQuota_1 = require("./defaultRetryQuota");
|
|
9
|
+
const delayDecider_1 = require("./delayDecider");
|
|
10
|
+
const retryDecider_1 = require("./retryDecider");
|
|
11
|
+
const util_1 = require("./util");
|
|
12
|
+
class StandardRetryStrategy {
|
|
13
|
+
constructor(maxAttemptsProvider, options) {
|
|
14
|
+
var _a, _b, _c;
|
|
15
|
+
this.maxAttemptsProvider = maxAttemptsProvider;
|
|
16
|
+
this.mode = util_retry_1.RETRY_MODES.STANDARD;
|
|
17
|
+
this.retryDecider = (_a = options === null || options === void 0 ? void 0 : options.retryDecider) !== null && _a !== void 0 ? _a : retryDecider_1.defaultRetryDecider;
|
|
18
|
+
this.delayDecider = (_b = options === null || options === void 0 ? void 0 : options.delayDecider) !== null && _b !== void 0 ? _b : delayDecider_1.defaultDelayDecider;
|
|
19
|
+
this.retryQuota = (_c = options === null || options === void 0 ? void 0 : options.retryQuota) !== null && _c !== void 0 ? _c : (0, defaultRetryQuota_1.getDefaultRetryQuota)(util_retry_1.INITIAL_RETRY_TOKENS);
|
|
20
|
+
}
|
|
21
|
+
shouldRetry(error, attempts, maxAttempts) {
|
|
22
|
+
return attempts < maxAttempts && this.retryDecider(error) && this.retryQuota.hasRetryTokens(error);
|
|
23
|
+
}
|
|
24
|
+
async getMaxAttempts() {
|
|
25
|
+
let maxAttempts;
|
|
26
|
+
try {
|
|
27
|
+
maxAttempts = await this.maxAttemptsProvider();
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
maxAttempts = util_retry_1.DEFAULT_MAX_ATTEMPTS;
|
|
31
|
+
}
|
|
32
|
+
return maxAttempts;
|
|
33
|
+
}
|
|
34
|
+
async retry(next, args, options) {
|
|
35
|
+
let retryTokenAmount;
|
|
36
|
+
let attempts = 0;
|
|
37
|
+
let totalDelay = 0;
|
|
38
|
+
const maxAttempts = await this.getMaxAttempts();
|
|
39
|
+
const { request } = args;
|
|
40
|
+
if (protocol_http_1.HttpRequest.isInstance(request)) {
|
|
41
|
+
request.headers[util_retry_1.INVOCATION_ID_HEADER] = (0, uuid_1.v4)();
|
|
42
|
+
}
|
|
43
|
+
while (true) {
|
|
44
|
+
try {
|
|
45
|
+
if (protocol_http_1.HttpRequest.isInstance(request)) {
|
|
46
|
+
request.headers[util_retry_1.REQUEST_HEADER] = `attempt=${attempts + 1}; max=${maxAttempts}`;
|
|
47
|
+
}
|
|
48
|
+
if (options === null || options === void 0 ? void 0 : options.beforeRequest) {
|
|
49
|
+
await options.beforeRequest();
|
|
50
|
+
}
|
|
51
|
+
const { response, output } = await next(args);
|
|
52
|
+
if (options === null || options === void 0 ? void 0 : options.afterRequest) {
|
|
53
|
+
options.afterRequest(response);
|
|
54
|
+
}
|
|
55
|
+
this.retryQuota.releaseRetryTokens(retryTokenAmount);
|
|
56
|
+
output.$metadata.attempts = attempts + 1;
|
|
57
|
+
output.$metadata.totalRetryDelay = totalDelay;
|
|
58
|
+
return { response, output };
|
|
59
|
+
}
|
|
60
|
+
catch (e) {
|
|
61
|
+
const err = (0, util_1.asSdkError)(e);
|
|
62
|
+
attempts++;
|
|
63
|
+
if (this.shouldRetry(err, attempts, maxAttempts)) {
|
|
64
|
+
retryTokenAmount = this.retryQuota.retrieveRetryTokens(err);
|
|
65
|
+
const delayFromDecider = this.delayDecider((0, service_error_classification_1.isThrottlingError)(err) ? util_retry_1.THROTTLING_RETRY_DELAY_BASE : util_retry_1.DEFAULT_RETRY_DELAY_BASE, attempts);
|
|
66
|
+
const delayFromResponse = getDelayFromRetryAfterHeader(err.$response);
|
|
67
|
+
const delay = Math.max(delayFromResponse || 0, delayFromDecider);
|
|
68
|
+
totalDelay += delay;
|
|
69
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
if (!err.$metadata) {
|
|
73
|
+
err.$metadata = {};
|
|
74
|
+
}
|
|
75
|
+
err.$metadata.attempts = attempts;
|
|
76
|
+
err.$metadata.totalRetryDelay = totalDelay;
|
|
77
|
+
throw err;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.StandardRetryStrategy = StandardRetryStrategy;
|
|
83
|
+
const getDelayFromRetryAfterHeader = (response) => {
|
|
84
|
+
if (!protocol_http_1.HttpResponse.isInstance(response))
|
|
85
|
+
return;
|
|
86
|
+
const retryAfterHeaderName = Object.keys(response.headers).find((key) => key.toLowerCase() === "retry-after");
|
|
87
|
+
if (!retryAfterHeaderName)
|
|
88
|
+
return;
|
|
89
|
+
const retryAfter = response.headers[retryAfterHeaderName];
|
|
90
|
+
const retryAfterSeconds = Number(retryAfter);
|
|
91
|
+
if (!Number.isNaN(retryAfterSeconds))
|
|
92
|
+
return retryAfterSeconds * 1000;
|
|
93
|
+
const retryAfterDate = new Date(retryAfter);
|
|
94
|
+
return retryAfterDate.getTime() - Date.now();
|
|
95
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NODE_RETRY_MODE_CONFIG_OPTIONS = exports.CONFIG_RETRY_MODE = exports.ENV_RETRY_MODE = exports.resolveRetryConfig = exports.NODE_MAX_ATTEMPT_CONFIG_OPTIONS = exports.CONFIG_MAX_ATTEMPTS = exports.ENV_MAX_ATTEMPTS = void 0;
|
|
4
|
+
const util_middleware_1 = require("@smithy/util-middleware");
|
|
5
|
+
const util_retry_1 = require("@smithy/util-retry");
|
|
6
|
+
exports.ENV_MAX_ATTEMPTS = "AWS_MAX_ATTEMPTS";
|
|
7
|
+
exports.CONFIG_MAX_ATTEMPTS = "max_attempts";
|
|
8
|
+
exports.NODE_MAX_ATTEMPT_CONFIG_OPTIONS = {
|
|
9
|
+
environmentVariableSelector: (env) => {
|
|
10
|
+
const value = env[exports.ENV_MAX_ATTEMPTS];
|
|
11
|
+
if (!value)
|
|
12
|
+
return undefined;
|
|
13
|
+
const maxAttempt = parseInt(value);
|
|
14
|
+
if (Number.isNaN(maxAttempt)) {
|
|
15
|
+
throw new Error(`Environment variable ${exports.ENV_MAX_ATTEMPTS} mast be a number, got "${value}"`);
|
|
16
|
+
}
|
|
17
|
+
return maxAttempt;
|
|
18
|
+
},
|
|
19
|
+
configFileSelector: (profile) => {
|
|
20
|
+
const value = profile[exports.CONFIG_MAX_ATTEMPTS];
|
|
21
|
+
if (!value)
|
|
22
|
+
return undefined;
|
|
23
|
+
const maxAttempt = parseInt(value);
|
|
24
|
+
if (Number.isNaN(maxAttempt)) {
|
|
25
|
+
throw new Error(`Shared config file entry ${exports.CONFIG_MAX_ATTEMPTS} mast be a number, got "${value}"`);
|
|
26
|
+
}
|
|
27
|
+
return maxAttempt;
|
|
28
|
+
},
|
|
29
|
+
default: util_retry_1.DEFAULT_MAX_ATTEMPTS,
|
|
30
|
+
};
|
|
31
|
+
const resolveRetryConfig = (input) => {
|
|
32
|
+
var _a;
|
|
33
|
+
const { retryStrategy } = input;
|
|
34
|
+
const maxAttempts = (0, util_middleware_1.normalizeProvider)((_a = input.maxAttempts) !== null && _a !== void 0 ? _a : util_retry_1.DEFAULT_MAX_ATTEMPTS);
|
|
35
|
+
return {
|
|
36
|
+
...input,
|
|
37
|
+
maxAttempts,
|
|
38
|
+
retryStrategy: async () => {
|
|
39
|
+
if (retryStrategy) {
|
|
40
|
+
return retryStrategy;
|
|
41
|
+
}
|
|
42
|
+
const retryMode = await (0, util_middleware_1.normalizeProvider)(input.retryMode)();
|
|
43
|
+
if (retryMode === util_retry_1.RETRY_MODES.ADAPTIVE) {
|
|
44
|
+
return new util_retry_1.AdaptiveRetryStrategy(maxAttempts);
|
|
45
|
+
}
|
|
46
|
+
return new util_retry_1.StandardRetryStrategy(maxAttempts);
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
exports.resolveRetryConfig = resolveRetryConfig;
|
|
51
|
+
exports.ENV_RETRY_MODE = "AWS_RETRY_MODE";
|
|
52
|
+
exports.CONFIG_RETRY_MODE = "retry_mode";
|
|
53
|
+
exports.NODE_RETRY_MODE_CONFIG_OPTIONS = {
|
|
54
|
+
environmentVariableSelector: (env) => env[exports.ENV_RETRY_MODE],
|
|
55
|
+
configFileSelector: (profile) => profile[exports.CONFIG_RETRY_MODE],
|
|
56
|
+
default: util_retry_1.DEFAULT_RETRY_MODE,
|
|
57
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getDefaultRetryQuota = void 0;
|
|
4
|
+
const util_retry_1 = require("@smithy/util-retry");
|
|
5
|
+
const getDefaultRetryQuota = (initialRetryTokens, options) => {
|
|
6
|
+
var _a, _b, _c;
|
|
7
|
+
const MAX_CAPACITY = initialRetryTokens;
|
|
8
|
+
const noRetryIncrement = (_a = options === null || options === void 0 ? void 0 : options.noRetryIncrement) !== null && _a !== void 0 ? _a : util_retry_1.NO_RETRY_INCREMENT;
|
|
9
|
+
const retryCost = (_b = options === null || options === void 0 ? void 0 : options.retryCost) !== null && _b !== void 0 ? _b : util_retry_1.RETRY_COST;
|
|
10
|
+
const timeoutRetryCost = (_c = options === null || options === void 0 ? void 0 : options.timeoutRetryCost) !== null && _c !== void 0 ? _c : util_retry_1.TIMEOUT_RETRY_COST;
|
|
11
|
+
let availableCapacity = initialRetryTokens;
|
|
12
|
+
const getCapacityAmount = (error) => (error.name === "TimeoutError" ? timeoutRetryCost : retryCost);
|
|
13
|
+
const hasRetryTokens = (error) => getCapacityAmount(error) <= availableCapacity;
|
|
14
|
+
const retrieveRetryTokens = (error) => {
|
|
15
|
+
if (!hasRetryTokens(error)) {
|
|
16
|
+
throw new Error("No retry token available");
|
|
17
|
+
}
|
|
18
|
+
const capacityAmount = getCapacityAmount(error);
|
|
19
|
+
availableCapacity -= capacityAmount;
|
|
20
|
+
return capacityAmount;
|
|
21
|
+
};
|
|
22
|
+
const releaseRetryTokens = (capacityReleaseAmount) => {
|
|
23
|
+
availableCapacity += capacityReleaseAmount !== null && capacityReleaseAmount !== void 0 ? capacityReleaseAmount : noRetryIncrement;
|
|
24
|
+
availableCapacity = Math.min(availableCapacity, MAX_CAPACITY);
|
|
25
|
+
};
|
|
26
|
+
return Object.freeze({
|
|
27
|
+
hasRetryTokens,
|
|
28
|
+
retrieveRetryTokens,
|
|
29
|
+
releaseRetryTokens,
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
exports.getDefaultRetryQuota = getDefaultRetryQuota;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultDelayDecider = void 0;
|
|
4
|
+
const util_retry_1 = require("@smithy/util-retry");
|
|
5
|
+
const defaultDelayDecider = (delayBase, attempts) => Math.floor(Math.min(util_retry_1.MAXIMUM_RETRY_DELAY, Math.random() * 2 ** attempts * delayBase));
|
|
6
|
+
exports.defaultDelayDecider = defaultDelayDecider;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
tslib_1.__exportStar(require("./AdaptiveRetryStrategy"), exports);
|
|
5
|
+
tslib_1.__exportStar(require("./StandardRetryStrategy"), exports);
|
|
6
|
+
tslib_1.__exportStar(require("./configurations"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./delayDecider"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./omitRetryHeadersMiddleware"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./retryDecider"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./retryMiddleware"), exports);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getOmitRetryHeadersPlugin = exports.omitRetryHeadersMiddlewareOptions = exports.omitRetryHeadersMiddleware = void 0;
|
|
4
|
+
const protocol_http_1 = require("@smithy/protocol-http");
|
|
5
|
+
const util_retry_1 = require("@smithy/util-retry");
|
|
6
|
+
const omitRetryHeadersMiddleware = () => (next) => async (args) => {
|
|
7
|
+
const { request } = args;
|
|
8
|
+
if (protocol_http_1.HttpRequest.isInstance(request)) {
|
|
9
|
+
delete request.headers[util_retry_1.INVOCATION_ID_HEADER];
|
|
10
|
+
delete request.headers[util_retry_1.REQUEST_HEADER];
|
|
11
|
+
}
|
|
12
|
+
return next(args);
|
|
13
|
+
};
|
|
14
|
+
exports.omitRetryHeadersMiddleware = omitRetryHeadersMiddleware;
|
|
15
|
+
exports.omitRetryHeadersMiddlewareOptions = {
|
|
16
|
+
name: "omitRetryHeadersMiddleware",
|
|
17
|
+
tags: ["RETRY", "HEADERS", "OMIT_RETRY_HEADERS"],
|
|
18
|
+
relation: "before",
|
|
19
|
+
toMiddleware: "awsAuthMiddleware",
|
|
20
|
+
override: true,
|
|
21
|
+
};
|
|
22
|
+
const getOmitRetryHeadersPlugin = (options) => ({
|
|
23
|
+
applyToStack: (clientStack) => {
|
|
24
|
+
clientStack.addRelativeTo((0, exports.omitRetryHeadersMiddleware)(), exports.omitRetryHeadersMiddlewareOptions);
|
|
25
|
+
},
|
|
26
|
+
});
|
|
27
|
+
exports.getOmitRetryHeadersPlugin = getOmitRetryHeadersPlugin;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultRetryDecider = void 0;
|
|
4
|
+
const service_error_classification_1 = require("@smithy/service-error-classification");
|
|
5
|
+
const defaultRetryDecider = (error) => {
|
|
6
|
+
if (!error) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
return (0, service_error_classification_1.isRetryableByTrait)(error) || (0, service_error_classification_1.isClockSkewError)(error) || (0, service_error_classification_1.isThrottlingError)(error) || (0, service_error_classification_1.isTransientError)(error);
|
|
10
|
+
};
|
|
11
|
+
exports.defaultRetryDecider = defaultRetryDecider;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getRetryAfterHint = exports.getRetryPlugin = exports.retryMiddlewareOptions = exports.retryMiddleware = void 0;
|
|
4
|
+
const protocol_http_1 = require("@smithy/protocol-http");
|
|
5
|
+
const service_error_classification_1 = require("@smithy/service-error-classification");
|
|
6
|
+
const util_retry_1 = require("@smithy/util-retry");
|
|
7
|
+
const uuid_1 = require("uuid");
|
|
8
|
+
const util_1 = require("./util");
|
|
9
|
+
const retryMiddleware = (options) => (next, context) => async (args) => {
|
|
10
|
+
let retryStrategy = await options.retryStrategy();
|
|
11
|
+
const maxAttempts = await options.maxAttempts();
|
|
12
|
+
if (isRetryStrategyV2(retryStrategy)) {
|
|
13
|
+
retryStrategy = retryStrategy;
|
|
14
|
+
let retryToken = await retryStrategy.acquireInitialRetryToken(context["partition_id"]);
|
|
15
|
+
let lastError = new Error();
|
|
16
|
+
let attempts = 0;
|
|
17
|
+
let totalRetryDelay = 0;
|
|
18
|
+
const { request } = args;
|
|
19
|
+
if (protocol_http_1.HttpRequest.isInstance(request)) {
|
|
20
|
+
request.headers[util_retry_1.INVOCATION_ID_HEADER] = (0, uuid_1.v4)();
|
|
21
|
+
}
|
|
22
|
+
while (true) {
|
|
23
|
+
try {
|
|
24
|
+
if (protocol_http_1.HttpRequest.isInstance(request)) {
|
|
25
|
+
request.headers[util_retry_1.REQUEST_HEADER] = `attempt=${attempts + 1}; max=${maxAttempts}`;
|
|
26
|
+
}
|
|
27
|
+
const { response, output } = await next(args);
|
|
28
|
+
retryStrategy.recordSuccess(retryToken);
|
|
29
|
+
output.$metadata.attempts = attempts + 1;
|
|
30
|
+
output.$metadata.totalRetryDelay = totalRetryDelay;
|
|
31
|
+
return { response, output };
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
const retryErrorInfo = getRetryErrorInfo(e);
|
|
35
|
+
lastError = (0, util_1.asSdkError)(e);
|
|
36
|
+
try {
|
|
37
|
+
retryToken = await retryStrategy.refreshRetryTokenForRetry(retryToken, retryErrorInfo);
|
|
38
|
+
}
|
|
39
|
+
catch (refreshError) {
|
|
40
|
+
if (!lastError.$metadata) {
|
|
41
|
+
lastError.$metadata = {};
|
|
42
|
+
}
|
|
43
|
+
lastError.$metadata.attempts = attempts + 1;
|
|
44
|
+
lastError.$metadata.totalRetryDelay = totalRetryDelay;
|
|
45
|
+
throw lastError;
|
|
46
|
+
}
|
|
47
|
+
attempts = retryToken.getRetryCount();
|
|
48
|
+
const delay = retryToken.getRetryDelay();
|
|
49
|
+
totalRetryDelay += delay;
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
retryStrategy = retryStrategy;
|
|
56
|
+
if (retryStrategy === null || retryStrategy === void 0 ? void 0 : retryStrategy.mode)
|
|
57
|
+
context.userAgent = [...(context.userAgent || []), ["cfg/retry-mode", retryStrategy.mode]];
|
|
58
|
+
return retryStrategy.retry(next, args);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
exports.retryMiddleware = retryMiddleware;
|
|
62
|
+
const isRetryStrategyV2 = (retryStrategy) => typeof retryStrategy.acquireInitialRetryToken !== "undefined" &&
|
|
63
|
+
typeof retryStrategy.refreshRetryTokenForRetry !== "undefined" &&
|
|
64
|
+
typeof retryStrategy.recordSuccess !== "undefined";
|
|
65
|
+
const getRetryErrorInfo = (error) => {
|
|
66
|
+
const errorInfo = {
|
|
67
|
+
errorType: getRetryErrorType(error),
|
|
68
|
+
};
|
|
69
|
+
const retryAfterHint = (0, exports.getRetryAfterHint)(error.$response);
|
|
70
|
+
if (retryAfterHint) {
|
|
71
|
+
errorInfo.retryAfterHint = retryAfterHint;
|
|
72
|
+
}
|
|
73
|
+
return errorInfo;
|
|
74
|
+
};
|
|
75
|
+
const getRetryErrorType = (error) => {
|
|
76
|
+
if ((0, service_error_classification_1.isThrottlingError)(error))
|
|
77
|
+
return "THROTTLING";
|
|
78
|
+
if ((0, service_error_classification_1.isTransientError)(error))
|
|
79
|
+
return "TRANSIENT";
|
|
80
|
+
if ((0, service_error_classification_1.isServerError)(error))
|
|
81
|
+
return "SERVER_ERROR";
|
|
82
|
+
return "CLIENT_ERROR";
|
|
83
|
+
};
|
|
84
|
+
exports.retryMiddlewareOptions = {
|
|
85
|
+
name: "retryMiddleware",
|
|
86
|
+
tags: ["RETRY"],
|
|
87
|
+
step: "finalizeRequest",
|
|
88
|
+
priority: "high",
|
|
89
|
+
override: true,
|
|
90
|
+
};
|
|
91
|
+
const getRetryPlugin = (options) => ({
|
|
92
|
+
applyToStack: (clientStack) => {
|
|
93
|
+
clientStack.add((0, exports.retryMiddleware)(options), exports.retryMiddlewareOptions);
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
exports.getRetryPlugin = getRetryPlugin;
|
|
97
|
+
const getRetryAfterHint = (response) => {
|
|
98
|
+
if (!protocol_http_1.HttpResponse.isInstance(response))
|
|
99
|
+
return;
|
|
100
|
+
const retryAfterHeaderName = Object.keys(response.headers).find((key) => key.toLowerCase() === "retry-after");
|
|
101
|
+
if (!retryAfterHeaderName)
|
|
102
|
+
return;
|
|
103
|
+
const retryAfter = response.headers[retryAfterHeaderName];
|
|
104
|
+
const retryAfterSeconds = Number(retryAfter);
|
|
105
|
+
if (!Number.isNaN(retryAfterSeconds))
|
|
106
|
+
return new Date(retryAfterSeconds * 1000);
|
|
107
|
+
const retryAfterDate = new Date(retryAfter);
|
|
108
|
+
return retryAfterDate;
|
|
109
|
+
};
|
|
110
|
+
exports.getRetryAfterHint = getRetryAfterHint;
|
package/dist-cjs/util.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.asSdkError = void 0;
|
|
4
|
+
const asSdkError = (error) => {
|
|
5
|
+
if (error instanceof Error)
|
|
6
|
+
return error;
|
|
7
|
+
if (error instanceof Object)
|
|
8
|
+
return Object.assign(new Error(), error);
|
|
9
|
+
if (typeof error === "string")
|
|
10
|
+
return new Error(error);
|
|
11
|
+
return new Error(`AWS SDK error wrapper for ${error}`);
|
|
12
|
+
};
|
|
13
|
+
exports.asSdkError = asSdkError;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { DefaultRateLimiter, RETRY_MODES } from "@smithy/util-retry";
|
|
2
|
+
import { StandardRetryStrategy } from "./StandardRetryStrategy";
|
|
3
|
+
export class AdaptiveRetryStrategy extends StandardRetryStrategy {
|
|
4
|
+
constructor(maxAttemptsProvider, options) {
|
|
5
|
+
const { rateLimiter, ...superOptions } = options ?? {};
|
|
6
|
+
super(maxAttemptsProvider, superOptions);
|
|
7
|
+
this.rateLimiter = rateLimiter ?? new DefaultRateLimiter();
|
|
8
|
+
this.mode = RETRY_MODES.ADAPTIVE;
|
|
9
|
+
}
|
|
10
|
+
async retry(next, args) {
|
|
11
|
+
return super.retry(next, args, {
|
|
12
|
+
beforeRequest: async () => {
|
|
13
|
+
return this.rateLimiter.getSendToken();
|
|
14
|
+
},
|
|
15
|
+
afterRequest: (response) => {
|
|
16
|
+
this.rateLimiter.updateClientSendingRate(response);
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { HttpRequest, HttpResponse } from "@smithy/protocol-http";
|
|
2
|
+
import { isThrottlingError } from "@smithy/service-error-classification";
|
|
3
|
+
import { DEFAULT_MAX_ATTEMPTS, DEFAULT_RETRY_DELAY_BASE, INITIAL_RETRY_TOKENS, INVOCATION_ID_HEADER, REQUEST_HEADER, RETRY_MODES, THROTTLING_RETRY_DELAY_BASE, } from "@smithy/util-retry";
|
|
4
|
+
import { v4 } from "uuid";
|
|
5
|
+
import { getDefaultRetryQuota } from "./defaultRetryQuota";
|
|
6
|
+
import { defaultDelayDecider } from "./delayDecider";
|
|
7
|
+
import { defaultRetryDecider } from "./retryDecider";
|
|
8
|
+
import { asSdkError } from "./util";
|
|
9
|
+
export class StandardRetryStrategy {
|
|
10
|
+
constructor(maxAttemptsProvider, options) {
|
|
11
|
+
this.maxAttemptsProvider = maxAttemptsProvider;
|
|
12
|
+
this.mode = RETRY_MODES.STANDARD;
|
|
13
|
+
this.retryDecider = options?.retryDecider ?? defaultRetryDecider;
|
|
14
|
+
this.delayDecider = options?.delayDecider ?? defaultDelayDecider;
|
|
15
|
+
this.retryQuota = options?.retryQuota ?? getDefaultRetryQuota(INITIAL_RETRY_TOKENS);
|
|
16
|
+
}
|
|
17
|
+
shouldRetry(error, attempts, maxAttempts) {
|
|
18
|
+
return attempts < maxAttempts && this.retryDecider(error) && this.retryQuota.hasRetryTokens(error);
|
|
19
|
+
}
|
|
20
|
+
async getMaxAttempts() {
|
|
21
|
+
let maxAttempts;
|
|
22
|
+
try {
|
|
23
|
+
maxAttempts = await this.maxAttemptsProvider();
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
maxAttempts = DEFAULT_MAX_ATTEMPTS;
|
|
27
|
+
}
|
|
28
|
+
return maxAttempts;
|
|
29
|
+
}
|
|
30
|
+
async retry(next, args, options) {
|
|
31
|
+
let retryTokenAmount;
|
|
32
|
+
let attempts = 0;
|
|
33
|
+
let totalDelay = 0;
|
|
34
|
+
const maxAttempts = await this.getMaxAttempts();
|
|
35
|
+
const { request } = args;
|
|
36
|
+
if (HttpRequest.isInstance(request)) {
|
|
37
|
+
request.headers[INVOCATION_ID_HEADER] = v4();
|
|
38
|
+
}
|
|
39
|
+
while (true) {
|
|
40
|
+
try {
|
|
41
|
+
if (HttpRequest.isInstance(request)) {
|
|
42
|
+
request.headers[REQUEST_HEADER] = `attempt=${attempts + 1}; max=${maxAttempts}`;
|
|
43
|
+
}
|
|
44
|
+
if (options?.beforeRequest) {
|
|
45
|
+
await options.beforeRequest();
|
|
46
|
+
}
|
|
47
|
+
const { response, output } = await next(args);
|
|
48
|
+
if (options?.afterRequest) {
|
|
49
|
+
options.afterRequest(response);
|
|
50
|
+
}
|
|
51
|
+
this.retryQuota.releaseRetryTokens(retryTokenAmount);
|
|
52
|
+
output.$metadata.attempts = attempts + 1;
|
|
53
|
+
output.$metadata.totalRetryDelay = totalDelay;
|
|
54
|
+
return { response, output };
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
const err = asSdkError(e);
|
|
58
|
+
attempts++;
|
|
59
|
+
if (this.shouldRetry(err, attempts, maxAttempts)) {
|
|
60
|
+
retryTokenAmount = this.retryQuota.retrieveRetryTokens(err);
|
|
61
|
+
const delayFromDecider = this.delayDecider(isThrottlingError(err) ? THROTTLING_RETRY_DELAY_BASE : DEFAULT_RETRY_DELAY_BASE, attempts);
|
|
62
|
+
const delayFromResponse = getDelayFromRetryAfterHeader(err.$response);
|
|
63
|
+
const delay = Math.max(delayFromResponse || 0, delayFromDecider);
|
|
64
|
+
totalDelay += delay;
|
|
65
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
if (!err.$metadata) {
|
|
69
|
+
err.$metadata = {};
|
|
70
|
+
}
|
|
71
|
+
err.$metadata.attempts = attempts;
|
|
72
|
+
err.$metadata.totalRetryDelay = totalDelay;
|
|
73
|
+
throw err;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const getDelayFromRetryAfterHeader = (response) => {
|
|
79
|
+
if (!HttpResponse.isInstance(response))
|
|
80
|
+
return;
|
|
81
|
+
const retryAfterHeaderName = Object.keys(response.headers).find((key) => key.toLowerCase() === "retry-after");
|
|
82
|
+
if (!retryAfterHeaderName)
|
|
83
|
+
return;
|
|
84
|
+
const retryAfter = response.headers[retryAfterHeaderName];
|
|
85
|
+
const retryAfterSeconds = Number(retryAfter);
|
|
86
|
+
if (!Number.isNaN(retryAfterSeconds))
|
|
87
|
+
return retryAfterSeconds * 1000;
|
|
88
|
+
const retryAfterDate = new Date(retryAfter);
|
|
89
|
+
return retryAfterDate.getTime() - Date.now();
|
|
90
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { normalizeProvider } from "@smithy/util-middleware";
|
|
2
|
+
import { AdaptiveRetryStrategy, DEFAULT_MAX_ATTEMPTS, DEFAULT_RETRY_MODE, RETRY_MODES, StandardRetryStrategy, } from "@smithy/util-retry";
|
|
3
|
+
export const ENV_MAX_ATTEMPTS = "AWS_MAX_ATTEMPTS";
|
|
4
|
+
export const CONFIG_MAX_ATTEMPTS = "max_attempts";
|
|
5
|
+
export const NODE_MAX_ATTEMPT_CONFIG_OPTIONS = {
|
|
6
|
+
environmentVariableSelector: (env) => {
|
|
7
|
+
const value = env[ENV_MAX_ATTEMPTS];
|
|
8
|
+
if (!value)
|
|
9
|
+
return undefined;
|
|
10
|
+
const maxAttempt = parseInt(value);
|
|
11
|
+
if (Number.isNaN(maxAttempt)) {
|
|
12
|
+
throw new Error(`Environment variable ${ENV_MAX_ATTEMPTS} mast be a number, got "${value}"`);
|
|
13
|
+
}
|
|
14
|
+
return maxAttempt;
|
|
15
|
+
},
|
|
16
|
+
configFileSelector: (profile) => {
|
|
17
|
+
const value = profile[CONFIG_MAX_ATTEMPTS];
|
|
18
|
+
if (!value)
|
|
19
|
+
return undefined;
|
|
20
|
+
const maxAttempt = parseInt(value);
|
|
21
|
+
if (Number.isNaN(maxAttempt)) {
|
|
22
|
+
throw new Error(`Shared config file entry ${CONFIG_MAX_ATTEMPTS} mast be a number, got "${value}"`);
|
|
23
|
+
}
|
|
24
|
+
return maxAttempt;
|
|
25
|
+
},
|
|
26
|
+
default: DEFAULT_MAX_ATTEMPTS,
|
|
27
|
+
};
|
|
28
|
+
export const resolveRetryConfig = (input) => {
|
|
29
|
+
const { retryStrategy } = input;
|
|
30
|
+
const maxAttempts = normalizeProvider(input.maxAttempts ?? DEFAULT_MAX_ATTEMPTS);
|
|
31
|
+
return {
|
|
32
|
+
...input,
|
|
33
|
+
maxAttempts,
|
|
34
|
+
retryStrategy: async () => {
|
|
35
|
+
if (retryStrategy) {
|
|
36
|
+
return retryStrategy;
|
|
37
|
+
}
|
|
38
|
+
const retryMode = await normalizeProvider(input.retryMode)();
|
|
39
|
+
if (retryMode === RETRY_MODES.ADAPTIVE) {
|
|
40
|
+
return new AdaptiveRetryStrategy(maxAttempts);
|
|
41
|
+
}
|
|
42
|
+
return new StandardRetryStrategy(maxAttempts);
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
export const ENV_RETRY_MODE = "AWS_RETRY_MODE";
|
|
47
|
+
export const CONFIG_RETRY_MODE = "retry_mode";
|
|
48
|
+
export const NODE_RETRY_MODE_CONFIG_OPTIONS = {
|
|
49
|
+
environmentVariableSelector: (env) => env[ENV_RETRY_MODE],
|
|
50
|
+
configFileSelector: (profile) => profile[CONFIG_RETRY_MODE],
|
|
51
|
+
default: DEFAULT_RETRY_MODE,
|
|
52
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { NO_RETRY_INCREMENT, RETRY_COST, TIMEOUT_RETRY_COST } from "@smithy/util-retry";
|
|
2
|
+
export const getDefaultRetryQuota = (initialRetryTokens, options) => {
|
|
3
|
+
const MAX_CAPACITY = initialRetryTokens;
|
|
4
|
+
const noRetryIncrement = options?.noRetryIncrement ?? NO_RETRY_INCREMENT;
|
|
5
|
+
const retryCost = options?.retryCost ?? RETRY_COST;
|
|
6
|
+
const timeoutRetryCost = options?.timeoutRetryCost ?? TIMEOUT_RETRY_COST;
|
|
7
|
+
let availableCapacity = initialRetryTokens;
|
|
8
|
+
const getCapacityAmount = (error) => (error.name === "TimeoutError" ? timeoutRetryCost : retryCost);
|
|
9
|
+
const hasRetryTokens = (error) => getCapacityAmount(error) <= availableCapacity;
|
|
10
|
+
const retrieveRetryTokens = (error) => {
|
|
11
|
+
if (!hasRetryTokens(error)) {
|
|
12
|
+
throw new Error("No retry token available");
|
|
13
|
+
}
|
|
14
|
+
const capacityAmount = getCapacityAmount(error);
|
|
15
|
+
availableCapacity -= capacityAmount;
|
|
16
|
+
return capacityAmount;
|
|
17
|
+
};
|
|
18
|
+
const releaseRetryTokens = (capacityReleaseAmount) => {
|
|
19
|
+
availableCapacity += capacityReleaseAmount ?? noRetryIncrement;
|
|
20
|
+
availableCapacity = Math.min(availableCapacity, MAX_CAPACITY);
|
|
21
|
+
};
|
|
22
|
+
return Object.freeze({
|
|
23
|
+
hasRetryTokens,
|
|
24
|
+
retrieveRetryTokens,
|
|
25
|
+
releaseRetryTokens,
|
|
26
|
+
});
|
|
27
|
+
};
|
package/dist-es/index.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./AdaptiveRetryStrategy";
|
|
2
|
+
export * from "./StandardRetryStrategy";
|
|
3
|
+
export * from "./configurations";
|
|
4
|
+
export * from "./delayDecider";
|
|
5
|
+
export * from "./omitRetryHeadersMiddleware";
|
|
6
|
+
export * from "./retryDecider";
|
|
7
|
+
export * from "./retryMiddleware";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { HttpRequest } from "@smithy/protocol-http";
|
|
2
|
+
import { INVOCATION_ID_HEADER, REQUEST_HEADER } from "@smithy/util-retry";
|
|
3
|
+
export const omitRetryHeadersMiddleware = () => (next) => async (args) => {
|
|
4
|
+
const { request } = args;
|
|
5
|
+
if (HttpRequest.isInstance(request)) {
|
|
6
|
+
delete request.headers[INVOCATION_ID_HEADER];
|
|
7
|
+
delete request.headers[REQUEST_HEADER];
|
|
8
|
+
}
|
|
9
|
+
return next(args);
|
|
10
|
+
};
|
|
11
|
+
export const omitRetryHeadersMiddlewareOptions = {
|
|
12
|
+
name: "omitRetryHeadersMiddleware",
|
|
13
|
+
tags: ["RETRY", "HEADERS", "OMIT_RETRY_HEADERS"],
|
|
14
|
+
relation: "before",
|
|
15
|
+
toMiddleware: "awsAuthMiddleware",
|
|
16
|
+
override: true,
|
|
17
|
+
};
|
|
18
|
+
export const getOmitRetryHeadersPlugin = (options) => ({
|
|
19
|
+
applyToStack: (clientStack) => {
|
|
20
|
+
clientStack.addRelativeTo(omitRetryHeadersMiddleware(), omitRetryHeadersMiddlewareOptions);
|
|
21
|
+
},
|
|
22
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { isClockSkewError, isRetryableByTrait, isThrottlingError, isTransientError, } from "@smithy/service-error-classification";
|
|
2
|
+
export const defaultRetryDecider = (error) => {
|
|
3
|
+
if (!error) {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
return isRetryableByTrait(error) || isClockSkewError(error) || isThrottlingError(error) || isTransientError(error);
|
|
7
|
+
};
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { HttpRequest, HttpResponse } from "@smithy/protocol-http";
|
|
2
|
+
import { isServerError, isThrottlingError, isTransientError } from "@smithy/service-error-classification";
|
|
3
|
+
import { INVOCATION_ID_HEADER, REQUEST_HEADER } from "@smithy/util-retry";
|
|
4
|
+
import { v4 } from "uuid";
|
|
5
|
+
import { asSdkError } from "./util";
|
|
6
|
+
export const retryMiddleware = (options) => (next, context) => async (args) => {
|
|
7
|
+
let retryStrategy = await options.retryStrategy();
|
|
8
|
+
const maxAttempts = await options.maxAttempts();
|
|
9
|
+
if (isRetryStrategyV2(retryStrategy)) {
|
|
10
|
+
retryStrategy = retryStrategy;
|
|
11
|
+
let retryToken = await retryStrategy.acquireInitialRetryToken(context["partition_id"]);
|
|
12
|
+
let lastError = new Error();
|
|
13
|
+
let attempts = 0;
|
|
14
|
+
let totalRetryDelay = 0;
|
|
15
|
+
const { request } = args;
|
|
16
|
+
if (HttpRequest.isInstance(request)) {
|
|
17
|
+
request.headers[INVOCATION_ID_HEADER] = v4();
|
|
18
|
+
}
|
|
19
|
+
while (true) {
|
|
20
|
+
try {
|
|
21
|
+
if (HttpRequest.isInstance(request)) {
|
|
22
|
+
request.headers[REQUEST_HEADER] = `attempt=${attempts + 1}; max=${maxAttempts}`;
|
|
23
|
+
}
|
|
24
|
+
const { response, output } = await next(args);
|
|
25
|
+
retryStrategy.recordSuccess(retryToken);
|
|
26
|
+
output.$metadata.attempts = attempts + 1;
|
|
27
|
+
output.$metadata.totalRetryDelay = totalRetryDelay;
|
|
28
|
+
return { response, output };
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
const retryErrorInfo = getRetryErrorInfo(e);
|
|
32
|
+
lastError = asSdkError(e);
|
|
33
|
+
try {
|
|
34
|
+
retryToken = await retryStrategy.refreshRetryTokenForRetry(retryToken, retryErrorInfo);
|
|
35
|
+
}
|
|
36
|
+
catch (refreshError) {
|
|
37
|
+
if (!lastError.$metadata) {
|
|
38
|
+
lastError.$metadata = {};
|
|
39
|
+
}
|
|
40
|
+
lastError.$metadata.attempts = attempts + 1;
|
|
41
|
+
lastError.$metadata.totalRetryDelay = totalRetryDelay;
|
|
42
|
+
throw lastError;
|
|
43
|
+
}
|
|
44
|
+
attempts = retryToken.getRetryCount();
|
|
45
|
+
const delay = retryToken.getRetryDelay();
|
|
46
|
+
totalRetryDelay += delay;
|
|
47
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
retryStrategy = retryStrategy;
|
|
53
|
+
if (retryStrategy?.mode)
|
|
54
|
+
context.userAgent = [...(context.userAgent || []), ["cfg/retry-mode", retryStrategy.mode]];
|
|
55
|
+
return retryStrategy.retry(next, args);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const isRetryStrategyV2 = (retryStrategy) => typeof retryStrategy.acquireInitialRetryToken !== "undefined" &&
|
|
59
|
+
typeof retryStrategy.refreshRetryTokenForRetry !== "undefined" &&
|
|
60
|
+
typeof retryStrategy.recordSuccess !== "undefined";
|
|
61
|
+
const getRetryErrorInfo = (error) => {
|
|
62
|
+
const errorInfo = {
|
|
63
|
+
errorType: getRetryErrorType(error),
|
|
64
|
+
};
|
|
65
|
+
const retryAfterHint = getRetryAfterHint(error.$response);
|
|
66
|
+
if (retryAfterHint) {
|
|
67
|
+
errorInfo.retryAfterHint = retryAfterHint;
|
|
68
|
+
}
|
|
69
|
+
return errorInfo;
|
|
70
|
+
};
|
|
71
|
+
const getRetryErrorType = (error) => {
|
|
72
|
+
if (isThrottlingError(error))
|
|
73
|
+
return "THROTTLING";
|
|
74
|
+
if (isTransientError(error))
|
|
75
|
+
return "TRANSIENT";
|
|
76
|
+
if (isServerError(error))
|
|
77
|
+
return "SERVER_ERROR";
|
|
78
|
+
return "CLIENT_ERROR";
|
|
79
|
+
};
|
|
80
|
+
export const retryMiddlewareOptions = {
|
|
81
|
+
name: "retryMiddleware",
|
|
82
|
+
tags: ["RETRY"],
|
|
83
|
+
step: "finalizeRequest",
|
|
84
|
+
priority: "high",
|
|
85
|
+
override: true,
|
|
86
|
+
};
|
|
87
|
+
export const getRetryPlugin = (options) => ({
|
|
88
|
+
applyToStack: (clientStack) => {
|
|
89
|
+
clientStack.add(retryMiddleware(options), retryMiddlewareOptions);
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
export const getRetryAfterHint = (response) => {
|
|
93
|
+
if (!HttpResponse.isInstance(response))
|
|
94
|
+
return;
|
|
95
|
+
const retryAfterHeaderName = Object.keys(response.headers).find((key) => key.toLowerCase() === "retry-after");
|
|
96
|
+
if (!retryAfterHeaderName)
|
|
97
|
+
return;
|
|
98
|
+
const retryAfter = response.headers[retryAfterHeaderName];
|
|
99
|
+
const retryAfterSeconds = Number(retryAfter);
|
|
100
|
+
if (!Number.isNaN(retryAfterSeconds))
|
|
101
|
+
return new Date(retryAfterSeconds * 1000);
|
|
102
|
+
const retryAfterDate = new Date(retryAfter);
|
|
103
|
+
return retryAfterDate;
|
|
104
|
+
};
|
package/dist-es/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist-es/util.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const asSdkError = (error) => {
|
|
2
|
+
if (error instanceof Error)
|
|
3
|
+
return error;
|
|
4
|
+
if (error instanceof Object)
|
|
5
|
+
return Object.assign(new Error(), error);
|
|
6
|
+
if (typeof error === "string")
|
|
7
|
+
return new Error(error);
|
|
8
|
+
return new Error(`AWS SDK error wrapper for ${error}`);
|
|
9
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { FinalizeHandler, FinalizeHandlerArguments, MetadataBearer, Provider } from "@smithy/types";
|
|
2
|
+
import { RateLimiter } from "@smithy/util-retry";
|
|
3
|
+
import { StandardRetryStrategy, StandardRetryStrategyOptions } from "./StandardRetryStrategy";
|
|
4
|
+
/**
|
|
5
|
+
* Strategy options to be passed to AdaptiveRetryStrategy
|
|
6
|
+
*/
|
|
7
|
+
export interface AdaptiveRetryStrategyOptions extends StandardRetryStrategyOptions {
|
|
8
|
+
rateLimiter?: RateLimiter;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* @deprecated use AdaptiveRetryStrategy from @smithy/util-retry
|
|
12
|
+
*/
|
|
13
|
+
export declare class AdaptiveRetryStrategy extends StandardRetryStrategy {
|
|
14
|
+
private rateLimiter;
|
|
15
|
+
constructor(maxAttemptsProvider: Provider<number>, options?: AdaptiveRetryStrategyOptions);
|
|
16
|
+
retry<Input extends object, Ouput extends MetadataBearer>(next: FinalizeHandler<Input, Ouput>, args: FinalizeHandlerArguments<Input>): Promise<{
|
|
17
|
+
response: unknown;
|
|
18
|
+
output: Ouput;
|
|
19
|
+
}>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { FinalizeHandler, FinalizeHandlerArguments, MetadataBearer, Provider, RetryStrategy } from "@smithy/types";
|
|
2
|
+
import { DelayDecider, RetryDecider, RetryQuota } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Strategy options to be passed to StandardRetryStrategy
|
|
5
|
+
*/
|
|
6
|
+
export interface StandardRetryStrategyOptions {
|
|
7
|
+
retryDecider?: RetryDecider;
|
|
8
|
+
delayDecider?: DelayDecider;
|
|
9
|
+
retryQuota?: RetryQuota;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated use StandardRetryStrategy from @smithy/util-retry
|
|
13
|
+
*/
|
|
14
|
+
export declare class StandardRetryStrategy implements RetryStrategy {
|
|
15
|
+
private readonly maxAttemptsProvider;
|
|
16
|
+
private retryDecider;
|
|
17
|
+
private delayDecider;
|
|
18
|
+
private retryQuota;
|
|
19
|
+
mode: string;
|
|
20
|
+
constructor(maxAttemptsProvider: Provider<number>, options?: StandardRetryStrategyOptions);
|
|
21
|
+
private shouldRetry;
|
|
22
|
+
private getMaxAttempts;
|
|
23
|
+
retry<Input extends object, Ouput extends MetadataBearer>(next: FinalizeHandler<Input, Ouput>, args: FinalizeHandlerArguments<Input>, options?: {
|
|
24
|
+
beforeRequest: Function;
|
|
25
|
+
afterRequest: Function;
|
|
26
|
+
}): Promise<{
|
|
27
|
+
response: unknown;
|
|
28
|
+
output: Ouput;
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { LoadedConfigSelectors } from "@smithy/node-config-provider";
|
|
2
|
+
import { Provider, RetryStrategy, RetryStrategyV2 } from "@smithy/types";
|
|
3
|
+
export declare const ENV_MAX_ATTEMPTS = "AWS_MAX_ATTEMPTS";
|
|
4
|
+
export declare const CONFIG_MAX_ATTEMPTS = "max_attempts";
|
|
5
|
+
export declare const NODE_MAX_ATTEMPT_CONFIG_OPTIONS: LoadedConfigSelectors<number>;
|
|
6
|
+
export interface RetryInputConfig {
|
|
7
|
+
/**
|
|
8
|
+
* The maximum number of times requests that encounter retryable failures should be attempted.
|
|
9
|
+
*/
|
|
10
|
+
maxAttempts?: number | Provider<number>;
|
|
11
|
+
/**
|
|
12
|
+
* The strategy to retry the request. Using built-in exponential backoff strategy by default.
|
|
13
|
+
*/
|
|
14
|
+
retryStrategy?: RetryStrategy | RetryStrategyV2;
|
|
15
|
+
}
|
|
16
|
+
interface PreviouslyResolved {
|
|
17
|
+
/**
|
|
18
|
+
* Specifies provider for retry algorithm to use.
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
retryMode: string | Provider<string>;
|
|
22
|
+
}
|
|
23
|
+
export interface RetryResolvedConfig {
|
|
24
|
+
/**
|
|
25
|
+
* Resolved value for input config {@link RetryInputConfig.maxAttempts}
|
|
26
|
+
*/
|
|
27
|
+
maxAttempts: Provider<number>;
|
|
28
|
+
/**
|
|
29
|
+
* Resolved value for input config {@link RetryInputConfig.retryStrategy}
|
|
30
|
+
*/
|
|
31
|
+
retryStrategy: Provider<RetryStrategyV2 | RetryStrategy>;
|
|
32
|
+
}
|
|
33
|
+
export declare const resolveRetryConfig: <T>(input: T & PreviouslyResolved & RetryInputConfig) => T & RetryResolvedConfig;
|
|
34
|
+
export declare const ENV_RETRY_MODE = "AWS_RETRY_MODE";
|
|
35
|
+
export declare const CONFIG_RETRY_MODE = "retry_mode";
|
|
36
|
+
export declare const NODE_RETRY_MODE_CONFIG_OPTIONS: LoadedConfigSelectors<string>;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RetryQuota } from "./types";
|
|
2
|
+
export interface DefaultRetryQuotaOptions {
|
|
3
|
+
/**
|
|
4
|
+
* The total amount of retry token to be incremented from retry token balance
|
|
5
|
+
* if an SDK operation invocation succeeds without requiring a retry request.
|
|
6
|
+
*/
|
|
7
|
+
noRetryIncrement?: number;
|
|
8
|
+
/**
|
|
9
|
+
* The total amount of retry tokens to be decremented from retry token balance.
|
|
10
|
+
*/
|
|
11
|
+
retryCost?: number;
|
|
12
|
+
/**
|
|
13
|
+
* The total amount of retry tokens to be decremented from retry token balance
|
|
14
|
+
* when a throttling error is encountered.
|
|
15
|
+
*/
|
|
16
|
+
timeoutRetryCost?: number;
|
|
17
|
+
}
|
|
18
|
+
export declare const getDefaultRetryQuota: (initialRetryTokens: number, options?: DefaultRetryQuotaOptions) => RetryQuota;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from "./AdaptiveRetryStrategy";
|
|
2
|
+
export * from "./StandardRetryStrategy";
|
|
3
|
+
export * from "./configurations";
|
|
4
|
+
export * from "./delayDecider";
|
|
5
|
+
export * from "./omitRetryHeadersMiddleware";
|
|
6
|
+
export * from "./retryDecider";
|
|
7
|
+
export * from "./retryMiddleware";
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { FinalizeHandler, MetadataBearer, Pluggable, RelativeMiddlewareOptions } from "@smithy/types";
|
|
2
|
+
export declare const omitRetryHeadersMiddleware: () => <Output extends MetadataBearer = MetadataBearer>(next: FinalizeHandler<any, Output>) => FinalizeHandler<any, Output>;
|
|
3
|
+
export declare const omitRetryHeadersMiddlewareOptions: RelativeMiddlewareOptions;
|
|
4
|
+
export declare const getOmitRetryHeadersPlugin: (options: unknown) => Pluggable<any, any>;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { AbsoluteLocation, FinalizeHandler, FinalizeRequestHandlerOptions, HandlerExecutionContext, MetadataBearer, Pluggable } from "@smithy/types";
|
|
2
|
+
import { RetryResolvedConfig } from "./configurations";
|
|
3
|
+
export declare const retryMiddleware: (options: RetryResolvedConfig) => <Output extends MetadataBearer = MetadataBearer>(next: FinalizeHandler<any, Output>, context: HandlerExecutionContext) => FinalizeHandler<any, Output>;
|
|
4
|
+
export declare const retryMiddlewareOptions: FinalizeRequestHandlerOptions & AbsoluteLocation;
|
|
5
|
+
export declare const getRetryPlugin: (options: RetryResolvedConfig) => Pluggable<any, any>;
|
|
6
|
+
export declare const getRetryAfterHint: (response: unknown) => Date | undefined;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { SdkError } from "@smithy/types";
|
|
2
|
+
/**
|
|
3
|
+
* Determines whether an error is retryable based on the number of retries
|
|
4
|
+
* already attempted, the HTTP status code, and the error received (if any).
|
|
5
|
+
*
|
|
6
|
+
* @param error - The error encountered.
|
|
7
|
+
*/
|
|
8
|
+
export interface RetryDecider {
|
|
9
|
+
(error: SdkError): boolean;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Determines the number of milliseconds to wait before retrying an action.
|
|
13
|
+
*
|
|
14
|
+
* @param delayBase - The base delay (in milliseconds).
|
|
15
|
+
* @param attempts - The number of times the action has already been tried.
|
|
16
|
+
*/
|
|
17
|
+
export interface DelayDecider {
|
|
18
|
+
(delayBase: number, attempts: number): number;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Interface that specifies the retry quota behavior.
|
|
22
|
+
*/
|
|
23
|
+
export interface RetryQuota {
|
|
24
|
+
/**
|
|
25
|
+
* returns true if retry tokens are available from the retry quota bucket.
|
|
26
|
+
*/
|
|
27
|
+
hasRetryTokens: (error: SdkError) => boolean;
|
|
28
|
+
/**
|
|
29
|
+
* returns token amount from the retry quota bucket.
|
|
30
|
+
* throws error is retry tokens are not available.
|
|
31
|
+
*/
|
|
32
|
+
retrieveRetryTokens: (error: SdkError) => number;
|
|
33
|
+
/**
|
|
34
|
+
* releases tokens back to the retry quota.
|
|
35
|
+
*/
|
|
36
|
+
releaseRetryTokens: (releaseCapacityAmount?: number) => void;
|
|
37
|
+
}
|
|
38
|
+
export interface RateLimiter {
|
|
39
|
+
/**
|
|
40
|
+
* If there is sufficient capacity (tokens) available, it immediately returns.
|
|
41
|
+
* If there is not sufficient capacity, it will either sleep a certain amount
|
|
42
|
+
* of time until the rate limiter can retrieve a token from its token bucket
|
|
43
|
+
* or raise an exception indicating there is insufficient capacity.
|
|
44
|
+
*/
|
|
45
|
+
getSendToken: () => Promise<void>;
|
|
46
|
+
/**
|
|
47
|
+
* Updates the client sending rate based on response.
|
|
48
|
+
* If the response was successful, the capacity and fill rate are increased.
|
|
49
|
+
* If the response was a throttling response, the capacity and fill rate are
|
|
50
|
+
* decreased. Transient errors do not affect the rate limiter.
|
|
51
|
+
*/
|
|
52
|
+
updateClientSendingRate: (response: any) => void;
|
|
53
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@smithy/middleware-retry",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"scripts": {
|
|
5
|
+
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types'",
|
|
6
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
7
|
+
"build:es": "tsc -p tsconfig.es.json",
|
|
8
|
+
"build:types": "tsc -p tsconfig.types.json",
|
|
9
|
+
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
|
|
10
|
+
"stage-release": "rimraf ./.release && yarn pack && mkdir ./.release && tar zxvf ./package.tgz --directory ./.release && rm ./package.tgz",
|
|
11
|
+
"clean": "rimraf ./dist-* && rimraf *.tsbuildinfo",
|
|
12
|
+
"lint": "eslint -c ../../.eslintrc.js \"src/**/*.ts\"",
|
|
13
|
+
"format": "prettier --config ../../prettier.config.js --ignore-path ../.prettierignore --write \"**/*.{ts,md,json}\"",
|
|
14
|
+
"extract:docs": "api-extractor run --local",
|
|
15
|
+
"test": "jest"
|
|
16
|
+
},
|
|
17
|
+
"main": "./dist-cjs/index.js",
|
|
18
|
+
"module": "./dist-es/index.js",
|
|
19
|
+
"types": "./dist-types/index.d.ts",
|
|
20
|
+
"author": {
|
|
21
|
+
"name": "AWS SDK for JavaScript Team",
|
|
22
|
+
"url": "https://aws.amazon.com/javascript/"
|
|
23
|
+
},
|
|
24
|
+
"license": "Apache-2.0",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@smithy/protocol-http": "workspace:^",
|
|
27
|
+
"@smithy/service-error-classification": "workspace:^",
|
|
28
|
+
"@smithy/types": "workspace:^",
|
|
29
|
+
"@smithy/util-middleware": "workspace:^",
|
|
30
|
+
"@smithy/util-retry": "workspace:^",
|
|
31
|
+
"tslib": "^2.5.0",
|
|
32
|
+
"uuid": "^8.3.2"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@smithy/node-config-provider": "workspace:^",
|
|
36
|
+
"@tsconfig/recommended": "1.0.1",
|
|
37
|
+
"@types/uuid": "^8.3.0",
|
|
38
|
+
"concurrently": "7.0.0",
|
|
39
|
+
"downlevel-dts": "0.10.1",
|
|
40
|
+
"jest": "28.1.1",
|
|
41
|
+
"rimraf": "3.0.2",
|
|
42
|
+
"typedoc": "0.23.23",
|
|
43
|
+
"typescript": "~4.9.5"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=14.0.0"
|
|
47
|
+
},
|
|
48
|
+
"typesVersions": {
|
|
49
|
+
"<4.0": {
|
|
50
|
+
"dist-types/*": [
|
|
51
|
+
"dist-types/ts3.4/*"
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"files": [
|
|
56
|
+
"dist-*/**"
|
|
57
|
+
],
|
|
58
|
+
"homepage": "https://github.com/awslabs/smithy-typescript/tree/main/packages/middleware-retry",
|
|
59
|
+
"repository": {
|
|
60
|
+
"type": "git",
|
|
61
|
+
"url": "https://github.com/awslabs/smithy-typescript.git",
|
|
62
|
+
"directory": "packages/middleware-retry"
|
|
63
|
+
},
|
|
64
|
+
"typedoc": {
|
|
65
|
+
"entryPoint": "src/index.ts"
|
|
66
|
+
}
|
|
67
|
+
}
|