RUBY-239 warn if bad options are passed to connection constructors
This commit is contained in:
parent
b99f00486d
commit
8e64c74d7d
|
@ -33,6 +33,8 @@ module Mongo
|
||||||
Thread.abort_on_exception = true
|
Thread.abort_on_exception = true
|
||||||
|
|
||||||
DEFAULT_PORT = 27017
|
DEFAULT_PORT = 27017
|
||||||
|
GENERIC_OPTS = [:ssl, :auths, :pool_size, :pool_timeout, :timeout, :op_timeout, :connect_timeout, :safe, :logger, :connect]
|
||||||
|
CONNECTION_OPTS = [:slave_ok]
|
||||||
|
|
||||||
mongo_thread_local_accessor :connections
|
mongo_thread_local_accessor :connections
|
||||||
|
|
||||||
|
@ -98,9 +100,17 @@ module Mongo
|
||||||
# Host and port of current master.
|
# Host and port of current master.
|
||||||
@host = @port = nil
|
@host = @port = nil
|
||||||
|
|
||||||
# slave_ok can be true only if one node is specified
|
# Default maximum BSON object size
|
||||||
@slave_ok = opts[:slave_ok]
|
@max_bson_size = Mongo::DEFAULT_MAX_BSON_SIZE
|
||||||
|
|
||||||
|
# Lock for request ids.
|
||||||
|
@id_lock = Mutex.new
|
||||||
|
|
||||||
|
# Connection pool for primay node
|
||||||
|
@primary = nil
|
||||||
|
@primary_pool = nil
|
||||||
|
|
||||||
|
check_opts(opts)
|
||||||
setup(opts)
|
setup(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -505,10 +515,22 @@ module Mongo
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
# Generic initialization code.
|
def valid_opts
|
||||||
|
GENERIC_OPTS + CONNECTION_OPTS
|
||||||
|
end
|
||||||
|
|
||||||
|
def check_opts(opts)
|
||||||
|
bad_opts = opts.keys.reject { |opt| valid_opts.include?(opt) }
|
||||||
|
|
||||||
|
unless bad_opts.empty?
|
||||||
|
bad_opts.each {|opt| warn "#{opt} is not a valid option for #{self.class}"}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Parse option hash
|
||||||
def setup(opts)
|
def setup(opts)
|
||||||
# Default maximum BSON object size
|
# slave_ok can be true only if one node is specified
|
||||||
@max_bson_size = Mongo::DEFAULT_MAX_BSON_SIZE
|
@slave_ok = opts[:slave_ok]
|
||||||
|
|
||||||
# Determine whether to use SSL.
|
# Determine whether to use SSL.
|
||||||
@ssl = opts.fetch(:ssl, false)
|
@ssl = opts.fetch(:ssl, false)
|
||||||
|
@ -521,9 +543,6 @@ module Mongo
|
||||||
# Authentication objects
|
# Authentication objects
|
||||||
@auths = opts.fetch(:auths, [])
|
@auths = opts.fetch(:auths, [])
|
||||||
|
|
||||||
# Lock for request ids.
|
|
||||||
@id_lock = Mutex.new
|
|
||||||
|
|
||||||
# Pool size and timeout.
|
# Pool size and timeout.
|
||||||
@pool_size = opts[:pool_size] || 1
|
@pool_size = opts[:pool_size] || 1
|
||||||
if opts[:timeout]
|
if opts[:timeout]
|
||||||
|
@ -541,10 +560,6 @@ module Mongo
|
||||||
# Global safe option. This is false by default.
|
# Global safe option. This is false by default.
|
||||||
@safe = opts[:safe] || false
|
@safe = opts[:safe] || false
|
||||||
|
|
||||||
# Connection pool for primay node
|
|
||||||
@primary = nil
|
|
||||||
@primary_pool = nil
|
|
||||||
|
|
||||||
@logger = opts.fetch(:logger, nil)
|
@logger = opts.fetch(:logger, nil)
|
||||||
|
|
||||||
if @logger
|
if @logger
|
||||||
|
|
|
@ -21,6 +21,8 @@ module Mongo
|
||||||
# Instantiates and manages connections to a MongoDB replica set.
|
# Instantiates and manages connections to a MongoDB replica set.
|
||||||
class ReplSetConnection < Connection
|
class ReplSetConnection < Connection
|
||||||
|
|
||||||
|
REPL_SET_OPTS = [:read, :refresh_mode, :refresh_interval, :require_primary, :read_secondary, :rs_name]
|
||||||
|
|
||||||
attr_reader :replica_set_name, :seeds, :refresh_interval, :refresh_mode,
|
attr_reader :replica_set_name, :seeds, :refresh_interval, :refresh_mode,
|
||||||
:refresh_version
|
:refresh_version
|
||||||
|
|
||||||
|
@ -106,49 +108,27 @@ module Mongo
|
||||||
@seeds.freeze
|
@seeds.freeze
|
||||||
|
|
||||||
# Refresh
|
# Refresh
|
||||||
@refresh_mode = opts.fetch(:refresh_mode, false)
|
|
||||||
@refresh_interval = opts[:refresh_interval] || 90
|
|
||||||
@last_refresh = Time.now
|
@last_refresh = Time.now
|
||||||
@refresh_version = 0
|
@refresh_version = 0
|
||||||
|
|
||||||
# No connection manager by default.
|
# No connection manager by default.
|
||||||
@manager = nil
|
@manager = nil
|
||||||
|
|
||||||
|
# Lock for request ids.
|
||||||
|
@id_lock = Mutex.new
|
||||||
|
|
||||||
@pool_mutex = Mutex.new
|
@pool_mutex = Mutex.new
|
||||||
|
|
||||||
if @refresh_mode == :async
|
|
||||||
warn ":async refresh mode has been deprecated. Refresh
|
|
||||||
mode will be disabled."
|
|
||||||
elsif ![:sync, false].include?(@refresh_mode)
|
|
||||||
raise MongoArgumentError,
|
|
||||||
"Refresh mode must be either :sync or false."
|
|
||||||
end
|
|
||||||
|
|
||||||
# Are we allowing reads from secondaries?
|
|
||||||
if opts[:read_secondary]
|
|
||||||
warn ":read_secondary options has now been deprecated and will " +
|
|
||||||
"be removed in driver v2.0. Use the :read option instead."
|
|
||||||
@read_secondary = opts.fetch(:read_secondary, false)
|
|
||||||
@read = :secondary
|
|
||||||
else
|
|
||||||
@read = opts.fetch(:read, :primary)
|
|
||||||
Mongo::Support.validate_read_preference(@read)
|
|
||||||
end
|
|
||||||
|
|
||||||
@connected = false
|
@connected = false
|
||||||
|
|
||||||
# Replica set name
|
@safe_mutex_lock = Mutex.new
|
||||||
if opts[:rs_name]
|
@safe_mutexes = Hash.new {|hash, key| hash[key] = Mutex.new}
|
||||||
warn ":rs_name option has been deprecated and will be removed in v2.0. " +
|
|
||||||
"Please use :name instead."
|
check_opts(opts)
|
||||||
@replica_set_name = opts[:rs_name]
|
setup(opts)
|
||||||
else
|
|
||||||
@replica_set_name = opts[:name]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Require a primary node to connect?
|
def valid_opts
|
||||||
@require_primary = opts.fetch(:require_primary, true)
|
GENERIC_OPTS + REPL_SET_OPTS
|
||||||
|
|
||||||
setup(opts)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def inspect
|
def inspect
|
||||||
|
@ -466,10 +446,42 @@ module Mongo
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Generic initialization code.
|
# Parse option hash
|
||||||
def setup(opts)
|
def setup(opts)
|
||||||
@safe_mutex_lock = Mutex.new
|
# Require a primary node to connect?
|
||||||
@safe_mutexes = Hash.new {|hash, key| hash[key] = Mutex.new}
|
@require_primary = opts.fetch(:require_primary, true)
|
||||||
|
|
||||||
|
# Refresh
|
||||||
|
@refresh_mode = opts.fetch(:refresh_mode, false)
|
||||||
|
@refresh_interval = opts[:refresh_interval] || 90
|
||||||
|
|
||||||
|
if @refresh_mode == :async
|
||||||
|
warn ":async refresh mode has been deprecated. Refresh
|
||||||
|
mode will be disabled."
|
||||||
|
elsif ![:sync, false].include?(@refresh_mode)
|
||||||
|
raise MongoArgumentError,
|
||||||
|
"Refresh mode must be either :sync or false."
|
||||||
|
end
|
||||||
|
|
||||||
|
# Are we allowing reads from secondaries?
|
||||||
|
if opts[:read_secondary]
|
||||||
|
warn ":read_secondary options has now been deprecated and will " +
|
||||||
|
"be removed in driver v2.0. Use the :read option instead."
|
||||||
|
@read_secondary = opts.fetch(:read_secondary, false)
|
||||||
|
@read = :secondary
|
||||||
|
else
|
||||||
|
@read = opts.fetch(:read, :primary)
|
||||||
|
Mongo::Support.validate_read_preference(@read)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Replica set name
|
||||||
|
if opts[:rs_name]
|
||||||
|
warn ":rs_name option has been deprecated and will be removed in v2.0. " +
|
||||||
|
"Please use :name instead."
|
||||||
|
@replica_set_name = opts[:rs_name]
|
||||||
|
else
|
||||||
|
@replica_set_name = opts[:name]
|
||||||
|
end
|
||||||
|
|
||||||
opts[:connect_timeout] = opts[:connect_timeout] || 30
|
opts[:connect_timeout] = opts[:connect_timeout] || 30
|
||||||
|
|
||||||
|
|
|
@ -26,6 +26,34 @@ class ConnectionTest < Test::Unit::TestCase
|
||||||
should "default slave_ok to false" do
|
should "default slave_ok to false" do
|
||||||
assert !@conn.slave_ok?
|
assert !@conn.slave_ok?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should "warn if invalid options are specified" do
|
||||||
|
conn = Connection.allocate
|
||||||
|
opts = {:connect => false}
|
||||||
|
|
||||||
|
ReplSetConnection::REPL_SET_OPTS.each do |opt|
|
||||||
|
conn.expects(:warn).with("#{opt} is not a valid option for #{conn.class}")
|
||||||
|
opts[opt] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
args = ['localhost', 27017, opts]
|
||||||
|
conn.send(:initialize, *args)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "given a replica set" do
|
||||||
|
should "warn if invalid options are specified" do
|
||||||
|
conn = ReplSetConnection.allocate
|
||||||
|
opts = {:connect => false}
|
||||||
|
|
||||||
|
Connection::CONNECTION_OPTS.each do |opt|
|
||||||
|
conn.expects(:warn).with("#{opt} is not a valid option for #{conn.class}")
|
||||||
|
opts[opt] = true
|
||||||
|
end
|
||||||
|
|
||||||
|
args = [['localhost:27017'], opts]
|
||||||
|
conn.send(:initialize, *args)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "initializing with a mongodb uri" do
|
context "initializing with a mongodb uri" do
|
||||||
|
|
Loading…
Reference in New Issue