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, options) ⇒ Ring

Returns a new instance of Ring.



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

def initialize(servers, options)
  @servers = servers
  @continuum = nil
  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 = continuum.sort { |a, b| a.value <=> b.value }
  end

  threadsafe! unless options[:threadsafe] == false
  @failover = options[:failover] != false
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

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/dalli/ring.rb', line 30

def server_for_key(key)
  if @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?
      break unless @failover
      hkey = hash_for("#{try}#{key}")
    end
  else
    server = @servers.first
    return server if server && server.alive?
  end

  raise Dalli::RingError, "No server available"
end