RUBY-269 RUBY-275 added connect_timeout option
This commit is contained in:
parent
fe423d05d6
commit
b0d5df72fa
|
@ -70,6 +70,8 @@ module Mongo
|
||||||
# Note: this setting is relevant only for multi-threaded applications (which in Ruby are rare).
|
# Note: this setting is relevant only for multi-threaded applications (which in Ruby are rare).
|
||||||
# @option opts [Float] :op_timeout (nil) The number of seconds to wait for a read operation to time out.
|
# @option opts [Float] :op_timeout (nil) The number of seconds to wait for a read operation to time out.
|
||||||
# Disabled by default.
|
# Disabled by default.
|
||||||
|
# @option opts [Float] :connect_timeout (nil) The number of seconds to wait before timing out a
|
||||||
|
# connection attempt.
|
||||||
#
|
#
|
||||||
# @example localhost, 27017
|
# @example localhost, 27017
|
||||||
# Connection.new
|
# Connection.new
|
||||||
|
@ -628,6 +630,10 @@ module Mongo
|
||||||
# Timeout on socket read operation.
|
# Timeout on socket read operation.
|
||||||
@op_timeout = opts[:op_timeout] || nil
|
@op_timeout = opts[:op_timeout] || nil
|
||||||
|
|
||||||
|
# Timeout on socket connect.
|
||||||
|
@connect_timeout = opts[:connect_timeout] || nil
|
||||||
|
|
||||||
|
|
||||||
# Mutex for synchronizing pool access
|
# Mutex for synchronizing pool access
|
||||||
@connection_mutex = Mutex.new
|
@connection_mutex = Mutex.new
|
||||||
|
|
||||||
|
@ -698,8 +704,16 @@ module Mongo
|
||||||
def check_is_master(node)
|
def check_is_master(node)
|
||||||
begin
|
begin
|
||||||
host, port = *node
|
host, port = *node
|
||||||
socket = TCPSocket.new(host, port)
|
|
||||||
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
if @connect_timeout
|
||||||
|
Mongo::TimeoutHandler.timeout(@connect_timeout, OperationTimeout) do
|
||||||
|
socket = TCPSocket.new(host, port)
|
||||||
|
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
socket = TCPSocket.new(host, port)
|
||||||
|
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
||||||
|
end
|
||||||
|
|
||||||
config = self['admin'].command({:ismaster => 1}, :socket => socket)
|
config = self['admin'].command({:ismaster => 1}, :socket => socket)
|
||||||
rescue OperationFailure, SocketError, SystemCallError, IOError => ex
|
rescue OperationFailure, SocketError, SystemCallError, IOError => ex
|
||||||
|
|
|
@ -46,6 +46,10 @@ module Mongo
|
||||||
# @option options [Float] :timeout (5.0) When all of the connections a pool are checked out,
|
# @option options [Float] :timeout (5.0) When all of the connections a pool are checked out,
|
||||||
# this is the number of seconds to wait for a new connection to be released before throwing an exception.
|
# this is the number of seconds to wait for a new connection to be released before throwing an exception.
|
||||||
# Note: this setting is relevant only for multi-threaded applications.
|
# Note: this setting is relevant only for multi-threaded applications.
|
||||||
|
# @option opts [Float] :op_timeout (nil) The number of seconds to wait for a read operation to time out.
|
||||||
|
# Disabled by default.
|
||||||
|
# @option opts [Float] :connect_timeout (nil) The number of seconds to wait before timing out a
|
||||||
|
# connection attempt.
|
||||||
#
|
#
|
||||||
# @example Connect to a replica set and provide two seed nodes. Note that the number of seed nodes does
|
# @example Connect to a replica set and provide two seed nodes. Note that the number of seed nodes does
|
||||||
# not have to be equal to the number of replica set members. The purpose of seed nodes is to permit
|
# not have to be equal to the number of replica set members. The purpose of seed nodes is to permit
|
||||||
|
@ -207,8 +211,16 @@ module Mongo
|
||||||
def check_is_master(node)
|
def check_is_master(node)
|
||||||
begin
|
begin
|
||||||
host, port = *node
|
host, port = *node
|
||||||
socket = TCPSocket.new(host, port)
|
|
||||||
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
if @connect_timeout
|
||||||
|
Mongo::TimeoutHandler.timeout(@connect_timeout, OperationTimeout) do
|
||||||
|
socket = TCPSocket.new(host, port)
|
||||||
|
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
socket = TCPSocket.new(host, port)
|
||||||
|
socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
|
||||||
|
end
|
||||||
|
|
||||||
config = self['admin'].command({:ismaster => 1}, :socket => socket)
|
config = self['admin'].command({:ismaster => 1}, :socket => socket)
|
||||||
|
|
||||||
|
|
|
@ -22,6 +22,21 @@ class TestConnection < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_connection_timeout
|
||||||
|
passed = false
|
||||||
|
begin
|
||||||
|
t0 = Time.now
|
||||||
|
Mongo::Connection.new('192.169.169.1', 27017, :connect_timeout => 3)
|
||||||
|
rescue OperationTimeout
|
||||||
|
passed = true
|
||||||
|
t1 = Time.now
|
||||||
|
end
|
||||||
|
|
||||||
|
assert passed
|
||||||
|
assert t1 - t0 < 4
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def test_host_port_accessors
|
def test_host_port_accessors
|
||||||
assert_equal @conn.host, TEST_HOST
|
assert_equal @conn.host, TEST_HOST
|
||||||
assert_equal @conn.port, TEST_PORT
|
assert_equal @conn.port, TEST_PORT
|
||||||
|
|
|
@ -27,6 +27,21 @@ class ConnectTest < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_connect_timeout
|
||||||
|
passed = false
|
||||||
|
timeout = 3
|
||||||
|
begin
|
||||||
|
t0 = Time.now
|
||||||
|
ReplSetConnection.new(['192.169.169.1', 27017], :connect_timeout => timeout)
|
||||||
|
rescue OperationTimeout
|
||||||
|
passed = true
|
||||||
|
t1 = Time.now
|
||||||
|
end
|
||||||
|
|
||||||
|
assert passed
|
||||||
|
assert t1 - t0 < timeout + 1
|
||||||
|
end
|
||||||
|
|
||||||
def test_connect
|
def test_connect
|
||||||
@conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
|
@conn = ReplSetConnection.new([RS.host, RS.ports[1]], [RS.host, RS.ports[0]],
|
||||||
[RS.host, RS.ports[2]], :name => RS.name)
|
[RS.host, RS.ports[2]], :name => RS.name)
|
||||||
|
@ -66,7 +81,6 @@ class ConnectTest < Test::Unit::TestCase
|
||||||
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
|
@conn = ReplSetConnection.new([RS.host, RS.ports[0]], [RS.host, RS.ports[1]],
|
||||||
[RS.host, RS.ports[2]])
|
[RS.host, RS.ports[2]])
|
||||||
end
|
end
|
||||||
assert @conn.connected?
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_connect_with_secondary_node_killed
|
def test_connect_with_secondary_node_killed
|
||||||
|
|
Loading…
Reference in New Issue