Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

RUBY-444 keep multiple replica set connections separate

  • Loading branch information...
commit 5591e3dfe3f773a07e2b1bd0642a059f14a2316d 1 parent 3e04550
@banker banker authored
Showing with 26 additions and 9 deletions.
  1. +16 −9 lib/mongo/repl_set_connection.rb
  2. +10 −0 test/replica_sets/basic_test.rb
View
25 lib/mongo/repl_set_connection.rb
@@ -25,7 +25,7 @@ class ReplSetConnection < Connection
: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 @@ def connect
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 @@ def hard_refresh!
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 @@ def close(opts={})
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 @@ def close_socket(socket)
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 @@ def get_socket_from_pool(pool_type)
return nil
end
end
-
+
def local_manager
- Thread.current[:manager]
+ Thread.current[:managers][self]
end
def arbiters
View
10 test/replica_sets/basic_test.rb
@@ -25,6 +25,16 @@ def test_connect
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)
Please sign in to comment.
Something went wrong with that request. Please try again.