lib/redis_cluster/client.rb in redis_cluster-0.2.1 vs lib/redis_cluster/client.rb in redis_cluster-0.2.3

- old
+ new

@@ -1,13 +1,15 @@ +require "thread" + module RedisCluster class Client def initialize(startup_hosts, global_configs = {}) @startup_hosts = startup_hosts @pool = Pool.new - @locking = false + @mutex = Mutex.new reload_pool_nodes end def execute(method, args) ttl = Configuration::REQUEST_TTL @@ -46,27 +48,26 @@ end private def reload_pool_nodes - return if @locking - @locking = true - @startup_hosts.each do |options| - begin - redis = Node.redis(options) - slots_mapping = redis.cluster("slots").group_by{|x| x[2]} - @pool.delete_except!(slots_mapping.keys) - slots_mapping.each do |host, infos| - slots_ranges = infos.map {|x| x[0]..x[1] } - @pool.add_node!({host: host[0], port: host[1]}, slots_ranges) + @mutex.synchronize do + @startup_hosts.each do |options| + begin + redis = Node.redis(options) + slots_mapping = redis.cluster("slots").group_by{|x| x[2]} + @pool.delete_except!(slots_mapping.keys) + slots_mapping.each do |host, infos| + slots_ranges = infos.map {|x| x[0]..x[1] } + @pool.add_node!({host: host[0], port: host[1]}, slots_ranges) + end + rescue + next end - rescue - next + break end - break + fresh_startup_nodes end - @locking = false - fresh_startup_nodes end def fresh_startup_nodes @pool.nodes.each {|node| @startup_hosts.push(node.host_hash) } @startup_hosts.uniq!