@azure/core-rest-pipeline
Version:
Isomorphic client library for making HTTP requests in node.js and browser.
69 lines • 3.1 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.exponentialRetryStrategy = exponentialRetryStrategy;
exports.isExponentialRetryResponse = isExponentialRetryResponse;
exports.isSystemError = isSystemError;
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;
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 };
}
return (0, core_util_1.calculateRetryDelay)(retryCount, {
retryDelayInMs: retryInterval,
maxRetryDelayInMs: maxRetryInterval,
});
},
};
}
/**
* 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);
}
/**
* 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");
}
//# sourceMappingURL=exponentialRetryStrategy.js.map
;