Skip to content

Commit caade1c

Browse files
committed
fix(nats): error during reconect and delayed retry
1 parent f34e421 commit caade1c

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/node-nats-streaming-buffered-client.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,15 @@ export class NatsBufferedClient
100100
* @param {number} [bufferSize=10] Size of our publish buffer
101101
* @param {boolean} [waitForInitialConnection=false] Allows publishing to the buffer before initial connect
102102
* @param {*} [logger=console] The console logger to use
103+
* @param {number} [reconnectDelay=5000] If reconnect fails retry after this amount of time. Default is 5 seconds
103104
* @memberof NatsBufferedClient
104105
*/
105106
constructor(
106107
bufferSize: number = 10,
107108
private waitForInitialConnection = false,
108-
logger = console )
109+
logger = console,
110+
private reconnectDelay = 5000,
111+
)
109112
{
110113
// Build our logger
111114
//
@@ -287,12 +290,21 @@ export class NatsBufferedClient
287290
// First close existing connection if still available
288291
//
289292
this.logger.log( '[NATS-BUFFERED-CLIENT] Reconnect requested. Disconnecting...' );
290-
await this.disconnect();
293+
try {
294+
await this.disconnect();
295+
} catch ( disconnectError ) {
296+
this.logger.warn( '[NATS-BUFFERED-CLIENT] Error during disconnect', disconnectError );
297+
}
291298

292299
// Create a new connection
293300
//
294301
this.logger.log( '[NATS-BUFFERED-CLIENT] Disconnected trying to connect again...' );
295-
await this.connect( this.clusterId, this.clientId, this.clientOptions );
302+
try {
303+
await this.connect( this.clusterId, this.clientId, this.clientOptions );
304+
} catch ( connectError ) {
305+
this.logger.warn( '[NATS-BUFFERED-CLIENT] Error during connect. Retry in 5 seconds', connectError );
306+
setTimeout( () => this.reconnect(), this.reconnectDelay );
307+
}
296308

297309
this.logger.log( '[NATS-BUFFERED-CLIENT] Reconnect completed' );
298310
}

0 commit comments

Comments
 (0)