UNPKG

1.7 kBJavaScriptView Raw
1import { NO_RETRY_INCREMENT, RETRY_COST, TIMEOUT_RETRY_COST } from "./constants";
2export var getDefaultRetryQuota = function (initialRetryTokens, options) {
3 var _a, _b, _c;
4 var MAX_CAPACITY = initialRetryTokens;
5 var noRetryIncrement = (_a = options === null || options === void 0 ? void 0 : options.noRetryIncrement) !== null && _a !== void 0 ? _a : NO_RETRY_INCREMENT;
6 var retryCost = (_b = options === null || options === void 0 ? void 0 : options.retryCost) !== null && _b !== void 0 ? _b : RETRY_COST;
7 var timeoutRetryCost = (_c = options === null || options === void 0 ? void 0 : options.timeoutRetryCost) !== null && _c !== void 0 ? _c : TIMEOUT_RETRY_COST;
8 var availableCapacity = initialRetryTokens;
9 var getCapacityAmount = function (error) { return (error.name === "TimeoutError" ? timeoutRetryCost : retryCost); };
10 var hasRetryTokens = function (error) { return getCapacityAmount(error) <= availableCapacity; };
11 var retrieveRetryTokens = function (error) {
12 if (!hasRetryTokens(error)) {
13 throw new Error("No retry token available");
14 }
15 var capacityAmount = getCapacityAmount(error);
16 availableCapacity -= capacityAmount;
17 return capacityAmount;
18 };
19 var releaseRetryTokens = function (capacityReleaseAmount) {
20 availableCapacity += capacityReleaseAmount !== null && capacityReleaseAmount !== void 0 ? capacityReleaseAmount : noRetryIncrement;
21 availableCapacity = Math.min(availableCapacity, MAX_CAPACITY);
22 };
23 return Object.freeze({
24 hasRetryTokens: hasRetryTokens,
25 retrieveRetryTokens: retrieveRetryTokens,
26 releaseRetryTokens: releaseRetryTokens,
27 });
28};