1
- import { parseURL } from '../utils'
2
1
import { EventEmitter } from 'events'
2
+ import { sample } from '../utils'
3
3
import { noop , defaults } from '../utils/lodash'
4
- import { IRedisOptions , getNodeKey } from './util'
4
+ import { IRedisOptions , getNodeKey , NodeKey , NodeRole } from './util'
5
5
6
6
const Redis = require ( '../redis' )
7
7
const debug = require ( '../utils/debug' ) ( 'ioredis:cluster:connectionPool' )
@@ -22,11 +22,21 @@ export default class ConnectionPool extends EventEmitter {
22
22
super ( )
23
23
}
24
24
25
- public getNodes ( role : 'all' | 'master' | 'slave' = 'all' ) : any [ ] {
25
+ public getNodes ( role : NodeRole = 'all' ) : any [ ] {
26
26
const nodes = this . nodes [ role ]
27
27
return Object . keys ( nodes ) . map ( ( key ) => nodes [ key ] )
28
28
}
29
29
30
+ public getInstanceByKey ( key : NodeKey ) : any {
31
+ return this . nodes . all [ key ]
32
+ }
33
+
34
+ public getSampleInstance ( role : NodeRole ) : any {
35
+ const keys = Object . keys ( this . nodes [ role ] )
36
+ const sampleKey = sample ( keys )
37
+ return this . nodes [ role ] [ sampleKey ]
38
+ }
39
+
30
40
/**
31
41
* Find or create a connection to the node
32
42
*
@@ -36,7 +46,6 @@ export default class ConnectionPool extends EventEmitter {
36
46
* @memberof ConnectionPool
37
47
*/
38
48
public findOrCreate ( node : IRedisOptions , readOnly : boolean = false ) : any {
39
- fillDefaultOptions ( node )
40
49
const key = getNodeKey ( node )
41
50
readOnly = Boolean ( readOnly )
42
51
@@ -104,28 +113,12 @@ export default class ConnectionPool extends EventEmitter {
104
113
* @param {(Array<string | number | object>) } nodes
105
114
* @memberof ConnectionPool
106
115
*/
107
- public reset ( nodes : Array < string | number | object > ) : void {
116
+ public reset ( nodes : IRedisOptions [ ] ) : void {
108
117
debug ( 'Reset with %O' , nodes ) ;
109
118
const newNodes = { }
110
119
nodes . forEach ( ( node ) => {
111
- const options : IRedisOptions = { }
112
- if ( typeof node === 'object' ) {
113
- Object . assign ( options , node )
114
- } else if ( typeof node === 'string' ) {
115
- Object . assign ( options , parseURL ( node ) )
116
- } else if ( typeof node === 'number' ) {
117
- options . port = node
118
- } else {
119
- throw new Error ( 'Invalid argument ' + node )
120
- }
121
- if ( typeof options . port === 'string' ) {
122
- options . port = parseInt ( options . port , 10 )
123
- }
124
- delete options . db
125
-
126
- fillDefaultOptions ( options )
127
- newNodes [ getNodeKey ( options ) ] = options
128
- } , this )
120
+ newNodes [ getNodeKey ( node ) ] = node
121
+ } )
129
122
130
123
Object . keys ( this . nodes . all ) . forEach ( ( key ) => {
131
124
if ( ! newNodes [ key ] ) {
@@ -139,8 +132,3 @@ export default class ConnectionPool extends EventEmitter {
139
132
} )
140
133
}
141
134
}
142
-
143
- function fillDefaultOptions ( node : IRedisOptions ) : void {
144
- node . port = node . port || 6379
145
- node . host = node . host || '127.0.0.1'
146
- }
0 commit comments