UNPKG

4.74 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.StandardRetryStrategy = void 0;
4const protocol_http_1 = require("@aws-sdk/protocol-http");
5const service_error_classification_1 = require("@aws-sdk/service-error-classification");
6const util_retry_1 = require("@aws-sdk/util-retry");
7const uuid_1 = require("uuid");
8const defaultRetryQuota_1 = require("./defaultRetryQuota");
9const delayDecider_1 = require("./delayDecider");
10const retryDecider_1 = require("./retryDecider");
11const util_1 = require("./util");
12class 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}
82exports.StandardRetryStrategy = StandardRetryStrategy;
83const 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};