diff --git a/sdk/core/core-amqp/src/ConnectionContextBase.ts b/sdk/core/core-amqp/src/ConnectionContextBase.ts index 6cf827519c46..f095693dff6d 100644 --- a/sdk/core/core-amqp/src/ConnectionContextBase.ts +++ b/sdk/core/core-amqp/src/ConnectionContextBase.ts @@ -117,11 +117,11 @@ export interface CreateConnectionContextBaseParameters { */ isEntityPathRequired?: boolean; /** - * @property {number} [operationTimeoutInSeconds] - The duration in which the promise should + * @property {number} [operationTimeoutInMs] - The duration in which the promise should * complete (resolve/reject). If it is not completed, then the Promise will be rejected after - * timeout occurs. Default: `60 seconds`. + * timeout occurs. Default: `60000 milliseconds`. */ - operationTimeoutInSeconds?: number; + operationTimeoutInMs?: number; } export module ConnectionContextBase { @@ -156,7 +156,9 @@ export module ConnectionContextBase { platform: `(${os.arch()}-${os.type()}-${os.release()})`, framework: `Node/${process.version}` }, - operationTimeoutInSeconds: parameters.operationTimeoutInSeconds + operationTimeoutInSeconds: parameters.operationTimeoutInMs + ? parameters.operationTimeoutInMs / 1000 + : undefined }; if ( diff --git a/sdk/core/core-amqp/src/requestResponseLink.ts b/sdk/core/core-amqp/src/requestResponseLink.ts index e7fa21cf4576..cd74a547309e 100644 --- a/sdk/core/core-amqp/src/requestResponseLink.ts +++ b/sdk/core/core-amqp/src/requestResponseLink.ts @@ -29,10 +29,10 @@ export interface SendRequestOptions { */ abortSignal?: AbortSignalLike; /** - * @property {number} [timeoutInSeconds] Max time to wait for the operation to complete. - * Default: `60 seconds`. + * @property {number} [timeoutInMs] Max time to wait for the operation to complete. + * Default: `60000 milliseconds`. */ - timeoutInSeconds?: number; + timeoutInMs?: number; /** * @property {string} [requestName] Name of the request being performed. */ @@ -74,20 +74,18 @@ export class RequestResponseLink implements ReqResLink { /** * Sends the given request message and returns the received response. If the operation is not - * completed in the provided timeout in seconds `default: 60`, then `OperationTimeoutError` is thrown. + * completed in the provided timeout in milliseconds `default: 60000`, then `OperationTimeoutError` is thrown. * * @param {Message} request The AMQP (request) message. * @param {SendRequestOptions} [options] Options that can be provided while sending a request. * @returns {Promise} Promise The AMQP (response) message. */ - sendRequest(request: AmqpMessage, options?: SendRequestOptions): Promise { - if (!options) options = {}; - - if (!options.timeoutInSeconds) { - options.timeoutInSeconds = Constants.defaultOperationTimeoutInSeconds; + sendRequest(request: AmqpMessage, options: SendRequestOptions = {}): Promise { + if (!options.timeoutInMs) { + options.timeoutInMs = Constants.defaultOperationTimeoutInMs; } - const aborter: AbortSignalLike | undefined = options && options.abortSignal; + const aborter: AbortSignalLike | undefined = options.abortSignal; return new Promise((resolve: any, reject: any) => { let waitTimer: any; @@ -100,7 +98,7 @@ export class RequestResponseLink implements ReqResLink { const rejectOnAbort = () => { const address = this.receiver.address || "address"; - const requestName = options!.requestName; + const requestName = options.requestName; const desc: string = `[${this.connection.id}] The request "${requestName}" ` + `to "${address}" has been cancelled by the user.`; @@ -212,7 +210,7 @@ export class RequestResponseLink implements ReqResLink { return reject(translate(e)); }; - waitTimer = setTimeout(actionAfterTimeout, options!.timeoutInSeconds! * 1000); + waitTimer = setTimeout(actionAfterTimeout, options.timeoutInMs); this.receiver.on(ReceiverEvents.message, messageCallback); log.reqres( diff --git a/sdk/core/core-amqp/src/retry.ts b/sdk/core/core-amqp/src/retry.ts index 1662245ed7d3..130e88fd0b6d 100644 --- a/sdk/core/core-amqp/src/retry.ts +++ b/sdk/core/core-amqp/src/retry.ts @@ -6,7 +6,7 @@ import { delay, isNode } from "./util/utils"; import * as log from "./log"; import { defaultMaxRetries, - defaultDelayBetweenOperationRetriesInSeconds, + defaultDelayBetweenOperationRetriesInMs, defaultMaxDelayForExponentialRetryInMs, defaultMinDelayForExponentialRetryInMs } from "./util/constants"; @@ -79,12 +79,12 @@ export interface RetryConfig { */ maxRetries?: number; /** - * @property {number} [delayInSeconds] Amount of time to wait in seconds before making the - * next attempt. Default: 30. + * @property {number} [delayInMs] Amount of time to wait in milliseconds before making the + * next attempt. Default: `30000 milliseconds`. * When `retryPolicy` option is set to `ExponentialRetryPolicy`, \ * this is used to compute the exponentially increasing delays between retries. */ - delayInSeconds?: number; + delayInMs?: number; /** * @property {string} connectionHost The host ".servicebus.windows.net". * Used to check network connectivity. @@ -160,8 +160,8 @@ export async function retry(config: RetryConfig): Promise { if (config.maxRetries == undefined || config.maxRetries < 0) { config.maxRetries = defaultMaxRetries; } - if (config.delayInSeconds == undefined || config.delayInSeconds < 0) { - config.delayInSeconds = defaultDelayBetweenOperationRetriesInSeconds; + if (config.delayInMs == undefined || config.delayInMs < 0) { + config.delayInMs = defaultDelayBetweenOperationRetriesInMs; } if (config.maxExponentialRetryDelayInMs == undefined || config.maxExponentialRetryDelayInMs < 0) { config.maxExponentialRetryDelayInMs = defaultMaxDelayForExponentialRetryInMs; @@ -216,12 +216,12 @@ export async function retry(config: RetryConfig): Promise { i, err ); - let targetDelayInMs = config.delayInSeconds; + let targetDelayInMs = config.delayInMs; if (config.retryPolicy === RetryPolicy.ExponentialRetryPolicy) { let incrementDelta = Math.pow(2, i) - 1; const boundedRandDelta = - config.delayInSeconds * 0.8 + - Math.floor(Math.random() * (config.delayInSeconds * 1.2 - config.delayInSeconds * 0.8)); + config.delayInMs * 0.8 + + Math.floor(Math.random() * (config.delayInMs * 1.2 - config.delayInMs * 0.8)); incrementDelta *= boundedRandDelta; targetDelayInMs = Math.min( @@ -232,9 +232,9 @@ export async function retry(config: RetryConfig): Promise { if (lastError && lastError.retryable) { log.error( - "[%s] Sleeping for %d seconds for '%s'.", + "[%s] Sleeping for %d milliseconds for '%s'.", config.connectionId, - targetDelayInMs / 1000, + targetDelayInMs, config.operationType ); await delay(targetDelayInMs); diff --git a/sdk/core/core-amqp/src/util/constants.ts b/sdk/core/core-amqp/src/util/constants.ts index f1992189b189..4a0201408efe 100644 --- a/sdk/core/core-amqp/src/util/constants.ts +++ b/sdk/core/core-amqp/src/util/constants.ts @@ -50,7 +50,7 @@ export const receiverError = "receiver_error"; export const senderError = "sender_error"; export const sessionError = "session_error"; export const connectionError = "connection_error"; -export const defaultOperationTimeoutInSeconds = 60; +export const defaultOperationTimeoutInMs = 60000; export const managementRequestKey = "managementRequest"; export const negotiateCbsKey = "negotiateCbs"; export const negotiateClaim = "negotiateClaim"; @@ -71,13 +71,13 @@ export const maxDurationValue = 922337203685477; export const minDurationValue = -922337203685477; // https://github.com/Azure/azure-amqp/blob/master/Microsoft.Azure.Amqp/Amqp/AmqpConstants.cs#L47 export const maxAbsoluteExpiryTime = new Date("9999-12-31T07:59:59.000Z").getTime(); -export const aadTokenValidityMarginSeconds = 5; +export const aadTokenValidityMarginInMs = 5000; export const connectionReconnectDelay = 300; export const defaultMaxRetries = 3; export const defaultMaxRetriesForConnection = 150; -export const defaultDelayBetweenOperationRetriesInSeconds = 30; -export const defaultMaxDelayForExponentialRetryInMs = 1000 * 90; -export const defaultMinDelayForExponentialRetryInMs = 1000 * 3; +export const defaultDelayBetweenOperationRetriesInMs = 30000; +export const defaultMaxDelayForExponentialRetryInMs = 90000; +export const defaultMinDelayForExponentialRetryInMs = 3000; export const receiverSettleMode = "receiver-settle-mode"; export const dispositionStatus = "disposition-status"; export const fromSequenceNumber = "from-sequence-number"; diff --git a/sdk/core/core-amqp/test/requestResponse.spec.ts b/sdk/core/core-amqp/test/requestResponse.spec.ts index fa231dbb5015..e26671f7a145 100644 --- a/sdk/core/core-amqp/test/requestResponse.spec.ts +++ b/sdk/core/core-amqp/test/requestResponse.spec.ts @@ -100,7 +100,7 @@ describe("RequestResponseLink", function() { } } }); - }, 500); + }, 200); setTimeout(() => { rcvr.emit("message", { message: { @@ -114,11 +114,11 @@ describe("RequestResponseLink", function() { body: "Hello World!!" } }); - }, 1000); + }, 2000); const sendRequestPromise = async (): Promise => { return await link.sendRequest(request, { - timeoutInSeconds: 5 + timeoutInMs: 5000 }); }; @@ -127,7 +127,7 @@ describe("RequestResponseLink", function() { connectionId: "connection-1", operationType: RetryOperationType.management, maxRetries: 3, - delayInSeconds: 1 + delayInMs: 1000 }; const message = await retry(config); diff --git a/sdk/core/core-amqp/test/retry.spec.ts b/sdk/core/core-amqp/test/retry.spec.ts index a289e1be37fe..acf802f6bfb2 100644 --- a/sdk/core/core-amqp/test/retry.spec.ts +++ b/sdk/core/core-amqp/test/retry.spec.ts @@ -38,7 +38,7 @@ dotenv.config(); }, connectionId: "connection-1", operationType: RetryOperationType.cbsAuth, - delayInSeconds: 15, + delayInMs: 15000, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; @@ -66,7 +66,7 @@ dotenv.config(); }, connectionId: "connection-1", operationType: RetryOperationType.management, - delayInSeconds: 15, + delayInMs: 15000, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; @@ -101,7 +101,7 @@ dotenv.config(); connectionId: "connection-1", operationType: RetryOperationType.receiverLink, maxRetries: 2, - delayInSeconds: 0.5, + delayInMs: 500, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; @@ -140,7 +140,7 @@ dotenv.config(); connectionId: "connection-1", operationType: RetryOperationType.senderLink, maxRetries: 2, - delayInSeconds: 0.5, + delayInMs: 500, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; @@ -180,7 +180,7 @@ dotenv.config(); connectionId: "connection-1", operationType: RetryOperationType.sendMessage, maxRetries: 2, - delayInSeconds: 0.5, + delayInMs: 500, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; @@ -207,7 +207,7 @@ dotenv.config(); connectionId: "connection-1", operationType: RetryOperationType.session, maxRetries: 4, - delayInSeconds: 0.5, + delayInMs: 500, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; @@ -236,7 +236,7 @@ dotenv.config(); }, connectionId: "connection-1", operationType: RetryOperationType.cbsAuth, - delayInSeconds: 0.5, + delayInMs: 500, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; @@ -265,7 +265,7 @@ dotenv.config(); connectionId: "connection-1", operationType: RetryOperationType.management, maxRetries: Infinity, - delayInSeconds: 0.5, + delayInMs: 500, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; @@ -300,7 +300,7 @@ dotenv.config(); connectionId: "connection-1", operationType: RetryOperationType.receiverLink, maxRetries: Infinity, - delayInSeconds: 0.5, + delayInMs: 500, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; @@ -339,7 +339,7 @@ dotenv.config(); connectionId: "connection-1", operationType: RetryOperationType.senderLink, maxRetries: Infinity, - delayInSeconds: 0.5, + delayInMs: 500, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; @@ -379,7 +379,7 @@ dotenv.config(); connectionId: "connection-1", operationType: RetryOperationType.sendMessage, maxRetries: Constants.defaultMaxRetriesForConnection, - delayInSeconds: 0.001, + delayInMs: 1, minExponentialRetryDelayInMs: 0, retryPolicy: retryPolicy }; diff --git a/sdk/eventhub/event-hubs/src/eventHubClient.ts b/sdk/eventhub/event-hubs/src/eventHubClient.ts index 36003e442ae1..bad6a4716d74 100644 --- a/sdk/eventhub/event-hubs/src/eventHubClient.ts +++ b/sdk/eventhub/event-hubs/src/eventHubClient.ts @@ -38,7 +38,7 @@ export interface RetryOptions { retryInterval?: number; /** * Number of milliseconds to wait before declaring that current attempt has timed out which will trigger a retry - * A minimum value of 60 seconds will be used if a value not greater than this is provided. + * A minimum value of `60000` milliseconds will be used if a value not greater than this is provided. */ timeoutInMs?: number; /** @@ -62,8 +62,8 @@ export function getRetryAttemptTimeoutInMs(retryOptions: RetryOptions | undefine retryOptions == undefined || typeof retryOptions.timeoutInMs !== "number" || !isFinite(retryOptions.timeoutInMs) || - retryOptions.timeoutInMs < Constants.defaultOperationTimeoutInSeconds * 1000 - ? Constants.defaultOperationTimeoutInSeconds * 1000 + retryOptions.timeoutInMs < Constants.defaultOperationTimeoutInMs + ? Constants.defaultOperationTimeoutInMs : retryOptions.timeoutInMs; return timeoutInMs; } diff --git a/sdk/eventhub/event-hubs/src/eventHubReceiver.ts b/sdk/eventhub/event-hubs/src/eventHubReceiver.ts index e4b3566eb3ce..944dfc8f5546 100644 --- a/sdk/eventhub/event-hubs/src/eventHubReceiver.ts +++ b/sdk/eventhub/event-hubs/src/eventHubReceiver.ts @@ -528,7 +528,7 @@ export class EventHubReceiver extends LinkEntity { const linkCreationConfig: RetryConfig = { connectionId: this._context.connectionId, connectionHost: this._context.config.host, - delayInSeconds: 15, + delayInMs: 15000, operation: () => this.initialize(initOptions), operationType: RetryOperationType.receiverLink, maxRetries: Constants.defaultMaxRetriesForConnection diff --git a/sdk/eventhub/event-hubs/src/eventHubSender.ts b/sdk/eventhub/event-hubs/src/eventHubSender.ts index 480fec5fe9c5..68b194f9fc6b 100644 --- a/sdk/eventhub/event-hubs/src/eventHubSender.ts +++ b/sdk/eventhub/event-hubs/src/eventHubSender.ts @@ -293,7 +293,7 @@ export class EventHubSender extends LinkEntity { operationType: RetryOperationType.senderLink, maxRetries: Constants.defaultMaxRetriesForConnection, connectionHost: this._context.config.host, - delayInSeconds: 15 + delayInMs: 15000 }; return retry(config); }); @@ -397,10 +397,7 @@ export class EventHubSender extends LinkEntity { connectionId: this._context.connectionId, operationType: RetryOperationType.senderLink, maxRetries: retryOptions.maxRetries, - delayInSeconds: - typeof retryOptions.retryInterval === "number" - ? retryOptions.retryInterval / 1000 - : undefined, + delayInMs: retryOptions.retryInterval, retryPolicy: retryOptions.retryPolicy, minExponentialRetryDelayInMs: retryOptions.minExponentialRetryDelayInMs, maxExponentialRetryDelayInMs: retryOptions.maxExponentialRetryDelayInMs @@ -748,10 +745,7 @@ export class EventHubSender extends LinkEntity { connectionId: this._context.connectionId, operationType: RetryOperationType.sendMessage, maxRetries: retryOptions.maxRetries, - delayInSeconds: - typeof retryOptions.retryInterval === "number" - ? retryOptions.retryInterval / 1000 - : undefined, + delayInMs: retryOptions.retryInterval, retryPolicy: retryOptions.retryPolicy, minExponentialRetryDelayInMs: retryOptions.minExponentialRetryDelayInMs, maxExponentialRetryDelayInMs: retryOptions.maxExponentialRetryDelayInMs diff --git a/sdk/eventhub/event-hubs/src/linkEntity.ts b/sdk/eventhub/event-hubs/src/linkEntity.ts index f97ad8f6debf..0533c78f8869 100644 --- a/sdk/eventhub/event-hubs/src/linkEntity.ts +++ b/sdk/eventhub/event-hubs/src/linkEntity.ts @@ -97,10 +97,10 @@ export class LinkEntity { */ protected _tokenRenewalTimer?: NodeJS.Timer; /** - * @property _tokenTimeout Indicates token timeout + * @property _tokenTimeout Indicates token timeout in milliseconds * @protected */ - protected _tokenTimeout?: number; + protected _tokenTimeoutInMs?: number; /** * Creates a new LinkEntity instance. * @ignore @@ -147,7 +147,7 @@ export class LinkEntity { tokenObject = this._context.tokenCredential.getToken(this.audience); tokenType = TokenType.CbsTokenTypeSas; // renew sas token in every 45 minutess - this._tokenTimeout = (3600 - 900) * 1000; + this._tokenTimeoutInMs = (3600 - 900) * 1000; } else { const aadToken = await this._context.tokenCredential.getToken(Constants.aadEventHubsScope); if (!aadToken) { @@ -155,7 +155,7 @@ export class LinkEntity { } tokenObject = aadToken; tokenType = TokenType.CbsTokenTypeJwt; - this._tokenTimeout = tokenObject.expiresOnTimestamp - Date.now() - 2 * 60 * 1000; + this._tokenTimeoutInMs = tokenObject.expiresOnTimestamp - Date.now() - 2 * 60 * 1000; } log.link( @@ -195,7 +195,7 @@ export class LinkEntity { * @returns */ protected async _ensureTokenRenewal(): Promise { - if (!this._tokenTimeout) { + if (!this._tokenTimeoutInMs) { return; } this._tokenRenewalTimer = setTimeout(async () => { @@ -211,15 +211,15 @@ export class LinkEntity { err ); } - }, this._tokenTimeout); + }, this._tokenTimeoutInMs); log.link( - "[%s] %s '%s' with address %s, has next token renewal in %d seconds @(%s).", + "[%s] %s '%s' with address %s, has next token renewal in %d milliseconds @(%s).", this._context.connectionId, this._type, this.name, this.address, - this._tokenTimeout / 1000, - new Date(Date.now() + this._tokenTimeout).toString() + this._tokenTimeoutInMs, + new Date(Date.now() + this._tokenTimeoutInMs).toString() ); } diff --git a/sdk/eventhub/event-hubs/src/managementClient.ts b/sdk/eventhub/event-hubs/src/managementClient.ts index f8fb25aa8ca5..8de26db709e8 100644 --- a/sdk/eventhub/event-hubs/src/managementClient.ts +++ b/sdk/eventhub/event-hubs/src/managementClient.ts @@ -380,7 +380,7 @@ export class ManagementClient extends LinkEntity { const sendRequestOptions: SendRequestOptions = { abortSignal: options.abortSignal, requestName: options.requestName, - timeoutInSeconds: remainingOperationTimeoutInMs / 1000 + timeoutInMs: remainingOperationTimeoutInMs }; count++; @@ -417,10 +417,7 @@ export class ManagementClient extends LinkEntity { connectionId: this._context.connectionId, operationType: RetryOperationType.management, maxRetries: retryOptions.maxRetries, - delayInSeconds: - typeof retryOptions.retryInterval === "number" - ? retryOptions.retryInterval / 1000 - : undefined, + delayInMs: retryOptions.retryInterval, retryPolicy: retryOptions.retryPolicy, minExponentialRetryDelayInMs: retryOptions.minExponentialRetryDelayInMs, maxExponentialRetryDelayInMs: retryOptions.maxExponentialRetryDelayInMs diff --git a/sdk/eventhub/event-hubs/src/receiver.ts b/sdk/eventhub/event-hubs/src/receiver.ts index c069e4115eff..f736adc12d9f 100644 --- a/sdk/eventhub/event-hubs/src/receiver.ts +++ b/sdk/eventhub/event-hubs/src/receiver.ts @@ -234,7 +234,7 @@ export class EventHubConsumer { options: EventIteratorOptions = {} ): AsyncIterableIterator { const maxMessageCount = 1; - const maxWaitTimeInSeconds = Constants.defaultOperationTimeoutInSeconds; + const maxWaitTimeInSeconds = Constants.defaultOperationTimeoutInMs / 1000; while (true) { const currentBatch = await this.receiveBatch( @@ -404,10 +404,7 @@ export class EventHubConsumer { const config: RetryConfig = { connectionHost: this._context.config.host, connectionId: this._context.connectionId, - delayInSeconds: - typeof retryOptions.retryInterval === "number" && retryOptions.retryInterval > 0 - ? retryOptions.retryInterval / 1000 - : Constants.defaultDelayBetweenOperationRetriesInSeconds, + delayInMs: retryOptions.retryInterval, operation: retrieveEvents, operationType: RetryOperationType.receiveMessage, maxRetries: retryOptions.maxRetries,