lib/redis_failover/client.rb in nogara-redis_failover-0.9.7.3 vs lib/redis_failover/client.rb in nogara-redis_failover-0.9.7.4
- old
+ new
@@ -38,19 +38,21 @@
# Creates a new failover redis client.
#
# @param [Hash] options the options used to initialize the client instance
# @option options [String] :zkservers comma-separated ZooKeeper host:port
+ # @option options [String] :zk an existing ZK client connection instance
# @option options [String] :znode_path znode path override for redis nodes
# @option options [String] :password password for redis nodes
# @option options [String] :db database to use for redis nodes
# @option options [String] :namespace namespace for redis nodes
# @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
+ # @note Use either :zkservers or :zk
# @return [RedisFailover::Client]
def initialize(options = {})
Util.logger = options[:logger] if options[:logger]
@master = nil
@slaves = []
@@ -175,12 +177,12 @@
private
# Sets up the underlying ZooKeeper connection.
def setup_zk
- @zk = ZK.new(@zkservers)
- @zk.watcher.register(redis_nodes_path) { |event| handle_zk_event(event) }
+ @zk = ZK.new(@zkservers) if @zkservers
+ @zk.register(redis_nodes_path) { |event| handle_zk_event(event) }
if @safe_mode
@zk.on_expired_session { purge_clients }
end
@zk.on_connected { @zk.stat(redis_nodes_path, :watch => true) }
@zk.stat(redis_nodes_path, :watch => true)
@@ -473,10 +475,14 @@
# Parses the configuration operations.
#
# @param [Hash] options the configuration options
def parse_options(options)
- @zkservers = options.fetch(:zkservers) { raise ArgumentError, ':zkservers required'}
+ @zk, @zkservers = options.values_at(:zk, :zkservers)
+ if [@zk, @zkservers].all? || [@zk, @zkservers].none?
+ raise ArgumentError, 'must specify :zk or :zkservers'
+ end
+
@root_znode = options.fetch(:znode_path, Util::DEFAULT_ROOT_ZNODE_PATH)
@namespace = options[:namespace]
@password = options[:password]
@db = options[:db]
@retry = options.fetch(:retry_failure, true)