class Dalli::Ring

Constants

POINTS_PER_SERVER

Attributes

continuum[RW]
servers[RW]

Public Class Methods

new(servers, options) click to toggle source
# 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.name}:#{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

Public Instance Methods

lock() { || ... } click to toggle source
# 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) click to toggle source
# File lib/dalli/ring.rb, line 30
def server_for_key(key)
  if @continuum
    hkey = hash_for(key)
    20.times do |try|
      entryidx = 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

Private Instance Methods

entry_count_for(server, total_servers, total_weight) click to toggle source
# File lib/dalli/ring.rb, line 69
def entry_count_for(server, total_servers, total_weight)
  ((total_servers * POINTS_PER_SERVER * server.weight) / Float(total_weight)).floor
end
hash_for(key) click to toggle source
# File lib/dalli/ring.rb, line 65
def hash_for(key)
  Zlib.crc32(key)
end
threadsafe!() click to toggle source
# File lib/dalli/ring.rb, line 59
def threadsafe!
  @servers.each do |s|
    s.extend(Dalli::Threadsafe)
  end
end