@@ -105,6 +105,38 @@ private void ConfigureRedisFromSentinel()
105105 }
106106 }
107107
108+ private Dictionary < string , string > ParseDataArray ( object [ ] items )
109+ {
110+ Dictionary < string , string > data = new Dictionary < string , string > ( ) ;
111+ bool isKey = false ;
112+ string key = null ;
113+ string value = null ;
114+
115+ foreach ( var item in items )
116+ {
117+ if ( item is byte [ ] )
118+ {
119+ isKey = ! isKey ;
120+
121+ if ( isKey )
122+ {
123+ key = Encoding . UTF8 . GetString ( ( byte [ ] ) item ) ;
124+ }
125+ else
126+ {
127+ value = Encoding . UTF8 . GetString ( ( byte [ ] ) item ) ;
128+
129+ if ( ! data . ContainsKey ( key ) )
130+ {
131+ data . Add ( key , value ) ;
132+ }
133+ }
134+ }
135+ }
136+
137+ return data ;
138+ }
139+
108140 /// <summary>
109141 /// Takes output from sentinel slaves command and converts into a list of servers
110142 /// </summary>
@@ -113,69 +145,26 @@ private void ConfigureRedisFromSentinel()
113145 private IEnumerable < string > ConvertSlaveArrayToList ( object [ ] slaves )
114146 {
115147 var servers = new List < string > ( ) ;
116- bool fetchIP = false ;
117- bool fetchPort = false ;
118- bool fetchFlags = false ;
119148 string ip = null ;
120149 string port = null ;
121- string value = null ;
122150 string flags = null ;
123151
124152 foreach ( var slave in slaves . OfType < object [ ] > ( ) )
125153 {
126- fetchIP = false ;
127- fetchPort = false ;
128- ip = null ;
129- port = null ;
154+ var data = ParseDataArray ( slave ) ;
130155
131- foreach ( var item in slave )
132- {
133- if ( item is byte [ ] )
134- {
135- value = Encoding . UTF8 . GetString ( ( byte [ ] ) item ) ;
136- if ( value == "ip" )
137- {
138- fetchIP = true ;
139- continue ;
140- }
141- else if ( value == "port" )
142- {
143- fetchPort = true ;
144- continue ;
145- }
146- else if ( value == "flags" )
147- {
148- fetchFlags = true ;
149- continue ;
150- }
151- else if ( fetchIP )
152- {
153- ip = value ;
154-
155- if ( ip == "127.0.0.1" )
156- {
157- ip = this . sentinelClient . Host ;
158- }
159- fetchIP = false ;
160- }
161- else if ( fetchPort )
162- {
163- port = value ;
164- fetchPort = false ;
165- }
166- else if ( fetchFlags )
167- {
168- flags = value ;
169- fetchFlags = false ;
170-
171- if ( ip != null && port != null && ! flags . Contains ( "s_down" ) )
172- {
173- servers . Add ( "{0}:{1}" . Fmt ( ip , port ) ) ;
174- }
175- }
156+ data . TryGetValue ( "flags" , out flags ) ;
157+ data . TryGetValue ( "ip" , out ip ) ;
158+ data . TryGetValue ( "port" , out port ) ;
176159
160+ if ( ip == "127.0.0.1" )
161+ {
162+ ip = this . sentinelClient . Host ;
163+ }
177164
178- }
165+ if ( ip != null && port != null && ! flags . Contains ( "s_down" ) && ! flags . Contains ( "o_down" ) )
166+ {
167+ servers . Add ( "{0}:{1}" . Fmt ( ip , port ) ) ;
179168 }
180169 }
181170
@@ -190,48 +179,17 @@ private IEnumerable<string> ConvertSlaveArrayToList(object[] slaves)
190179 private IEnumerable < string > ConvertMasterArrayToList ( object [ ] items )
191180 {
192181 var servers = new List < string > ( ) ;
193- bool fetchIP = false ;
194- bool fetchPort = false ;
195182 string ip = null ;
196183 string port = null ;
197- string value = null ;
198184
199- foreach ( var item in items )
200- {
201- if ( item is byte [ ] )
202- {
203- value = Encoding . UTF8 . GetString ( ( byte [ ] ) item ) ;
204- if ( value == "ip" )
205- {
206- fetchIP = true ;
207- continue ;
208- }
209- else if ( value == "port" )
210- {
211- fetchPort = true ;
212- continue ;
213- }
214- else if ( fetchIP )
215- {
216- ip = value ;
217- if ( ip == "127.0.0.1" )
218- {
219- ip = this . sentinelClient . Host ;
220- }
221- fetchIP = false ;
222- }
223- else if ( fetchPort )
224- {
225- port = value ;
226- fetchPort = false ;
227- }
185+ var data = ParseDataArray ( items ) ;
228186
229- if ( ip != null && port != null )
230- {
231- servers . Add ( "{0}:{1}" . Fmt ( ip , port ) ) ;
232- break ;
233- }
234- }
187+ data . TryGetValue ( "ip" , out ip ) ;
188+ data . TryGetValue ( "port" , out port ) ;
189+
190+ if ( ip != null && port != null )
191+ {
192+ servers . Add ( "{0}:{1}" . Fmt ( ip , port ) ) ;
235193 }
236194
237195 return servers ;
0 commit comments