Skip to content

Commit a7d3b3f

Browse files
authored
Fix slot range calculation. (#55)
1 parent 6a75f89 commit a7d3b3f

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

cluster/test/async/redis/cluster_client.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,13 @@
4343

4444
expect(result).to be == value
4545
end
46+
47+
it "can map every slot to a client" do
48+
clients = Async::Redis::ClusterClient::HASH_SLOTS.times.map do |slot|
49+
client = cluster.client_for(slot)
50+
end.uniq
51+
52+
expect(clients.size).to be == 3
53+
expect(clients).not.to have_value(be_nil)
54+
end
4655
end

lib/async/redis/cluster_client.rb

Lines changed: 11 additions & 6 deletions
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))
@@ -106,7 +111,7 @@ def reload_cluster!(endpoints = @endpoints)
106111
shard = shard.each_slice(2).to_h
107112

108113
slots = shard['slots']
109-
range = Range.new(*slots, exclude_end: false)
114+
range = Range.new(*slots)
110115

111116
nodes = shard['nodes'].map do |node|
112117
node = node.each_slice(2).to_h
@@ -179,10 +184,10 @@ def crc16(bytes)
179184
return sum
180185
end
181186

182-
HASH_SLOTS = 16_384
183-
184187
public
185188

189+
HASH_SLOTS = 16_384
190+
186191
# Return Redis::Client for a given key.
187192
# Modified from https://github.com/antirez/redis-rb-cluster/blob/master/cluster.rb#L104-L117
188193
def slot_for(key)

0 commit comments

Comments
 (0)