@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
84 lines (83 loc) • 3.31 kB
JavaScript
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 sessionRetryPolicy_exports = {};
__export(sessionRetryPolicy_exports, {
SessionRetryPolicy: () => SessionRetryPolicy
});
module.exports = __toCommonJS(sessionRetryPolicy_exports);
var import_common = require("../common/index.js");
class SessionRetryPolicy {
/**
* @param globalEndpointManager - The GlobalEndpointManager instance.
*/
constructor(globalEndpointManager, resourceType, operationType, connectionPolicy) {
this.globalEndpointManager = globalEndpointManager;
this.resourceType = resourceType;
this.operationType = operationType;
this.connectionPolicy = connectionPolicy;
}
/** Current retry attempt count. */
currentRetryAttemptCount = 0;
/** Retry interval in milliseconds. */
retryAfterInMs = 0;
/**
* Determines whether the request should be retried or not.
* @param err - Error returned by the request.
* @param callback - The callback function which takes bool argument which specifies whether the request
* will be retried or not.
*/
async shouldRetry(err, diagnosticNode, retryContext) {
if (!err) {
return false;
}
if (!retryContext) {
return false;
}
if (!this.connectionPolicy.enableEndpointDiscovery) {
return false;
}
if (this.globalEndpointManager.canUseMultipleWriteLocations(this.resourceType, this.operationType)) {
const endpoints = (0, import_common.isReadRequest)(this.operationType) ? await this.globalEndpointManager.getReadEndpoints() : await this.globalEndpointManager.getWriteEndpoints();
if (this.currentRetryAttemptCount > endpoints.length) {
return false;
} else {
this.currentRetryAttemptCount++;
retryContext.retryCount++;
retryContext.retryRequestOnPreferredLocations = this.currentRetryAttemptCount > 1;
retryContext.clearSessionTokenNotAvailable = this.currentRetryAttemptCount === endpoints.length;
diagnosticNode.addData({ successfulRetryPolicy: "session" });
return true;
}
} else {
if (this.currentRetryAttemptCount > 1) {
return false;
} else {
this.currentRetryAttemptCount++;
retryContext.retryCount++;
retryContext.retryRequestOnPreferredLocations = false;
retryContext.clearSessionTokenNotAvailable = true;
diagnosticNode.addData({ successfulRetryPolicy: "session" });
return true;
}
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SessionRetryPolicy
});