lib/async/redis/cluster_client.rb in async-redis-0.10.0 vs lib/async/redis/cluster_client.rb in async-redis-0.10.1

- old
+ new

@@ -11,10 +11,13 @@ module Redis class ClusterClient class ReloadError < StandardError end + class SlotError < StandardError + end + Node = Struct.new(:id, :endpoint, :role, :health, :client) class RangeMap def initialize @ranges = [] @@ -82,14 +85,16 @@ def client_for(slot, role = :master) unless @shards reload_cluster! end - nodes = @shards.find(slot) + if nodes = @shards.find(slot) + nodes = nodes.select{|node| node.role == role} + else + raise SlotError, "No nodes found for slot #{slot}" + end - nodes = nodes.select{|node| node.role == role} - if node = nodes.sample return (node.client ||= Client.new(node.endpoint)) end end @@ -104,11 +109,11 @@ client.call('CLUSTER', 'SHARDS').each do |shard| shard = shard.each_slice(2).to_h slots = shard['slots'] - range = Range.new(*slots, exclude_end: false) + range = Range.new(*slots) nodes = shard['nodes'].map do |node| node = node.each_slice(2).to_h endpoint = Endpoint.remote(node['ip'], node['port']) @@ -177,12 +182,12 @@ end return sum end - HASH_SLOTS = 16_384 - public + + HASH_SLOTS = 16_384 # Return Redis::Client for a given key. # Modified from https://github.com/antirez/redis-rb-cluster/blob/master/cluster.rb#L104-L117 def slot_for(key) key = key.to_s