@softchef/cdk-iot-device-management
Version:
IoT device management is composed of things, thing types, thing groups, jobs, files API services. The constructs can be used independently, that are based on full-managed service to create an API Gateway & Lambda function.
29 lines (28 loc) • 1.7 kB
JavaScript
import { NO_RETRY_INCREMENT, RETRY_COST, TIMEOUT_RETRY_COST } from "./constants";
export var getDefaultRetryQuota = function (initialRetryTokens, options) {
var _a, _b, _c;
var MAX_CAPACITY = initialRetryTokens;
var noRetryIncrement = (_a = options === null || options === void 0 ? void 0 : options.noRetryIncrement) !== null && _a !== void 0 ? _a : NO_RETRY_INCREMENT;
var retryCost = (_b = options === null || options === void 0 ? void 0 : options.retryCost) !== null && _b !== void 0 ? _b : RETRY_COST;
var timeoutRetryCost = (_c = options === null || options === void 0 ? void 0 : options.timeoutRetryCost) !== null && _c !== void 0 ? _c : TIMEOUT_RETRY_COST;
var availableCapacity = initialRetryTokens;
var getCapacityAmount = function (error) { return (error.name === "TimeoutError" ? timeoutRetryCost : retryCost); };
var hasRetryTokens = function (error) { return getCapacityAmount(error) <= availableCapacity; };
var retrieveRetryTokens = function (error) {
if (!hasRetryTokens(error)) {
throw new Error("No retry token available");
}
var capacityAmount = getCapacityAmount(error);
availableCapacity -= capacityAmount;
return capacityAmount;
};
var releaseRetryTokens = function (capacityReleaseAmount) {
availableCapacity += capacityReleaseAmount !== null && capacityReleaseAmount !== void 0 ? capacityReleaseAmount : noRetryIncrement;
availableCapacity = Math.min(availableCapacity, MAX_CAPACITY);
};
return Object.freeze({
hasRetryTokens: hasRetryTokens,
retrieveRetryTokens: retrieveRetryTokens,
releaseRetryTokens: releaseRetryTokens,
});
};