Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: broken behavior for Pub/Sub #220

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions lib/redis_client/cluster/pub_sub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(router, command_builder)
@router = router
@command_builder = command_builder
@pubsub_states = {}
@messages = []
end

def call(*args, **kwargs)
Expand All @@ -22,15 +23,15 @@ def call_v(command)
def close
@pubsub_states.each_value(&:close)
@pubsub_states.clear
@messages.clear
end

def next_event(timeout = nil)
return if @pubsub_states.empty?
return @messages.shift unless @messages.empty?

msgs = collect_messages(timeout).compact
return msgs.first if msgs.size < 2

msgs
collect_messages(timeout)
@messages.shift
end

private
Expand All @@ -45,8 +46,8 @@ def _call(command)
pubsub.call_v(command)
end

def collect_messages(timeout) # rubocop:disable Metrics/AbcSize
@pubsub_states.each_slice(MAX_THREADS).each_with_object([]) do |chuncked_pubsub_states, acc|
def collect_messages(timeout)
@pubsub_states.each_slice(MAX_THREADS) do |chuncked_pubsub_states|
threads = chuncked_pubsub_states.map do |_, v|
Thread.new(v) do |pubsub|
Thread.current[:reply] = pubsub.next_event(timeout)
Expand All @@ -57,7 +58,7 @@ def collect_messages(timeout) # rubocop:disable Metrics/AbcSize

threads.each do |t|
t.join
acc << t[:reply] unless t[:reply].nil?
@messages << t[:reply] unless t[:reply].nil?
end
end
end
Expand Down
18 changes: 6 additions & 12 deletions test/redis_client/test_cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,8 @@ def test_global_pubsub_with_multiple_channels

sub = Fiber.new do |pubsub|
pubsub.call('SUBSCRIBE', *Array.new(10) { |i| "g-chan#{i}" })
assert_equal(
Array.new(10) { |i| ['subscribe', "g-chan#{i}", i + 1] },
collect_messages(pubsub).sort_by { |e| e[1].to_s }
)
got = collect_messages(pubsub).sort_by { |e| e[1].to_s }
10.times { |i| assert_equal(['subscribe', "g-chan#{i}", i + 1], got[i]) }
Fiber.yield
Fiber.yield(collect_messages(pubsub))
pubsub.call('UNSUBSCRIBE')
Expand All @@ -229,10 +227,8 @@ def test_global_pubsub_with_multiple_channels
cli.pipelined { |pi| 10.times { |i| pi.call('PUBLISH', "g-chan#{i}", i) } }
end

assert_equal(
Array.new(10) { |i| ['message', "g-chan#{i}", i.to_s] },
sub.resume.sort_by { |e| e[1].to_s }
)
got = sub.resume.sort_by { |e| e[1].to_s }
10.times { |i| assert_equal(['message', "g-chan#{i}", i.to_s], got[i]) }
end

def test_sharded_pubsub
Expand Down Expand Up @@ -285,10 +281,8 @@ def test_sharded_pubsub_with_multiple_channels
cli.pipelined { |pi| 10.times { |i| pi.call('SPUBLISH', "s-chan#{i}", i) } }
end

assert_equal(
Array.new(10) { |i| ['smessage', "s-chan#{i}", i.to_s] },
sub.resume.sort_by { |e| e[1].to_s }
)
got = sub.resume.sort_by { |e| e[1].to_s }
10.times { |i| assert_equal(['smessage', "s-chan#{i}", i.to_s], got[i]) }
end

def test_close
Expand Down