@@ -89,12 +89,13 @@ def next_event(timeout = nil) # rubocop:disable Metrics/AbcSize, Metrics/Cycloma
89
89
90
90
case event = @queue . pop ( true )
91
91
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' )
96
93
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
98
99
when StandardError then raise event
99
100
when Array then break event
100
101
end
@@ -114,25 +115,20 @@ def _call(command)
114
115
end
115
116
end
116
117
117
- def call_to_single_state ( command , retry_count : 1 )
118
+ def call_to_single_state ( command )
118
119
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
127
125
end
128
126
129
127
def call_to_all_states ( command )
130
128
@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
136
132
end
137
133
end
138
134
@@ -152,10 +148,27 @@ def calc_max_duration(timeout)
152
148
timeout . nil? || timeout < 0 ? 0 : timeout * 1_000_000
153
149
end
154
150
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
+
155
160
def start_over
156
161
@state_dict . each_value ( &:close )
157
162
@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
+
159
172
nil
160
173
end
161
174
end
0 commit comments