@@ -498,7 +498,7 @@ export default class RedisClient<
498
498
this . #validateOptions( options )
499
499
this . #options = this . #initiateOptions( options ) ;
500
500
this . #queue = this . #initiateQueue( ) ;
501
- this . #socket = this . #initiateSocket ( this . #options) ;
501
+ this . #socket = this . #createSocket ( this . #options) ;
502
502
// Queue
503
503
// toWrite [ C D E ]
504
504
// waitingForReply [ A B ]
@@ -511,54 +511,54 @@ export default class RedisClient<
511
511
// 4. [EVENT] In-flight commands completed
512
512
// 5. [ACTION] Unpause writing -> we are going to write to the new socket from now on
513
513
// 6. [ACTION] Destroy old socket
514
- this . options ?. gracefulMaintenance && this . #queue. events . on ( 'moving' , async ( afterMs : number , host : string , port : number ) => {
515
- // 1
516
- console . log ( `Moving to ${ host } :${ port } before ${ afterMs } ms` ) ;
517
-
518
- // 2
519
- console . log ( `Pausing writing until new socket is ready and all in-flight commands are completed` ) ;
520
- // this.#paused = true;
521
-
522
- const oldSocket = this . #socket;
523
- this . #socket = this . #initiateSocket( {
524
- ...this . #options,
525
- socket : {
526
- ...this . #options?. socket ,
527
- host,
528
- port
529
- }
530
- } ) ;
531
-
532
- // 3
533
- this . #socket. once ( 'ready' , ( ) => {
534
- //TODO handshake...???
535
- console . log ( `Connected to ${ host } :${ port } ` ) ;
536
-
537
- // 4
538
- if ( ! this . #queue. isWaitingForReply ( ) ) {
539
- // 5 and 6
540
- console . log ( `All in-flight commands completed` ) ;
541
- console . log ( `Resume writing` )
542
- oldSocket . destroy ( ) ;
543
- this . #paused = false ;
544
- }
545
- } ) ;
546
-
547
- // 4
548
- this . #queue. events . once ( 'waitingForReplyEmpty' , ( ) => {
549
- console . log ( `All in-flight commands completed` ) ;
550
- // 3
551
- if ( this . #socket. isReady ) {
552
- // 5 and 6
553
- console . log ( `Connected to ${ host } :${ port } ` ) ;
554
- console . log ( `Resume writing` )
555
- oldSocket . destroy ( ) ;
556
- this . #paused = false ;
557
- }
558
- } ) ;
559
-
560
- await this . #socket. connect ( )
561
- } ) ;
514
+ // this.options?.gracefulMaintenance && this.#queue.events.on('moving', async (afterMs: number, host: string, port: number) => {
515
+ // // 1
516
+ // console.log(`Moving to ${host}:${port} before ${afterMs}ms`);
517
+
518
+ // // 2
519
+ // console.log(`Pausing writing until new socket is ready and all in-flight commands are completed`);
520
+ // // this.#paused = true;
521
+
522
+ // const oldSocket = this.#socket;
523
+ // this.#socket = this.#initiateSocket({
524
+ // ...this.#options,
525
+ // socket: {
526
+ // ...this.#options?.socket,
527
+ // host,
528
+ // port
529
+ // }
530
+ // });
531
+
532
+ // // 3
533
+ // this.#socket.once('ready', () => {
534
+ // //TODO handshake...???
535
+ // console.log(`Connected to ${host}:${port}`);
536
+
537
+ // // 4
538
+ // if(!this.#queue.isWaitingForReply()) {
539
+ // // 5 and 6
540
+ // console.log(`All in-flight commands completed`);
541
+ // console.log(`Resume writing`)
542
+ // oldSocket.destroy();
543
+ // this.#paused = false;
544
+ // }
545
+ // });
546
+
547
+ // // 4
548
+ // this.#queue.events.once('waitingForReplyEmpty', () => {
549
+ // console.log(`All in-flight commands completed`);
550
+ // // 3
551
+ // if(this.#socket.isReady) {
552
+ // // 5 and 6
553
+ // console.log(`Connected to ${host}:${port}`);
554
+ // console.log(`Resume writing`)
555
+ // oldSocket.destroy();
556
+ // this.#paused = false;
557
+ // }
558
+ // });
559
+
560
+ // await this.#socket.connect()
561
+ // });
562
562
563
563
if ( options ?. clientSideCache ) {
564
564
if ( options . clientSideCache instanceof ClientSideCacheProvider ) {
@@ -783,39 +783,42 @@ export default class RedisClient<
783
783
return commands ;
784
784
}
785
785
786
- #initiateSocket( options ?: RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > ) : RedisSocket {
787
- const socketInitiator = async ( ) => {
788
- console . log ( 'Initiator...' ) ;
789
- const promises = [ ] ,
790
- chainId = Symbol ( 'Socket Initiator' ) ;
786
+ async #initiateSocket( options ?: RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > ) : Promise < void > {
787
+ await this . #socket. waitForReady ( ) ;
788
+ console . log ( 'Initiator...' ) ;
789
+ const promises = [ ] ;
790
+ const chainId = Symbol ( 'Socket Initiator' ) ;
791
+
792
+ const resubscribePromise = this . #queue. resubscribe ( chainId ) ;
793
+ if ( resubscribePromise ) {
794
+ promises . push ( resubscribePromise ) ;
795
+ }
791
796
792
- const resubscribePromise = this . #queue. resubscribe ( chainId ) ;
793
- if ( resubscribePromise ) {
794
- promises . push ( resubscribePromise ) ;
795
- }
797
+ if ( this . #monitorCallback) {
798
+ promises . push (
799
+ this . #queue. monitor (
800
+ this . #monitorCallback,
801
+ {
802
+ typeMapping : this . _commandOptions ?. typeMapping ,
803
+ chainId,
804
+ asap : true
805
+ }
806
+ )
807
+ ) ;
808
+ }
796
809
797
- if ( this . #monitorCallback) {
798
- promises . push (
799
- this . #queue. monitor (
800
- this . #monitorCallback,
801
- {
802
- typeMapping : this . _commandOptions ?. typeMapping ,
803
- chainId,
804
- asap : true
805
- }
806
- )
807
- ) ;
808
- }
810
+ promises . push ( ...( await this . #handshake( chainId , true ) ) ) ;
809
811
810
- promises . push ( ... ( await this . #handshake ( chainId , true ) ) ) ;
812
+ this . #setPingTimer ( ) ;
811
813
812
- if ( promises . length ) {
813
- this . #write( ) ;
814
- return Promise . all ( promises ) ;
815
- }
816
- } ;
814
+ if ( promises . length ) {
815
+ this . #write( ) ;
816
+ await Promise . all ( promises ) ;
817
+ }
818
+ }
817
819
818
- return new RedisSocket ( socketInitiator , options ?. socket )
820
+ #createSocket( options ?: RedisClientOptions < M , F , S , RESP , TYPE_MAPPING > ) : RedisSocket {
821
+ return new RedisSocket ( options ?. socket )
819
822
. on ( 'data' , chunk => {
820
823
try {
821
824
this . #queue. decoder . write ( chunk ) ;
@@ -837,8 +840,6 @@ export default class RedisClient<
837
840
. on ( 'ready' , ( ) => {
838
841
console . log ( 'Socket ready' ) ;
839
842
this . emit ( 'ready' ) ;
840
- this . #setPingTimer( ) ;
841
- this . #maybeScheduleWrite( ) ;
842
843
} )
843
844
. on ( 'reconnecting' , ( ) => this . emit ( 'reconnecting' ) )
844
845
. on ( 'drain' , ( ) => this . #maybeScheduleWrite( ) )
@@ -951,6 +952,7 @@ export default class RedisClient<
951
952
952
953
async connect ( ) {
953
954
await this . _self . #socket. connect ( ) ;
955
+ await this . _self . #initiateSocket( this . _self . #options) ;
954
956
return this as unknown as RedisClientType < M , F , S , RESP , TYPE_MAPPING > ;
955
957
}
956
958
0 commit comments