lib/redis_failover/client.rb in nogara-redis_failover-1.0.5 vs lib/redis_failover/client.rb in nogara-redis_failover-1.0.6

- old
+ new

@@ -54,10 +54,11 @@ # @option options [Logger] :logger logger override # @option options [Boolean] :retry_failure indicates if failures are retried # @option options [Integer] :max_retries max retries for a failure # @option options [Boolean] :safe_mode indicates if safe mode is used or not # @option options [Boolean] :master_only indicates if only redis master is used + # @option options [Boolean] :verify_role verify the actual role of a redis node before every command # @note Use either :zkservers or :zk # @return [RedisFailover::Client] def initialize(options = {}) Util.logger = options[:logger] if options[:logger] @master = nil @@ -239,10 +240,11 @@ logger.error("Error while handling `#{method}` - #{ex.inspect}") logger.error(ex.backtrace.join("\n")) if tries < @max_retries tries += 1 + free_client build_clients sleep(RETRY_WAIT_TIME) retry end raise @@ -255,11 +257,11 @@ # # @return [Redis] the Redis client for the current master # @raise [NoMasterError] if no master is available def master if master = @lock.synchronize { @master } - verify_role!(master, :master) + verify_role!(master, :master) if @verify_role return master end raise NoMasterError end @@ -269,11 +271,11 @@ # @return [Redis] the Redis client for the slave or master # @raise [NoMasterError] if no master fallback is available def slave # pick a slave, if none available fallback to master if slave = @lock.synchronize { @slaves.shuffle.first } - verify_role!(slave, :slave) + verify_role!(slave, :slave) if @verify_role return slave end master end @@ -341,11 +343,11 @@ nodes.map do |node| host, port = node.split(':') opts = {:host => host, :port => port} opts.update(:db => @db) if @db opts.update(:password => @password) if @password - client = Redis.new(opts) + client = Redis.new(@redis_client_options.merge(opts)) if @namespace client = Redis::Namespace.new(@namespace, :redis => client) end @node_addresses[client] = node client @@ -498,9 +500,14 @@ @db = options[:db] @retry = options.fetch(:retry_failure, true) @max_retries = @retry ? options.fetch(:max_retries, 3) : 0 @safe_mode = options.fetch(:safe_mode, true) @master_only = options.fetch(:master_only, false) + @verify_role = options.fetch(:verify_role, true) + + @redis_client_options = Redis::Client::DEFAULTS.keys.each_with_object({}) do |key, hash| + hash[key] = options[key] + end end # @return [String] the znode path for the master redis nodes config def redis_nodes_path "#{@root_znode}/nodes"