@@ -17,54 +17,40 @@ interface SlotNodes<M extends RedisModules, S extends RedisLuaScripts> {
1717 clientIterator : IterableIterator < RedisClientType < M , S > > | undefined ;
1818}
1919
20+ type OnError = ( err : unknown ) => void ;
21+
2022export default class RedisClusterSlots < M extends RedisModules , S extends RedisLuaScripts > {
2123 readonly #options: RedisClusterOptions ;
24+ readonly #onError: OnError ;
2225 readonly #nodeByUrl = new Map < string , ClusterNode < M , S > > ( ) ;
2326 readonly #slots: Array < SlotNodes < M , S > > = [ ] ;
2427
25- constructor ( options : RedisClusterOptions ) {
28+ constructor ( options : RedisClusterOptions , onError : OnError ) {
2629 this . #options = options ;
30+ this . #onError = onError ;
2731 }
2832
2933 async connect ( ) : Promise < void > {
3034 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 ;
3836 }
3937
4038 throw new Error ( 'None of the root nodes is available' ) ;
4139 }
4240
4341 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 ;
5143
5244 for ( const { client } of this . #nodeByUrl. values ( ) ) {
5345 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 ;
6248 }
6349
6450 throw new Error ( 'None of the cluster nodes is available' ) ;
6551 }
6652
67- async #discoverNodes( socketOptions ?: RedisSocketOptions ) : Promise < void > {
53+ async #discoverNodes( socketOptions ?: RedisSocketOptions ) : Promise < boolean > {
6854 const client = RedisClient . create ( {
6955 socket : socketOptions
7056 } ) ;
@@ -73,8 +59,14 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
7359
7460 try {
7561 await this . #reset( await client . clusterNodes ( ) ) ;
62+ return true ;
63+ } catch ( err ) {
64+ this . #onError( err ) ;
65+ return false ;
7666 } finally {
77- await client . disconnect ( ) ; // TODO: catch error from disconnect?
67+ if ( client . isOpen ) {
68+ await client . disconnect ( ) ;
69+ }
7870 }
7971 }
8072
@@ -102,7 +94,6 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisLu
10294 for ( const [ url , { client } ] of this . #nodeByUrl. entries ( ) ) {
10395 if ( clientsInUse . has ( url ) ) continue ;
10496
105- // TODO: ignore error from `.disconnect`?
10697 promises . push ( client . disconnect ( ) ) ;
10798 this . #nodeByUrl. delete ( url ) ;
10899 }
0 commit comments