lib/redis_failover/client.rb in redis_failover-0.5.1 vs lib/redis_failover/client.rb in redis_failover-0.5.2

- old
+ new

@@ -69,23 +69,23 @@ # :znode_path - the Znode path override for redis server list (optional) # :password - password for redis nodes (optional) # :namespace - namespace for redis nodes (optional) # :logger - logger override (optional) # :retry_failure - indicate if failures should be retried (default true) - # :max_retries - max retries for a failure (default 5) + # :max_retries - max retries for a failure (default 3) # def initialize(options = {}) Util.logger = options[:logger] if options[:logger] @zkservers = options.fetch(:zkservers) { raise ArgumentError, ':zkservers required'} @znode = options[:znode_path] || Util::DEFAULT_ZNODE_PATH @namespace = options[:namespace] @password = options[:password] @retry = options[:retry_failure] || true - @max_retries = @retry ? options.fetch(:max_retries, 3) : 0 + @max_retries = @retry ? options.fetch(:max_retries, 3) : 1 @master = nil @slaves = [] - @lock = Mutex.new + @lock = Monitor.new setup_zookeeper_client build_clients end def method_missing(method, *args, &block) @@ -110,16 +110,16 @@ def setup_zookeeper_client @zkclient = ZkClient.new(@zkservers) # when session expires, purge client list @zkclient.on_session_expiration do - @lock.synchronize { purge_clients } + purge_clients end # when we are disconnected, purge client list @zkclient.event_handler.register_state_handler(:connecting) do - @lock.synchronize { purge_clients } + purge_clients end # when session is recovered, watch again @zkclient.on_session_recovered do @zkclient.stat(@znode, :watch => true) @@ -129,11 +129,11 @@ @zkclient.watcher.register(@znode) do |event| if event.node_created? || event.node_changed? build_clients elsif event.node_deleted? @zkclient.stat(@znode, :watch => true) - @lock.synchronize { purge_clients } + purge_clients else logger.error("Unknown ZK node event: #{event.inspect}") end end end @@ -152,11 +152,11 @@ slave.send(method, *args, &block) else # direct everything else to master master.send(method, *args, &block) end - rescue *ALL_ERRORS => ex + rescue *CONNECTIVITY_ERRORS => ex logger.error("Error while handling operation `#{method}` - #{ex.message}") logger.error(ex.backtrace.join("\n")) if tries < @max_retries tries += 1 @@ -198,11 +198,11 @@ logger.info("Building new clients for nodes #{nodes}") new_master = new_clients_for(nodes[:master]).first if nodes[:master] new_slaves = new_clients_for(*nodes[:slaves]) @master = new_master @slaves = new_slaves - rescue *ALL_ERRORS => ex + rescue StandardError, *CONNECTIVITY_ERRORS => ex purge_clients logger.error("Failed to fetch nodes from #{@zkservers} - #{ex.message}") logger.error(ex.backtrace.join("\n")) if tries < @max_retries @@ -283,12 +283,14 @@ end end end def purge_clients - logger.info("Purging current redis clients") - disconnect(@master, *@slaves) - @master = nil - @slaves = [] + @lock.synchronize do + logger.info("Purging current redis clients") + disconnect(@master, *@slaves) + @master = nil + @slaves = [] + end end end end