@@ -35,14 +35,17 @@ SentinelConnector.prototype.connect = function (callback) {
35
35
if ( typeof this . currentPoint !== 'number' ) {
36
36
this . currentPoint = - 1 ;
37
37
}
38
+ if ( ! Array . isArray ( this . sentinels ) ) {
39
+ this . sentinels = this . options . sentinels ;
40
+ }
38
41
39
42
var _this = this ;
40
43
var lastError ;
41
44
connectToNext ( ) ;
42
45
43
46
function connectToNext ( ) {
44
47
_this . currentPoint += 1 ;
45
- if ( _this . currentPoint === _this . options . sentinels . length ) {
48
+ if ( _this . currentPoint === _this . sentinels . length ) {
46
49
_this . currentPoint = - 1 ;
47
50
48
51
var retryDelay ;
@@ -62,7 +65,7 @@ SentinelConnector.prototype.connect = function (callback) {
62
65
return ;
63
66
}
64
67
65
- var endpoint = _this . options . sentinels [ _this . currentPoint ] ;
68
+ var endpoint = _this . sentinels [ _this . currentPoint ] ;
66
69
_this . resolve ( endpoint , function ( err , resolved ) {
67
70
if ( ! _this . connecting ) {
68
71
callback ( new Error ( 'Connection is closed.' ) ) ;
@@ -84,13 +87,48 @@ SentinelConnector.prototype.connect = function (callback) {
84
87
}
85
88
} ;
86
89
90
+ SentinelConnector . prototype . updateSentinels = function ( client , callback ) {
91
+ var _this = this ;
92
+ client . sentinel ( 'sentinels' , this . options . name , function ( err , result ) {
93
+ if ( err ) {
94
+ client . disconnect ( ) ;
95
+ return callback ( err ) ;
96
+ }
97
+ if ( Array . isArray ( result ) ) {
98
+ for ( var i = 0 ; i < result . length ; ++ i ) {
99
+ var sentinel = utils . packObject ( result [ i ] ) ;
100
+ var flags = sentinel . flags ? sentinel . flags . split ( ',' ) : [ ] ;
101
+ if ( flags . indexOf ( 'disconnected' ) === - 1 && sentinel . ip && sentinel . port ) {
102
+ var endpoint = { host : sentinel . ip , port : parseInt ( sentinel . port ) } ;
103
+ var isDuplicate = _this . sentinels . some ( function ( o ) {
104
+ return o . host === endpoint . host && o . port === endpoint . port ;
105
+ } ) ;
106
+ if ( ! isDuplicate ) {
107
+ debug ( 'adding sentinel %s:%s' , endpoint . host , endpoint . port ) ;
108
+ _this . sentinels . push ( endpoint ) ;
109
+ }
110
+ }
111
+ }
112
+ debug ( 'sentinels' , _this . sentinels ) ;
113
+ }
114
+ callback ( null ) ;
115
+ } ) ;
116
+ } ;
117
+
87
118
SentinelConnector . prototype . resolveMaster = function ( client , callback ) {
119
+ var _this = this ;
88
120
client . sentinel ( 'get-master-addr-by-name' , this . options . name , function ( err , result ) {
89
- client . disconnect ( ) ;
90
121
if ( err ) {
122
+ client . disconnect ( ) ;
91
123
return callback ( err ) ;
92
124
}
93
- callback ( null , Array . isArray ( result ) ? { host : result [ 0 ] , port : result [ 1 ] } : null ) ;
125
+ _this . updateSentinels ( client , function ( err ) {
126
+ client . disconnect ( ) ;
127
+ if ( err ) {
128
+ return callback ( err ) ;
129
+ }
130
+ callback ( null , Array . isArray ( result ) ? { host : result [ 0 ] , port : result [ 1 ] } : null ) ;
131
+ } ) ;
94
132
} ) ;
95
133
} ;
96
134
0 commit comments