[Service Bus] Promise executor functions should not be async (#13818)

This commit is contained in:
Ramya Rao 2021-02-16 18:26:42 -08:00 коммит произвёл GitHub
Родитель f931f1b646
Коммит 65b209a32c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
1 изменённых файлов: 109 добавлений и 118 удалений

Просмотреть файл

@ -67,7 +67,11 @@ export class MessageSender extends LinkEntity<AwaitableSender> {
private _onSessionClose: OnAmqpEvent;
private _retryOptions: RetryOptions;
constructor(connectionContext: ConnectionContext, entityPath: string, retryOptions: RetryOptions) {
constructor(
connectionContext: ConnectionContext,
entityPath: string,
retryOptions: RetryOptions
) {
super(entityPath, entityPath, connectionContext, "sender", logger, {
address: entityPath,
audience: `${connectionContext.config.endpoint}${entityPath}`
@ -170,8 +174,7 @@ export class MessageSender extends LinkEntity<AwaitableSender> {
? Constants.defaultOperationTimeoutInMs
: this._retryOptions.timeoutInMs;
const sendEventPromise = () =>
new Promise<void>(async (resolve, reject) => {
const sendEventPromise = async (): Promise<void> => {
const initStartTime = Date.now();
if (!this.isOpen()) {
try {
@ -192,11 +195,10 @@ export class MessageSender extends LinkEntity<AwaitableSender> {
this.logPrefix,
this.name
);
return reject(err);
throw err;
}
}
try {
const timeTakenByInit = Date.now() - initStartTime;
logger.verbose(
@ -224,7 +226,20 @@ export class MessageSender extends LinkEntity<AwaitableSender> {
this.link?.session?.outgoing?.available()
);
}
if (this.link?.sendable()) {
if (!this.link?.sendable()) {
// let us retry to send the message after some time.
const msg =
`[${this.logPrefix}] Sender "${this.name}", ` +
`cannot send the message right now. Please try later.`;
logger.warning(msg);
const amqpError: AmqpError = {
condition: ErrorNameConditionMapper.SenderBusyError,
description: msg
};
throw translateServiceBusError(amqpError);
}
if (timeoutInMs <= timeTakenByInit) {
const desc: string =
`${this.logPrefix} Sender "${this.name}" ` +
@ -235,8 +250,9 @@ export class MessageSender extends LinkEntity<AwaitableSender> {
condition: ErrorNameConditionMapper.ServiceUnavailableError,
description: desc
};
return reject(translateServiceBusError(e));
throw translateServiceBusError(e);
}
try {
this.link.sendTimeoutInSeconds = (timeoutInMs - timeTakenByInit) / 1000;
const delivery = await this.link!.send(
@ -250,32 +266,12 @@ export class MessageSender extends LinkEntity<AwaitableSender> {
this.name,
delivery.id
);
return resolve();
} catch (error) {
error = translateServiceBusError(error.innerError || error);
logger.logError(
error,
`${this.logPrefix} An error occurred while sending the message`
);
return reject(error);
logger.logError(error, `${this.logPrefix} An error occurred while sending the message`);
throw error;
}
} else {
// let us retry to send the message after some time.
const msg =
`[${this.logPrefix}] Sender "${this.name}", ` +
`cannot send the message right now. Please try later.`;
logger.warning(msg);
const amqpError: AmqpError = {
condition: ErrorNameConditionMapper.SenderBusyError,
description: msg
};
reject(translateServiceBusError(amqpError));
}
} catch (err) {
reject(err);
}
});
const config: RetryConfig<void> = {
operation: sendEventPromise,
connectionId: this._context.connectionId!,
@ -481,8 +477,7 @@ export class MessageSender extends LinkEntity<AwaitableSender> {
if (this.isOpen()) {
return this.link!.maxMessageSize;
}
return new Promise<number>(async (resolve, reject) => {
try {
const config: RetryConfig<void> = {
operation: () => this.open(undefined, options?.abortSignal),
connectionId: this._context.connectionId,
@ -493,11 +488,7 @@ export class MessageSender extends LinkEntity<AwaitableSender> {
await retry<void>(config);
return resolve(this.link!.maxMessageSize);
} catch (err) {
reject(err);
}
});
return this.link!.maxMessageSize;
}
async createBatch(options?: CreateMessageBatchOptions): Promise<ServiceBusMessageBatch> {