Class: ConsistentCluster::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/consistent-cluster/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
# File 'lib/consistent-cluster/client.rb', line 10

def initialize(options)

  @cluster = options[:cluster]

  replicas = options[:consistent_hashing_replicas] || 3
  @ring = ConsistentHashing::Ring.new(@cluster.keys,replicas)

  @shard_num = 0
end

Instance Method Details

#shard(key = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/consistent-cluster/client.rb', line 20

def shard(key=nil)
  cluster_sum = @cluster.length
  raise "no service available" if cluster_sum < 1
  if key
    point = @ring.point_for(key)
    server = @cluster[point.node]
  else
    @shard_num += 1
    @shard_num = @shard_num%cluster_sum
    server = @cluster.values[@shard_num]
  end
  server
end