From 5591e3dfe3f773a07e2b1bd0642a059f14a2316d Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Fri, 25 May 2012 13:57:56 -0400 Subject: [PATCH] RUBY-444 keep multiple replica set connections separate --- lib/mongo/repl_set_connection.rb | 25 ++++++++++++++++--------- test/replica_sets/basic_test.rb | 10 ++++++++++ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/mongo/repl_set_connection.rb b/lib/mongo/repl_set_connection.rb index aab385a..b795ac9 100644 --- a/lib/mongo/repl_set_connection.rb +++ b/lib/mongo/repl_set_connection.rb @@ -25,7 +25,7 @@ module Mongo :read_secondary, :rs_name, :name] attr_reader :replica_set_name, :seeds, :refresh_interval, :refresh_mode, - :refresh_version + :refresh_version, :manager # Create a connection to a MongoDB replica set. # @@ -149,8 +149,9 @@ module Mongo discovered_seeds = @manager ? @manager.seeds : [] @manager = PoolManager.new(self, discovered_seeds) - - Thread.current[:manager] = @manager + + Thread.current[:managers] ||= Hash.new + Thread.current[:managers][self] = @manager @manager.connect @refresh_version += 1 @@ -203,7 +204,7 @@ module Mongo new_manager = PoolManager.new(self, discovered_seeds | @seeds) new_manager.connect - Thread.current[:manager] = new_manager + Thread.current[:managers][self] = new_manager # TODO: make sure that connect has succeeded @old_managers << @manager @@ -263,6 +264,12 @@ module Mongo else @manager.close if @manager end + + # Clear the reference to this object. + if Thread.current[:managers] + Thread.current[:managers].delete(self) + end + @connected = false end @@ -395,10 +402,10 @@ module Mongo end def get_socket_from_pool(pool_type) - if Thread.current[:manager] != @manager - Thread.current[:manager] = @manager + if Thread.current[:managers][self] != @manager + Thread.current[:managers][self] = @manager end - + pool = case pool_type when :primary primary_pool @@ -417,9 +424,9 @@ module Mongo return nil end end - + def local_manager - Thread.current[:manager] + Thread.current[:managers][self] end def arbiters diff --git a/test/replica_sets/basic_test.rb b/test/replica_sets/basic_test.rb index 8690dcf..28d1b03 100644 --- a/test/replica_sets/basic_test.rb +++ b/test/replica_sets/basic_test.rb @@ -25,6 +25,16 @@ class BasicTest < Test::Unit::TestCase assert @conn.connected? end + def test_multiple_concurrent_replica_set_connection + @conn1 = ReplSetConnection.new(build_seeds(3), :name => @rs.name) + @conn2 = ReplSetConnection.new(build_seeds(3), :name => @rs.name) + assert @conn1.connected? + assert @conn2.connected? + + assert @conn1.manager != @conn2.manager + assert @conn1.local_manager != @conn2.local_manager + end + def test_cache_original_seed_nodes seeds = build_seeds(3) << "#{@rs.host}:19356" @conn = ReplSetConnection.new(seeds, :name => @rs.name)