Fixed threaded replica set reconnection.

This commit is contained in:
Kyle Banker 2012-02-27 17:11:14 -05:00
parent 6301a41254
commit ebfe279784
2 changed files with 31 additions and 22 deletions

View File

@ -123,6 +123,8 @@ module Mongo
@safe_mutex_lock = Mutex.new @safe_mutex_lock = Mutex.new
@safe_mutexes = Hash.new {|hash, key| hash[key] = Mutex.new} @safe_mutexes = Hash.new {|hash, key| hash[key] = Mutex.new}
@connect_mutex = Mutex.new
check_opts(opts) check_opts(opts)
setup(opts) setup(opts)
end end
@ -139,6 +141,7 @@ module Mongo
# Initiate a connection to the replica set. # Initiate a connection to the replica set.
def connect def connect
log(:info, "Connecting...") log(:info, "Connecting...")
@connect_mutex.synchronize do
return if @connected return if @connected
discovered_seeds = @manager ? @manager.seeds : [] discovered_seeds = @manager ? @manager.seeds : []
@ -157,6 +160,7 @@ module Mongo
@connected = true @connected = true
end end
end end
end
# Determine whether a replica set refresh is # Determine whether a replica set refresh is
# required. If so, run a hard refresh. You can # required. If so, run a hard refresh. You can
@ -312,6 +316,7 @@ module Mongo
if socket if socket
socket socket
else else
@connected = false
raise ConnectionFailure.new("Could not connect to a node for reading.") raise ConnectionFailure.new("Could not connect to a node for reading.")
end end
end end
@ -332,6 +337,7 @@ module Mongo
if socket if socket
socket socket
else else
@connected = false
raise ConnectionFailure.new("Could not connect to a secondary for reading.") raise ConnectionFailure.new("Could not connect to a secondary for reading.")
end end
end end
@ -358,6 +364,7 @@ module Mongo
if socket if socket
socket socket
else else
@connected = false
raise ConnectionFailure.new("Could not connect to primary node.") raise ConnectionFailure.new("Could not connect to primary node.")
end end
end end
@ -507,6 +514,7 @@ module Mongo
end end
def sync_refresh def sync_refresh
@connect_mutex.synchronize do
if @refresh_mode == :sync && if @refresh_mode == :sync &&
((Time.now - @last_refresh) > @refresh_interval) ((Time.now - @last_refresh) > @refresh_interval)
@last_refresh = Time.now @last_refresh = Time.now
@ -514,4 +522,5 @@ module Mongo
end end
end end
end end
end
end end

View File

@ -7,7 +7,7 @@ class ReplicaSetPooledInsertTest < Test::Unit::TestCase
def setup def setup
ensure_rs ensure_rs
@conn = ReplSetConnection.new(build_seeds(3), :pool_size => 5, :timeout => 5, :refresh_mode => false) @conn = ReplSetConnection.new(build_seeds(3), :pool_size => 10, :timeout => 5, :refresh_mode => false)
@db = @conn.db(MONGO_TEST_DB) @db = @conn.db(MONGO_TEST_DB)
@db.drop_collection("test-sets") @db.drop_collection("test-sets")
@coll = @db.collection("test-sets") @coll = @db.collection("test-sets")