1
1
import calculateSlot from 'cluster-key-slot' ;
2
2
import RedisClient , { RedisClientType } from './client' ;
3
- import { RedisSocketOptions } from './socket' ;
4
3
import { RedisClusterMasterNode , RedisClusterReplicaNode } from './commands/CLUSTER_NODES' ;
5
4
import { RedisClusterClientOptions , RedisClusterOptions } from './cluster' ;
6
5
import { RedisModules } from './commands' ;
@@ -32,7 +31,7 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
32
31
33
32
async connect ( ) : Promise < void > {
34
33
for ( const rootNode of this . #options. rootNodes ) {
35
- if ( await this . #discoverNodes( rootNode ) ) return ;
34
+ if ( await this . #discoverNodes( this . #clientOptionsDefaults ( rootNode ) ) ) return ;
36
35
}
37
36
38
37
throw new Error ( 'None of the root nodes is available' ) ;
@@ -99,6 +98,18 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
99
98
await Promise . all ( promises ) ;
100
99
}
101
100
101
+ #clientOptionsDefaults( options : RedisClusterClientOptions ) : RedisClusterClientOptions {
102
+ if ( ! this . #options. defaults ) return options ;
103
+
104
+ const merged = Object . assign ( { } , this . #options. defaults , options ) ;
105
+
106
+ if ( options . socket && this . #options. defaults . socket ) {
107
+ Object . assign ( { } , this . #options. defaults . socket , options . socket ) ;
108
+ }
109
+
110
+ return merged ;
111
+ }
112
+
102
113
#initiateClientForNode( nodeData : RedisClusterMasterNode | RedisClusterReplicaNode , readonly : boolean , clientsInUse : Set < string > , promises : Array < Promise < void > > ) : ClusterNode < M , S > {
103
114
const url = `${ nodeData . host } :${ nodeData . port } ` ;
104
115
clientsInUse . add ( url ) ;
@@ -107,13 +118,15 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
107
118
if ( ! node ) {
108
119
node = {
109
120
id : nodeData . id ,
110
- client : RedisClient . create ( {
111
- socket : {
112
- host : nodeData . host ,
113
- port : nodeData . port
114
- } ,
115
- readonly
116
- } )
121
+ client : RedisClient . create (
122
+ this . #clientOptionsDefaults( {
123
+ socket : {
124
+ host : nodeData . host ,
125
+ port : nodeData . port
126
+ } ,
127
+ readonly
128
+ } )
129
+ )
117
130
} ;
118
131
promises . push ( node . client . connect ( ) ) ;
119
132
this . #nodeByUrl. set ( url , node ) ;
0 commit comments