@@ -49,12 +49,14 @@ def get_cluster_nodes(nodes)
4949 host , port = ip_port . split ( ':' )
5050 flags = flags . split ( ',' )
5151 flags . delete ( 'myself' )
52- ret [ id ] = {
53- host : host ,
54- port : port . to_i ,
55- name : "#{ host } :#{ port } " ,
56- flags : flags
57- }
52+ if flags . include? ( 'master' ) or flags . include? ( 'slave' )
53+ ret [ id ] = {
54+ host : host ,
55+ port : port . to_i ,
56+ name : "#{ host } :#{ port } " ,
57+ flags : flags
58+ }
59+ end
5860 end
5961 return ret
6062 end
@@ -117,6 +119,7 @@ def send_cluster_command(argv)
117119 raise e
118120 end
119121 rescue Redis ::ConnectionError => e
122+ close_connection ( redis )
120123 try_random_connection = true
121124 end
122125 end
@@ -132,7 +135,7 @@ def get_random_connection
132135 node = @nodes [ node_id ]
133136 conn = Redis . new ( node [ :host ] , node [ :port ] )
134137 if conn . ping == "PONG"
135- close_existing_connection
138+ close_existing_connections
136139 @connections [ node_id ] = conn
137140 return conn
138141 else
@@ -142,7 +145,8 @@ def get_random_connection
142145 return conn if conn . ping == "PONG"
143146 end
144147 rescue => e
145- # Just try with the next node.
148+ # Try with the next node
149+ close_connection ( conn ) unless conn . nil?
146150 end
147151 end
148152 raise "Error: failed to get random connection (#{ e } )"
@@ -152,25 +156,35 @@ def get_connection_by(slot)
152156 node_id = @slots [ slot ]
153157 return get_random_connection if node_id . nil?
154158
155- if ! @connections [ node_id ]
156- close_existing_connection
159+ unless @connections [ node_id ]
160+ close_existing_connections
157161 node = @nodes [ node_id ]
158- @connections [ node_id ] = Redis . new ( node [ :host ] , node [ :port ] )
162+ begin
163+ @connections [ node_id ] = Redis . new ( node [ :host ] , node [ :port ] )
164+ rescue
165+ return get_random_connection
166+ end
159167 end
160168
161169 @connections [ node_id ]
162170 end
163171
164- def close_existing_connection
172+ def close_connection ( conn )
173+ raise TypeError unless conn . instance_of? ( Redis )
174+ @connections . delete_if { |i , c | c . host == conn . host && c . port == conn . port }
175+ conn . close
176+ end
177+ 178+ def close_existing_connections
165179 while @connections . length > @max_cached_connections
166180 id , conn = @connections . shift
167- conn . close
181+ close_connection ( conn )
168182 end
169183 end
170184
171185 def close_all_connections
172186 @connections . each do |id , conn |
173- conn . close
187+ close_connection ( conn )
174188 end
175189 @connections . clear
176190 end
0 commit comments