Class: Dalli::Ring

Inherits:
Object
  • Object
show all
Defined in:
lib/dalli/ring.rb

Defined Under Namespace

Classes: Entry

Constant Summary collapse

POINTS_PER_SERVER =

this is the default in libmemcached

160

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers) ⇒ Ring

Returns a new instance of Ring.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dalli/ring.rb', line 10

def initialize(servers)
  @servers = servers
  if servers.size > 1
    total_weight = servers.inject(0) { |memo, srv| memo + srv.weight }
    continuum = []
    servers.each do |server|
      entry_count_for(server, servers.size, total_weight).times do |idx|
        hash = Digest::SHA1.hexdigest("#{server.hostname}:#{server.port}:#{idx}")
        value = Integer("0x#{hash[0..7]}")
        continuum << Dalli::Ring::Entry.new(value, server)
      end
    end
    continuum.sort { |a, b| a.value <=> b.value }
    @continuum = continuum
  end
end

Instance Attribute Details

#continuumObject

Returns the value of attribute continuum.



8
9
10
# File 'lib/dalli/ring.rb', line 8

def continuum
  @continuum
end

#serversObject

Returns the value of attribute servers.



8
9
10
# File 'lib/dalli/ring.rb', line 8

def servers
  @servers
end

Instance Method Details

#lockObject



48
49
50
51
52
53
54
55
# File 'lib/dalli/ring.rb', line 48

def lock
  @servers.each { |s| s.lock! }
  begin
    return yield
  ensure
    @servers.each { |s| s.unlock! }
  end
end

#server_for_key(key) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dalli/ring.rb', line 27

def server_for_key(key)
  return @servers.first unless @continuum

  hkey = hash_for(key)

  20.times do |try|
    entryidx = self.class.binary_search(@continuum, hkey)
    server = @continuum[entryidx].server
    return server if server.alive?
    hkey = hash_for("#{try}#{key}")
  end

  raise Dalli::NetworkError, "No servers available"
end

#threadsafe!Object



42
43
44
45
46
# File 'lib/dalli/ring.rb', line 42

def threadsafe!
  @servers.each do |s|
    s.extend(Dalli::Threadsafe)
  end
end