11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT license.
33
4- import { Connection , ConnectionOptions , generate_uuid } from "rhea-promise" ;
4+ import {
5+ AwaitableSender ,
6+ Connection ,
7+ ConnectionOptions ,
8+ CreateAwaitableSenderOptions ,
9+ CreateReceiverOptions ,
10+ CreateSenderOptions ,
11+ Receiver ,
12+ Sender ,
13+ generate_uuid ,
14+ } from "rhea-promise" ;
515import { getFrameworkInfo , getPlatformInfo } from "./util/runtimeInfo" ;
616import { CbsClient } from "./cbs" ;
717import { ConnectionConfig } from "./connectionConfig/connectionConfig" ;
@@ -100,6 +110,53 @@ export interface CreateConnectionContextBaseParameters {
100110 operationTimeoutInMs ?: number ;
101111}
102112
113+ const maxListenerLimit = 1000 ;
114+
115+ class CoreAmqpConnection extends Connection {
116+ /**
117+ * Creates an amqp sender link. Max listener limit on the sender is set to 1000 because the
118+ * default value of 10 in NodeJS is too low.
119+ * @param options - Optional parameters to create a sender link.
120+ * @returns Promise<Sender>.
121+ */
122+ async createSender ( options ?: CreateSenderOptions ) : Promise < Sender > {
123+ const sender = await super . createSender ( options ) ;
124+ sender . setMaxListeners ( maxListenerLimit ) ;
125+ return sender ;
126+ }
127+
128+ /**
129+ * Creates an awaitable amqp sender. Max listener limit on the sender is set to 1000 because the
130+ * default value of 10 in NodeJS is too low.
131+ * @param options - Optional parameters to create an awaitable sender link.
132+ * - If `onError` and `onSessionError` handlers are not provided then the `AwaitableSender` will
133+ * clear the timer and reject the Promise for all the entries of inflight send operation in its
134+ * `deliveryDispositionMap`.
135+ * - If the user is handling the reconnection of sender link or the underlying connection in it's
136+ * app, then the `onError` and `onSessionError` handlers must be provided by the user and (s)he
137+ * shall be responsible of clearing the `deliveryDispositionMap` of inflight `send()` operation.
138+ *
139+ * @returns Promise<AwaitableSender>.
140+ */
141+ async createAwaitableSender ( options ?: CreateAwaitableSenderOptions ) : Promise < AwaitableSender > {
142+ const sender = await super . createAwaitableSender ( options ) ;
143+ sender . setMaxListeners ( maxListenerLimit ) ;
144+ return sender ;
145+ }
146+
147+ /**
148+ * Creates an amqp receiver link. Max listener limit on the sender is set to 1000 because the
149+ * default value of 10 in NodeJS is too low.
150+ * @param options - Optional parameters to create a receiver link.
151+ * @returns Promise<Receiver>.
152+ */
153+ async createReceiver ( options ?: CreateReceiverOptions ) : Promise < Receiver > {
154+ const receiver = await super . createReceiver ( options ) ;
155+ receiver . setMaxListeners ( maxListenerLimit ) ;
156+ return receiver ;
157+ }
158+ }
159+
103160// eslint-disable-next-line @typescript-eslint/no-redeclare -- renaming constant would be a breaking change.
104161export const ConnectionContextBase = {
105162 /**
@@ -157,7 +214,7 @@ export const ConnectionContextBase = {
157214 } ;
158215 }
159216
160- const connection = new Connection ( connectionOptions ) ;
217+ const connection = new CoreAmqpConnection ( connectionOptions ) ;
161218 const connectionLock = `${ Constants . establishConnection } -${ generate_uuid ( ) } ` ;
162219 const connectionContextBase : ConnectionContextBase = {
163220 wasConnectionCloseCalled : false ,
@@ -168,7 +225,7 @@ export const ConnectionContextBase = {
168225 cbsSession : new CbsClient ( connection , connectionLock ) ,
169226 config : parameters . config ,
170227 refreshConnection ( ) {
171- const newConnection = new Connection ( connectionOptions ) ;
228+ const newConnection = new CoreAmqpConnection ( connectionOptions ) ;
172229 const newConnectionLock = `${ Constants . establishConnection } -${ generate_uuid ( ) } ` ;
173230 this . wasConnectionCloseCalled = false ;
174231 this . connectionLock = newConnectionLock ;
0 commit comments