@@ -17,54 +17,40 @@ interface SlotNodes<M extends RedisModules, S extends RedisLuaScripts> {
17
17
clientIterator : IterableIterator < RedisClientType < M , S > > | undefined ;
18
18
}
19
19
20
+ type OnError = ( err : unknown ) => void ;
21
+
20
22
export default class RedisClusterSlots < M extends RedisModules , S extends RedisLuaScripts > {
21
23
readonly #options: RedisClusterOptions ;
24
+ readonly #onError: OnError ;
22
25
readonly #nodeByUrl = new Map < string , ClusterNode < M , S > > ( ) ;
23
26
readonly #slots: Array < SlotNodes < M , S > > = [ ] ;
24
27
25
- constructor ( options : RedisClusterOptions ) {
28
+ constructor ( options : RedisClusterOptions , onError : OnError ) {
26
29
this . #options = options ;
30
+ this . #onError = onError ;
27
31
}
28
32
29
33
async connect ( ) : Promise < void > {
30
34
for ( const rootNode of this . #options. rootNodes ) {
31
- try {
32
- await this . #discoverNodes( rootNode ) ;
33
- return ;
34
- } catch ( err ) {
35
- console . error ( err ) ;
36
- // this.emit('error', err);
37
- }
35
+ if ( await this . #discoverNodes( rootNode ) ) return ;
38
36
}
39
37
40
38
throw new Error ( 'None of the root nodes is available' ) ;
41
39
}
42
40
43
41
async discover ( startWith : RedisClientType < M , S > ) : Promise < void > {
44
- try {
45
- await this . #discoverNodes( startWith . options ?. socket ) ;
46
- return ;
47
- } catch ( err ) {
48
- console . error ( err ) ;
49
- // this.emit('error', err);
50
- }
42
+ if ( await this . #discoverNodes( startWith . options ?. socket ) ) return ;
51
43
52
44
for ( const { client } of this . #nodeByUrl. values ( ) ) {
53
45
if ( client === startWith ) continue ;
54
-
55
- try {
56
- await this . #discoverNodes( client . options ?. socket ) ;
57
- return ;
58
- } catch ( err ) {
59
- console . error ( err ) ;
60
- // this.emit('error', err);
61
- }
46
+
47
+ if ( await this . #discoverNodes( client . options ?. socket ) ) return ;
62
48
}
63
49
64
50
throw new Error ( 'None of the cluster nodes is available' ) ;
65
51
}
66
52
67
- async #discoverNodes( socketOptions ?: RedisSocketOptions ) : Promise < void > {
53
+ async #discoverNodes( socketOptions ?: RedisSocketOptions ) : Promise < boolean > {
68
54
const client = RedisClient . create ( {
69
55
socket : socketOptions
70
56
} ) ;
@@ -73,8 +59,14 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
73
59
74
60
try {
75
61
await this . #reset( await client . clusterNodes ( ) ) ;
62
+ return true ;
63
+ } catch ( err ) {
64
+ this . #onError( err ) ;
65
+ return false ;
76
66
} finally {
77
- await client . disconnect ( ) ; // TODO: catch error from disconnect?
67
+ if ( client . isOpen ) {
68
+ await client . disconnect ( ) ;
69
+ }
78
70
}
79
71
}
80
72
@@ -102,7 +94,6 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
102
94
for ( const [ url , { client } ] of this . #nodeByUrl. entries ( ) ) {
103
95
if ( clientsInUse . has ( url ) ) continue ;
104
96
105
- // TODO: ignore error from `.disconnect`?
106
97
promises . push ( client . disconnect ( ) ) ;
107
98
this . #nodeByUrl. delete ( url ) ;
108
99
}
0 commit comments