Skip to content

Instantly share code, notes, and snippets.

@jdunphy
Created November 5, 2009 19:20
Show Gist options
  • Save jdunphy/227307 to your computer and use it in GitHub Desktop.
Save jdunphy/227307 to your computer and use it in GitHub Desktop.
##
# Pick a server to handle the request based on a hash of the key.
def get_server_for_key(key, options = {})
raise ArgumentError, "illegal character in key #{key.inspect}" if
key =~ /\s/
raise ArgumentError, "key too long #{key.inspect}" if key.length > 250
raise MemCacheError, "No servers available" if @servers.empty?
return @servers.first if @servers.length == 1
hkey = hash_for(key)
20.times do |try|
entryidx = Continuum.binary_search(@continuum, hkey)
server = @continuum[entryidx].server
return server if server.alive?
break unless failover
hkey = hash_for "#{try}#{key}"
end
raise MemCacheError, "No servers available"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment