Skip to content

Commit 38311a8

Browse files
authored
Refactor redirection error handling into one block (#307)
2 parents 7cafb29 + 81f435f commit 38311a8

File tree

1 file changed

+14
-34
lines changed

1 file changed

+14
-34
lines changed

lib/redis_client/cluster/router.rb

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -72,45 +72,25 @@ def send_command(method, command, *args, &block) # rubocop:disable Metrics/AbcSi
7272
end
7373

7474
# @see https://redis.io/docs/reference/cluster-spec/#redirection-and-resharding Redirection and resharding
75-
def try_send(node, method, command, args, retry_count: 3, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
76-
if args.empty?
77-
# prevent memory allocation for variable-length args
78-
node.public_send(method, command, &block)
79-
else
80-
node.public_send(method, *args, command, &block)
81-
end
82-
rescue ::RedisClient::CircuitBreaker::OpenCircuitError
83-
raise
84-
rescue ::RedisClient::CommandError => e
85-
if e.message.start_with?('MOVED')
86-
node = assign_redirection_node(e.message)
87-
retry_count -= 1
88-
retry if retry_count >= 0
89-
elsif e.message.start_with?('ASK')
90-
node = assign_asking_node(e.message)
91-
retry_count -= 1
92-
if retry_count >= 0
93-
# Don't actually prepend our next command with ASKING unless we're going to retry.
94-
node.call('ASKING')
95-
retry
75+
def try_send(node, method, command, args, retry_count: 3, &block)
76+
handle_redirection(node, retry_count: retry_count) do |on_node|
77+
if args.empty?
78+
# prevent memory allocation for variable-length args
79+
on_node.public_send(method, command, &block)
80+
else
81+
on_node.public_send(method, *args, command, &block)
9682
end
97-
elsif e.message.start_with?('CLUSTERDOWN Hash slot not served')
98-
update_cluster_info!
99-
retry_count -= 1
100-
retry if retry_count >= 0
10183
end
102-
raise
103-
rescue ::RedisClient::ConnectionError => e
104-
update_cluster_info!
105-
106-
raise if retry_count <= 0
84+
end
10785

108-
retry_count -= 1
109-
retry
86+
def try_delegate(node, method, *args, retry_count: 3, **kwargs, &block)
87+
handle_redirection(node, retry_count: retry_count) do |on_node|
88+
on_node.public_send(method, *args, **kwargs, &block)
89+
end
11090
end
11191

112-
def try_delegate(node, method, *args, retry_count: 3, **kwargs, &block) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
113-
node.public_send(method, *args, **kwargs, &block)
92+
def handle_redirection(node, retry_count:) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
93+
yield node
11494
rescue ::RedisClient::CircuitBreaker::OpenCircuitError
11595
raise
11696
rescue ::RedisClient::CommandError => e

0 commit comments

Comments
 (0)