@@ -42,20 +42,8 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisSc
4242 throw new Error ( 'None of the root nodes is available' ) ;
4343 }
4444
45- async discover ( startWith : RedisClientType < M , S > ) : Promise < void > {
46- if ( await this . #discoverNodes( startWith . options ) ) return ;
47-
48- for ( const { client } of this . #nodeByUrl. values ( ) ) {
49- if ( client === startWith ) continue ;
50-
51- if ( await this . #discoverNodes( client . options ) ) return ;
52- }
53-
54- throw new Error ( 'None of the cluster nodes is available' ) ;
55- }
56-
5745 async #discoverNodes( clientOptions ?: RedisClusterClientOptions ) : Promise < boolean > {
58- const client = new this . #Client ( clientOptions ) ;
46+ const client = this . #initiateClient ( clientOptions ) ;
5947
6048 await client . connect ( ) ;
6149
@@ -72,6 +60,29 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisSc
7260 }
7361 }
7462
63+ #runningRediscoverPromise?: Promise < void > ;
64+
65+ async rediscover ( startWith : RedisClientType < M , S > ) : Promise < void > {
66+ if ( ! this . #runningRediscoverPromise) {
67+ this . #runningRediscoverPromise = this . #rediscover( startWith )
68+ . finally ( ( ) => this . #runningRediscoverPromise = undefined ) ;
69+ }
70+
71+ return this . #runningRediscoverPromise;
72+ }
73+
74+ async #rediscover( startWith : RedisClientType < M , S > ) : Promise < void > {
75+ if ( await this . #discoverNodes( startWith . options ) ) return ;
76+
77+ for ( const { client } of this . #nodeByUrl. values ( ) ) {
78+ if ( client === startWith ) continue ;
79+
80+ if ( await this . #discoverNodes( client . options ) ) return ;
81+ }
82+
83+ throw new Error ( 'None of the cluster nodes is available' ) ;
84+ }
85+
7586 async #reset( masters : Array < RedisClusterMasterNode > ) : Promise < void > {
7687 // Override this.#slots and add not existing clients to this.#nodeByUrl
7788 const promises : Array < Promise < void > > = [ ] ,
@@ -103,18 +114,23 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisSc
103114 await Promise . all ( promises ) ;
104115 }
105116
106- #clientOptionsDefaults( options : RedisClusterClientOptions ) : RedisClusterClientOptions {
117+ #clientOptionsDefaults( options ? : RedisClusterClientOptions ) : RedisClusterClientOptions | undefined {
107118 if ( ! this . #options. defaults ) return options ;
108119
109120 const merged = Object . assign ( { } , this . #options. defaults , options ) ;
110121
111- if ( options . socket && this . #options. defaults . socket ) {
122+ if ( options ? .socket && this . #options. defaults . socket ) {
112123 Object . assign ( { } , this . #options. defaults . socket , options . socket ) ;
113124 }
114125
115126 return merged ;
116127 }
117128
129+ #initiateClient( options ?: RedisClusterClientOptions ) : RedisClientType < M , S > {
130+ return new this . #Client( this . #clientOptionsDefaults( options ) )
131+ . on ( 'error' , this . #onError) ;
132+ }
133+
118134 #initiateClientForNode( nodeData : RedisClusterMasterNode | RedisClusterReplicaNode , readonly : boolean , clientsInUse : Set < string > , promises : Array < Promise < void > > ) : ClusterNode < M , S > {
119135 const url = `${ nodeData . host } :${ nodeData . port } ` ;
120136 clientsInUse . add ( url ) ;
@@ -123,15 +139,13 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisSc
123139 if ( ! node ) {
124140 node = {
125141 id : nodeData . id ,
126- client : new this . #Client(
127- this . #clientOptionsDefaults( {
128- socket : {
129- host : nodeData . host ,
130- port : nodeData . port
131- } ,
132- readonly
133- } )
134- )
142+ client : this . #initiateClient( {
143+ socket : {
144+ host : nodeData . host ,
145+ port : nodeData . port
146+ } ,
147+ readonly
148+ } )
135149 } ;
136150 promises . push ( node . client . connect ( ) ) ;
137151 this . #nodeByUrl. set ( url , node ) ;
0 commit comments