"use strict"; // Copyright (c) Microsoft Corporation. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.isSystemError = exports.isExponentialRetryResponse = exports.exponentialRetryStrategy = void 0; const core_util_1 = require("@azure/core-util"); const throttlingRetryStrategy_js_1 = require("./throttlingRetryStrategy.js"); // intervals are in milliseconds const DEFAULT_CLIENT_RETRY_INTERVAL = 1000; const DEFAULT_CLIENT_MAX_RETRY_INTERVAL = 1000 * 64; /** * A retry strategy that retries with an exponentially increasing delay in these two cases: * - When there are errors in the underlying transport layer (e.g. DNS lookup failures). * - Or otherwise if the outgoing request fails (408, greater or equal than 500, except for 501 and 505). */ function exponentialRetryStrategy(options = {}) { var _a, _b; const retryInterval = (_a = options.retryDelayInMs) !== null && _a !== void 0 ? _a : DEFAULT_CLIENT_RETRY_INTERVAL; const maxRetryInterval = (_b = options.maxRetryDelayInMs) !== null && _b !== void 0 ? _b : DEFAULT_CLIENT_MAX_RETRY_INTERVAL; let retryAfterInMs = retryInterval; return { name: "exponentialRetryStrategy", retry({ retryCount, response, responseError }) { const matchedSystemError = isSystemError(responseError); const ignoreSystemErrors = matchedSystemError && options.ignoreSystemErrors; const isExponential = isExponentialRetryResponse(response); const ignoreExponentialResponse = isExponential && options.ignoreHttpStatusCodes; const unknownResponse = response && ((0, throttlingRetryStrategy_js_1.isThrottlingRetryResponse)(response) || !isExponential); if (unknownResponse || ignoreExponentialResponse || ignoreSystemErrors) { return { skipStrategy: true }; } if (responseError && !matchedSystemError && !isExponential) { return { errorToThrow: responseError }; } // Exponentially increase the delay each time const exponentialDelay = retryAfterInMs * Math.pow(2, retryCount); // Don't let the delay exceed the maximum const clampedExponentialDelay = Math.min(maxRetryInterval, exponentialDelay); // Allow the final value to have some "jitter" (within 50% of the delay size) so // that retries across multiple clients don't occur simultaneously. retryAfterInMs = clampedExponentialDelay / 2 + (0, core_util_1.getRandomIntegerInclusive)(0, clampedExponentialDelay / 2); return { retryAfterInMs }; }, }; } exports.exponentialRetryStrategy = exponentialRetryStrategy; /** * A response is a retry response if it has status codes: * - 408, or * - Greater or equal than 500, except for 501 and 505. */ function isExponentialRetryResponse(response) { return Boolean(response && response.status !== undefined && (response.status >= 500 || response.status === 408) && response.status !== 501 && response.status !== 505); } exports.isExponentialRetryResponse = isExponentialRetryResponse; /** * Determines whether an error from a pipeline response was triggered in the network layer. */ function isSystemError(err) { if (!err) { return false; } return (err.code === "ETIMEDOUT" || err.code === "ESOCKETTIMEDOUT" || err.code === "ECONNREFUSED" || err.code === "ECONNRESET" || err.code === "ENOENT" || err.code === "ENOTFOUND"); } exports.isSystemError = isSystemError; //# sourceMappingURL=exponentialRetryStrategy.js.map