@@ -42,20 +42,8 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisSc
42
42
throw new Error ( 'None of the root nodes is available' ) ;
43
43
}
44
44
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
-
57
45
async #discoverNodes( clientOptions ?: RedisClusterClientOptions ) : Promise < boolean > {
58
- const client = new this . #Client ( clientOptions ) ;
46
+ const client = this . #initiateClient ( clientOptions ) ;
59
47
60
48
await client . connect ( ) ;
61
49
@@ -72,6 +60,29 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisSc
72
60
}
73
61
}
74
62
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
+
75
86
async #reset( masters : Array < RedisClusterMasterNode > ) : Promise < void > {
76
87
// Override this.#slots and add not existing clients to this.#nodeByUrl
77
88
const promises : Array < Promise < void > > = [ ] ,
@@ -103,18 +114,23 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisSc
103
114
await Promise . all ( promises ) ;
104
115
}
105
116
106
- #clientOptionsDefaults( options : RedisClusterClientOptions ) : RedisClusterClientOptions {
117
+ #clientOptionsDefaults( options ? : RedisClusterClientOptions ) : RedisClusterClientOptions | undefined {
107
118
if ( ! this . #options. defaults ) return options ;
108
119
109
120
const merged = Object . assign ( { } , this . #options. defaults , options ) ;
110
121
111
- if ( options . socket && this . #options. defaults . socket ) {
122
+ if ( options ? .socket && this . #options. defaults . socket ) {
112
123
Object . assign ( { } , this . #options. defaults . socket , options . socket ) ;
113
124
}
114
125
115
126
return merged ;
116
127
}
117
128
129
+ #initiateClient( options ?: RedisClusterClientOptions ) : RedisClientType < M , S > {
130
+ return new this . #Client( this . #clientOptionsDefaults( options ) )
131
+ . on ( 'error' , this . #onError) ;
132
+ }
133
+
118
134
#initiateClientForNode( nodeData : RedisClusterMasterNode | RedisClusterReplicaNode , readonly : boolean , clientsInUse : Set < string > , promises : Array < Promise < void > > ) : ClusterNode < M , S > {
119
135
const url = `${ nodeData . host } :${ nodeData . port } ` ;
120
136
clientsInUse . add ( url ) ;
@@ -123,15 +139,13 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisSc
123
139
if ( ! node ) {
124
140
node = {
125
141
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
+ } )
135
149
} ;
136
150
promises . push ( node . client . connect ( ) ) ;
137
151
this . #nodeByUrl. set ( url , node ) ;
0 commit comments