Skip to content

Commit

Permalink
must use Connection.paired for paired connections
Browse files Browse the repository at this point in the history
  • Loading branch information
banker committed Apr 7, 2010
1 parent e18d2d6 commit 910a82d
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 48 deletions.
1 change: 1 addition & 0 deletions HISTORY
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* Removed the following deprecated items:
* GridStore class
* RegexpOfHolding class
* Paired connections must now be initialized with Connection.paired

* BSON-related code extracted into two separate gems: bson and bson_ext (thx to Chuck Remes).
* mongo_ext no longer exists.
Expand Down
1 change: 0 additions & 1 deletion ext/cbson/cbson.c
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,6 @@ void Init_cbson() {
rb_require("bson/types/min_max_keys");
MinKey = rb_const_get(bson, rb_intern("MinKey"));
MaxKey = rb_const_get(bson, rb_intern("MaxKey"));
rb_require("bson/types/regexp_of_holding");
Regexp = rb_const_get(rb_cObject, rb_intern("Regexp"));
rb_require("bson/exceptions");
InvalidKeyName = rb_const_get(bson, rb_intern("InvalidKeyName"));
Expand Down
35 changes: 5 additions & 30 deletions lib/mongo/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,21 @@ class Connection
# If connecting to just one server, you may specify whether connection to slave is permitted.
# In all cases, the default host is "localhost" and the default port is 27017.
#
# When specifying a pair, +pair_or_host+, is a hash with two keys: :left and :right. Each key maps to either
# * a server name, in which case port is 27017,
# * a port number, in which case the server is "localhost", or
# * an array containing [server_name, port_number]
# To specify a pair, use Connection.paired.
#
# Note that there are a few issues when using connection pooling with Ruby 1.9 on Windows. These
# should be resolved in the next release.
#
# @param [String, Hash] pair_or_host See explanation above.
# @param [Integer] port specify a port number here if only one host is being specified. Leave nil if
# specifying a pair of servers in +pair_or_host+.
# @param [String, Hash] host.
# @param [Integer] port specify a port number here if only one host is being specified.
#
# @option options [Boolean] :slave_ok (false) Must be set to +true+ when connecting
# to a single, slave node.
# @option options [Logger, #debug] :logger (nil) Logger instance to receive driver operation log.
# @option options [Boolean] :auto_reconnect DEPRECATED. See http://www.mongodb.org/display/DOCS/Replica+Pairs+in+Ruby
# @option options [Integer] :pool_size (1) The maximum number of socket connections that can be opened to the database.
# @option options [Float] :timeout (5.0) When all of the connections to the pool are checked out,
# this is the number of seconds to wait for a new connection to be released before throwing an exception.
#
#
# @example localhost, 27017
# Connection.new
#
Expand All @@ -77,25 +71,16 @@ class Connection
# @example localhost, 3000, where this node may be a slave
# Connection.new("localhost", 3000, :slave_ok => true)
#
# @example DEPRECATED. To initialize a paired connection, use Connection.paired instead.
# Connection.new({:left => ["db1.example.com", 27017],
# :right => ["db2.example.com", 27017]})
#
# @example DEPRECATED. To initialize a paired connection, use Connection.paired instead.
# Connection.new({:left => ["db1.example.com", 27017],
# :right => ["db2.example.com", 27017]}, nil,
# :pool_size => 20, :timeout => 5)
#
# @see http://www.mongodb.org/display/DOCS/Replica+Pairs+in+Ruby Replica pairs in Ruby
#
# @core connections
def initialize(pair_or_host=nil, port=nil, options={})
def initialize(host=nil, port=nil, options={})
@auths = []

if block_given?
@nodes = yield self
else
@nodes = format_pair(pair_or_host, port)
@nodes = format_pair(host, port)
end

# Host and port of current master.
Expand All @@ -118,10 +103,6 @@ def initialize(pair_or_host=nil, port=nil, options={})
@sockets = []
@checked_out = []

if options[:auto_reconnect]
warn(":auto_reconnect is deprecated. see http://www.mongodb.org/display/DOCS/Replica+Pairs+in+Ruby")
end

# slave_ok can be true only if one node is specified
@slave_ok = options[:slave_ok] && @nodes.length == 1
@logger = options[:logger] || nil
Expand Down Expand Up @@ -479,12 +460,6 @@ def format_pair(pair_or_host, port)
case pair_or_host
when String
[[pair_or_host, port ? port.to_i : DEFAULT_PORT]]
when Hash
warn "Initializing a paired connection with Connection.new is deprecated. Use Connection.pair instead."
connections = []
connections << pair_val_to_connection(pair_or_host[:left])
connections << pair_val_to_connection(pair_or_host[:right])
connections
when nil
[['localhost', DEFAULT_PORT]]
end
Expand Down
18 changes: 3 additions & 15 deletions test/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,23 +121,11 @@ def test_drop_database
end

def test_nodes
db = Connection.new({:left => ['foo', 123]}, nil, :connect => false)
nodes = db.nodes
assert_equal 2, db.nodes.length
assert_equal ['foo', 123], nodes[0]
assert_equal ['localhost', Connection::DEFAULT_PORT], nodes[1]

db = Connection.new({:right => 'bar'}, nil, :connect => false)
nodes = db.nodes
assert_equal 2, nodes.length
assert_equal ['localhost', Connection::DEFAULT_PORT], nodes[0]
assert_equal ['bar', Connection::DEFAULT_PORT], nodes[1]

db = Connection.new({:right => ['foo', 123], :left => 'bar'}, nil, :connect => false)
db = Connection.paired([['foo', 27017], ['bar', 27018]], :connect => false)
nodes = db.nodes
assert_equal 2, nodes.length
assert_equal ['bar', Connection::DEFAULT_PORT], nodes[0]
assert_equal ['foo', 123], nodes[1]
assert_equal ['foo', 27017], nodes[0]
assert_equal ['bar', 27018], nodes[1]
end

context "Saved authentications" do
Expand Down
4 changes: 2 additions & 2 deletions test/db_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ def test_collections
def test_pair
@@conn.close
@@users = nil
@@conn = Connection.new({:left => "this-should-fail", :right => [@@host, @@port]})
@@conn = Connection.paired([["this-should-fail", 27017], [@@host, @@port]])
@@db = @@conn[MONGO_TEST_DB]
assert @@conn.connected?
ensure
unless @@conn.connected?
@@conn = Connection.new(@@host, @@port)
@@conn = Connection.new(@@host, @@port)
@@db = @@conn.db(MONGO_TEST_DB)
end
@@users = @@db.collection('system.users')
Expand Down

0 comments on commit 910a82d

Please sign in to comment.