Skip to content

Commit c5adad1

Browse files
committed
Improved error handling.
1 parent 1265ae4 commit c5adad1

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

cluster/test/async/redis/cluster_client.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,11 @@
4545
end
4646

4747
it "can map every slot to a client" do
48-
Async::Redis::ClusterClient::HASH_SLOTS.times do |slot|
48+
clients = Async::Redis::ClusterClient::HASH_SLOTS.times.map do |slot|
4949
client = cluster.client_for(slot)
50-
51-
expect(client).not.to be_nil
52-
end
50+
end.uniq
51+
52+
expect(clients.size).to be == 3
53+
expect(clients).not.to have_value(be_nil)
5354
end
5455
end

lib/async/redis/cluster_client.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class ClusterClient
1313
class ReloadError < StandardError
1414
end
1515

16+
class SlotError < StandardError
17+
end
18+
1619
Node = Struct.new(:id, :endpoint, :role, :health, :client)
1720

1821
class RangeMap
@@ -84,9 +87,11 @@ def client_for(slot, role = :master)
8487
reload_cluster!
8588
end
8689

87-
nodes = @shards.find(slot)
88-
89-
nodes = nodes.select{|node| node.role == role}
90+
if nodes = @shards.find(slot)
91+
nodes = nodes.select{|node| node.role == role}
92+
else
93+
raise SlotError, "No nodes found for slot #{slot}"
94+
end
9095

9196
if node = nodes.sample
9297
return (node.client ||= Client.new(node.endpoint))

0 commit comments

Comments
 (0)