Add a mechanism to check if the connection is active/healthy.
This commit is contained in:
parent
ef99f49dd9
commit
1ad3285767
|
@ -482,6 +482,22 @@ module Mongo
|
||||||
@primary_pool && @primary_pool.host && @primary_pool.port
|
@primary_pool && @primary_pool.host && @primary_pool.port
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Determine if the connection is active. In a normal case the *server_info* operation
|
||||||
|
# will be performed without issues, but if the connection was dropped by the server or
|
||||||
|
# for some reason the sockets are unsynchronized, a ConnectionFailure will be raised and
|
||||||
|
# the return will be false.
|
||||||
|
#
|
||||||
|
# @return [Boolean]
|
||||||
|
def active?
|
||||||
|
return false unless connected?
|
||||||
|
|
||||||
|
server_info
|
||||||
|
true
|
||||||
|
|
||||||
|
rescue ConnectionFailure
|
||||||
|
false
|
||||||
|
end
|
||||||
|
|
||||||
# Determine whether we're reading from a primary node. If false,
|
# Determine whether we're reading from a primary node. If false,
|
||||||
# this connection connects to a secondary node and @slave_ok is true.
|
# this connection connects to a secondary node and @slave_ok is true.
|
||||||
#
|
#
|
||||||
|
|
|
@ -200,6 +200,26 @@ class TestConnection < Test::Unit::TestCase
|
||||||
assert_equal Mongo::DEFAULT_MAX_BSON_SIZE, BSON::BSON_CODER.max_bson_size
|
assert_equal Mongo::DEFAULT_MAX_BSON_SIZE, BSON::BSON_CODER.max_bson_size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_connection_activity
|
||||||
|
conn = standard_connection
|
||||||
|
assert conn.active?
|
||||||
|
|
||||||
|
conn.primary_pool.close
|
||||||
|
assert !conn.active?
|
||||||
|
|
||||||
|
# Simulate a dropped connection.
|
||||||
|
dropped_socket = Mocha::Mock.new
|
||||||
|
dropped_socket.stubs(:read).raises(Errno::ECONNRESET)
|
||||||
|
dropped_socket.stubs(:send).raises(Errno::ECONNRESET)
|
||||||
|
dropped_socket.stub_everything
|
||||||
|
|
||||||
|
conn.primary_pool.host = 'localhost'
|
||||||
|
conn.primary_pool.port = Mongo::Connection::DEFAULT_PORT
|
||||||
|
conn.primary_pool.instance_variable_set("@sockets", [dropped_socket])
|
||||||
|
|
||||||
|
assert !conn.active?
|
||||||
|
end
|
||||||
|
|
||||||
context "Saved authentications" do
|
context "Saved authentications" do
|
||||||
setup do
|
setup do
|
||||||
@conn = standard_connection
|
@conn = standard_connection
|
||||||
|
|
Loading…
Reference in New Issue