Skip to content

Commit 35057cd

Browse files
authored
fix: client should handle redirection for sharded pubsub (#226)
1 parent e704d40 commit 35057cd

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

lib/redis_client/cluster/pub_sub.rb

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'redis_client'
4+
35
class RedisClient
46
class Cluster
57
class PubSub
@@ -76,8 +78,26 @@ def next_event(timeout = nil)
7678

7779
def _call(command)
7880
node_key = @router.find_node_key(command)
79-
@states[node_key] = State.new(@router.find_node(node_key).pubsub) unless @states.key?(node_key)
81+
add_state(node_key)
82+
try_call(node_key, command)
83+
end
84+
85+
def try_call(node_key, command, retry_count: 1)
8086
@states[node_key].call(command)
87+
rescue ::RedisClient::CommandError => e
88+
raise if !e.message.start_with?('MOVED') || retry_count <= 0
89+
90+
# for sharded pub/sub
91+
node_key = e.message.split[2]
92+
add_state(node_key)
93+
retry_count -= 1
94+
retry
95+
end
96+
97+
def add_state(node_key)
98+
return @states[node_key] if @states.key?(node_key)
99+
100+
@states[node_key] = State.new(@router.find_node(node_key).pubsub)
81101
end
82102

83103
def obtain_current_time

0 commit comments

Comments
 (0)