UNPKG

1.78 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getDefaultRetryQuota = void 0;
4const util_retry_1 = require("@aws-sdk/util-retry");
5const 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};
32exports.getDefaultRetryQuota = getDefaultRetryQuota;