UNPKG

@azure/cosmos

Version:
78 lines (77 loc) 3.39 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var resourceThrottleRetryPolicy_exports = {}; __export(resourceThrottleRetryPolicy_exports, { ResourceThrottleRetryPolicy: () => ResourceThrottleRetryPolicy }); module.exports = __toCommonJS(resourceThrottleRetryPolicy_exports); var import_constants = require("../common/constants.js"); class ResourceThrottleRetryPolicy { /** Current retry attempt count. */ currentRetryAttemptCount = 0; /** Cummulative wait time in milliseconds for a request while the retries are happening. */ cummulativeWaitTimeinMs = 0; /** Retry interval in milliseconds to wait before the next request will be sent. */ retryAfterInMs = 0; /** Max wait time in milliseconds to wait for a request while the retries are happening. */ timeoutInMs; /** * @param maxTries - Max number of retries to be performed for a request. * @param fixedRetryIntervalInMs - Fixed retry interval in milliseconds to wait between each * retry ignoring the retryAfter returned as part of the response. * @param timeoutInSeconds - Max wait time in seconds to wait for a request while the * retries are happening. */ maxTries; fixedRetryIntervalInMs; constructor(options) { this.maxTries = options.maxRetryAttemptCount ?? import_constants.Constants.ThrottledRequestMaxRetryAttemptCount; this.fixedRetryIntervalInMs = options.fixedRetryIntervalInMilliseconds ?? import_constants.Constants.ThrottledRequestFixedRetryIntervalInMs; const timeoutInSeconds = options.maxWaitTimeInSeconds ?? import_constants.Constants.ThrottledRequestMaxWaitTimeInSeconds; this.timeoutInMs = timeoutInSeconds * 1e3; this.currentRetryAttemptCount = 0; this.cummulativeWaitTimeinMs = 0; } /** * Determines whether the request should be retried or not. * @param err - Error returned by the request. */ async shouldRetry(err, diagnosticNode) { if (err) { if (this.currentRetryAttemptCount < this.maxTries) { this.currentRetryAttemptCount++; this.retryAfterInMs = 0; if (this.fixedRetryIntervalInMs) { this.retryAfterInMs = this.fixedRetryIntervalInMs; } else if (err.retryAfterInMs) { this.retryAfterInMs = err.retryAfterInMs; } if (this.cummulativeWaitTimeinMs < this.timeoutInMs) { this.cummulativeWaitTimeinMs += this.retryAfterInMs; diagnosticNode.addData({ successfulRetryPolicy: "resourceThrottle" }); return true; } } } return false; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ResourceThrottleRetryPolicy });