Skip to content

Commit

Permalink
Improved error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Aug 22, 2024
1 parent 1265ae4 commit c5adad1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
9 changes: 5 additions & 4 deletions cluster/test/async/redis/cluster_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@
end

it "can map every slot to a client" do
Async::Redis::ClusterClient::HASH_SLOTS.times do |slot|
clients = Async::Redis::ClusterClient::HASH_SLOTS.times.map do |slot|
client = cluster.client_for(slot)

expect(client).not.to be_nil
end
end.uniq

expect(clients.size).to be == 3
expect(clients).not.to have_value(be_nil)
end
end
11 changes: 8 additions & 3 deletions lib/async/redis/cluster_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class ClusterClient
class ReloadError < StandardError
end

class SlotError < StandardError
end

Node = Struct.new(:id, :endpoint, :role, :health, :client)

class RangeMap
Expand Down Expand Up @@ -84,9 +87,11 @@ def client_for(slot, role = :master)
reload_cluster!
end

nodes = @shards.find(slot)

nodes = nodes.select{|node| node.role == role}
if nodes = @shards.find(slot)
nodes = nodes.select{|node| node.role == role}
else
raise SlotError, "No nodes found for slot #{slot}"
end

if node = nodes.sample
return (node.client ||= Client.new(node.endpoint))
Expand Down

0 comments on commit c5adad1

Please sign in to comment.