Connection#end_request

This commit is contained in:
Kyle Banker 2011-11-16 13:06:56 -05:00
parent fe58da0e3c
commit e61f867e65
4 changed files with 54 additions and 2 deletions

View File

@ -501,6 +501,30 @@ module Mongo
self.connections[self.object_id][:writer] ||= checkout_writer
end
# Allow the current threads connection to return to the pool.
#
# Calling this method allows the socket that has been reserved
# for this thread to be returned to the pool. Other threads will
# then be able to re-use that socket. If your application uses many
# threads, or has long-running threads that infrequently perform MongoDB
# operations, then judicious use of this method can lead to performance gains.
# Care should be taken, however, to make sure that end_request is not called
# in the middle of a sequence of operations in which ordering is important. This
# could lead to unexpected results.
#
# One important case is when a thread is dying permanently. It is best to call
# end_request when you know a thread is finished, as otherwise its socket will
# not be reclaimed.
def end_request
if socket = self.connections[self.object_id][:reader]
checkin(socket)
end
if socket = self.connections[self.object_id][:writer]
checkin(socket)
end
end
# Used to close, check in, or refresh sockets held
# in thread-local variables.
def local_socket_done(socket)

View File

@ -339,6 +339,30 @@ module Mongo
@threads_to_sockets[Thread.current][:writer] = socket
end
# Allow the current threads connection to return to the pool.
#
# Calling this method allows the socket that has been reserved
# for this thread to be returned to the pool. Other threads will
# then be able to re-use that socket. If your application uses many
# threads, or has long-running threads that infrequently perform MongoDB
# operations, then judicious use of this method can lead to performance gains.
# Care should be taken, however, to make sure that end_request is not called
# in the middle of a sequence of operations in which ordering is important. This
# could lead to unexpected results.
#
# One important case is when a thread is dying permanently. It is best to call
# end_request when you know a thread is finished, as otherwise its socket will
# not be reclaimed.
def end_request
if socket = self.connections[self.object_id][:reader]
checkin(socket)
end
if socket = self.connections[self.object_id][:writer]
checkin(socket)
end
end
# Used to close, check in, or refresh sockets held
# in thread-local variables.
def local_socket_done(socket)

View File

@ -263,7 +263,7 @@ module Mongo
end
@connection_mutex.synchronize do
if @size > 10
if @size > 1000
if @sockets.size > 0.7 * @size
@sockets_low = true
else

View File

@ -4,7 +4,8 @@ class TestThreading < Test::Unit::TestCase
include Mongo
@@db = standard_connection(:pool_size => 10, :timeout => 30).db(MONGO_TEST_DB)
@@con = standard_connection(:pool_size => 10, :timeout => 30)
@@db = @@con[MONGO_TEST_DB]
@@coll = @@db.collection('thread-test-collection')
def set_up_safe_data
@ -38,6 +39,7 @@ class TestThreading < Test::Unit::TestCase
@duplicate.update({"test" => "insert"}, {"$set" => {"test" => "update"}}, :safe => true)
times << Time.now - t1
end
@@con.end_request
end
end
end
@ -59,6 +61,7 @@ class TestThreading < Test::Unit::TestCase
else
@duplicate.insert({"test" => "insert"}, :safe => true)
end
@@con.end_request
end
end
@ -84,6 +87,7 @@ class TestThreading < Test::Unit::TestCase
sum += document["x"]
end
assert_equal 499500, sum
@@con.end_request
end
end