@@ -295,7 +295,7 @@ export class WebSocketConnector {
295295 }
296296
297297 private shouldSkipReconnect ( conn : w3cwebsocket ) : boolean {
298- if ( ! this . _allowReconnect ) {
298+ if ( ! this . isReconnectAllowed ( ) ) {
299299 return true ;
300300 }
301301 return this . _suppressedSockets . has ( conn ) ;
@@ -439,7 +439,14 @@ export class WebSocketConnector {
439439 await new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
440440 }
441441
442+ private isReconnectAllowed ( ) : boolean {
443+ return this . _allowReconnect ;
444+ }
445+
442446 private async triggerReconnect ( ) : Promise < void > {
447+ if ( ! this . isReconnectAllowed ( ) ) {
448+ return ;
449+ }
443450 if ( ! this . _reconnectLock ) {
444451 this . _reconnectLock = this . _doReconnect ( ) ;
445452 }
@@ -455,6 +462,9 @@ export class WebSocketConnector {
455462 }
456463
457464 private async _doReconnect ( ) : Promise < void > {
465+ if ( ! this . isReconnectAllowed ( ) ) {
466+ return ;
467+ }
458468 this . _isReconnecting = true ;
459469 try {
460470 await this . attemptReconnect ( ) ;
@@ -485,6 +495,9 @@ export class WebSocketConnector {
485495
486496 for ( let i = 0 ; i < totalAddresses ; i ++ ) {
487497 for ( let retry = 0 ; retry < this . _retryConfig . retries ; retry ++ ) {
498+ if ( ! this . isReconnectAllowed ( ) ) {
499+ return ;
500+ }
488501 try {
489502 logger . info ( `Reconnecting to ${ this . getCurrentAddress ( ) } , attempt ${ retry + 1 } ` ) ;
490503 await this . reconnect ( ) ;
@@ -539,16 +552,19 @@ export class WebSocketConnector {
539552 }
540553
541554 close ( ) {
555+ this . _allowReconnect = false ;
556+ this . failAllInflightRequests (
557+ new TDWebSocketClientError (
558+ ErrorCode . ERR_CONNECTION_CLOSED ,
559+ "websocket connection closed"
560+ )
561+ ) ;
542562 if ( this . _conn ) {
543- this . _allowReconnect = false ;
544563 AddressConnectionTracker . instance ( ) . decrement ( this . getCurrentAddress ( ) ) ;
545564 this . _suppressedSockets . add ( this . _conn ) ;
546565 this . _conn . close ( ) ;
547566 } else {
548- throw new TDWebSocketClientError (
549- ErrorCode . ERR_WEBSOCKET_CONNECTION_FAIL ,
550- "WebSocket connection is undefined"
551- ) ;
567+ logger . warn ( "close() called but websocket connection is undefined" ) ;
552568 }
553569 }
554570
0 commit comments