@@ -89,12 +89,13 @@ def next_event(timeout = nil) # rubocop:disable Metrics/AbcSize, Metrics/Cycloma
8989
9090 case event = @queue . pop ( true )
9191 when ::RedisClient ::CommandError
92- if event . message . start_with? ( 'MOVED' , 'CLUSTERDOWN Hash slot not served' )
93- @router . renew_cluster_state
94- break start_over
95- end
92+ raise event unless event . message . start_with? ( 'MOVED' , 'CLUSTERDOWN Hash slot not served' )
9693
97- raise event
94+ @router . renew_cluster_state
95+ break start_over
96+ when ::RedisClient ::ConnectionError
97+ @router . renew_cluster_state
98+ break start_over
9899 when StandardError then raise event
99100 when Array then break event
100101 end
@@ -114,25 +115,20 @@ def _call(command)
114115 end
115116 end
116117
117- def call_to_single_state ( command , retry_count : 1 )
118+ def call_to_single_state ( command )
118119 node_key = @router . find_node_key ( command )
119- @state_dict [ node_key ] ||= State . new ( @router . find_node ( node_key ) . pubsub , @queue )
120- @state_dict [ node_key ] . call ( command )
121- rescue ::RedisClient ::ConnectionError
122- @state_dict [ node_key ] . close
123- @state_dict . delete ( node_key )
124- @router . renew_cluster_state
125- retry_count -= 1
126- retry_count >= 0 ? retry : raise
120+
121+ handle_connection_error ( node_key ) do
122+ @state_dict [ node_key ] ||= State . new ( @router . find_node ( node_key ) . pubsub , @queue )
123+ @state_dict [ node_key ] . call ( command )
124+ end
127125 end
128126
129127 def call_to_all_states ( command )
130128 @state_dict . each do |node_key , state |
131- state . call ( command )
132- rescue ::RedisClient ::ConnectionError
133- @state_dict [ node_key ] . close
134- @state_dict . delete ( node_key )
135- @router . renew_cluster_state
129+ handle_connection_error ( node_key , ignore : true ) do
130+ state . call ( command )
131+ end
136132 end
137133 end
138134
@@ -152,10 +148,27 @@ def calc_max_duration(timeout)
152148 timeout . nil? || timeout < 0 ? 0 : timeout * 1_000_000
153149 end
154150
151+ def handle_connection_error ( node_key , ignore : false )
152+ yield
153+ rescue ::RedisClient ::ConnectionError
154+ @state_dict [ node_key ] . close
155+ @state_dict . delete ( node_key )
156+ @router . renew_cluster_state
157+ raise unless ignore
158+ end
159+
155160 def start_over
156161 @state_dict . each_value ( &:close )
157162 @state_dict . clear
158- @commands . each { |command | _call ( command ) }
163+ @commands . each do |command |
164+ loop do
165+ _call ( command )
166+ break
167+ rescue ::RedisClient ::ConnectionError
168+ sleep 1.0
169+ end
170+ end
171+
159172 nil
160173 end
161174 end
0 commit comments