Skip to content

Commit 5e3d396

Browse files
author
Sylvester Chin
committed
Pass a randomized array into Command.load
This reduces the dependencies between classes.
1 parent 98cc7ef commit 5e3d396

File tree

4 files changed

+6
-14
lines changed

4 files changed

+6
-14
lines changed

lib/redis_client/cluster/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class << self
2424
def load(nodes)
2525
cmd = errors = nil
2626

27-
nodes&.shuffle_each do |node|
27+
nodes&.each do |node|
2828
reply = node.call('COMMAND')
2929
commands = parse_command_reply(reply)
3030
cmd = ::RedisClient::Cluster::Command.new(commands)

lib/redis_client/cluster/node.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ def each(&block)
201201
@topology.clients.each_value(&block)
202202
end
203203

204-
def shuffle_each(&block)
205-
@topology.clients.values.shuffle!.each(&block)
204+
def shuffled_nodes
205+
@topology.clients.values.shuffle
206206
end
207207

208208
def sample

lib/redis_client/cluster/router.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(config, pool: nil, **kwargs)
2323
@pool = pool
2424
@client_kwargs = kwargs
2525
@node = fetch_cluster_info(@config, pool: @pool, **@client_kwargs)
26-
@command = ::RedisClient::Cluster::Command.load(@node)
26+
@command = ::RedisClient::Cluster::Command.load(@node.shuffled_nodes)
2727
@mutex = Mutex.new
2828
@command_builder = @config.command_builder
2929
end

test/redis_client/cluster/test_command.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@ class RedisClient
77
class Cluster
88
class TestCommand < TestingWrapper
99
def setup
10-
per_node_key = TEST_REDIS_PORTS.map do |port|
11-
[
12-
"#{TEST_REDIS_HOST}:#{port}",
13-
{ host: TEST_REDIS_HOST, port: port }.merge(TEST_GENERIC_OPTIONS)
14-
]
15-
end.to_h
16-
node_info_list = ::RedisClient::Cluster::Node.load_info(per_node_key)
17-
@raw_clients = ::RedisClient::Cluster::Node.new(per_node_key, node_info_list: node_info_list)
18-
@empty_clients = ::RedisClient::Cluster::Node.new([], node_info_list: node_info_list)
10+
@raw_clients = TEST_NODE_URIS.map { |addr| ::RedisClient.config(url: addr, **TEST_GENERIC_OPTIONS).new_client }
1911
end
2012

2113
def teardown
@@ -25,7 +17,7 @@ def teardown
2517
def test_load
2618
[
2719
{ nodes: @raw_clients, error: nil },
28-
{ nodes: @empty_clients, error: ::RedisClient::Cluster::InitialSetupError },
20+
{ nodes: [], error: ::RedisClient::Cluster::InitialSetupError },
2921
{ nodes: [''], error: NoMethodError },
3022
{ nodes: nil, error: ::RedisClient::Cluster::InitialSetupError }
3123
].each_with_index do |c, idx|

0 commit comments

Comments
 (0)