From c308f9b025423da15fa70d0db7ea23b9f5eb4a2b Mon Sep 17 00:00:00 2001 From: Kyle Banker Date: Tue, 13 Dec 2011 14:51:39 -0500 Subject: [PATCH] RUBY-386 set 30 second connect timeout. Fail if ismaster fails. --- lib/mongo/repl_set_connection.rb | 4 ++-- lib/mongo/util/node.rb | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/mongo/repl_set_connection.rb b/lib/mongo/repl_set_connection.rb index 4c3a67a..3a5f8f4 100644 --- a/lib/mongo/repl_set_connection.rb +++ b/lib/mongo/repl_set_connection.rb @@ -52,7 +52,7 @@ module Mongo # 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. # @option opts [Float] :op_timeout (30) The number of seconds to wait for a read operation to time out. - # @option opts [Float] :connect_timeout (nil) The number of seconds to wait before timing out a + # @option opts [Float] :connect_timeout (30) The number of seconds to wait before timing out a # connection attempt. # @option opts [Boolean] :ssl (false) If true, create the connection to the server using SSL. # @option opts [Boolean] :refresh_mode (false) Set this to :sync to periodically update the @@ -472,7 +472,7 @@ module Mongo @op_timeout = opts[:op_timeout] || 30 # Timeout on socket connect. - @connect_timeout = opts[:connect_timeout] || nil + @connect_timeout = opts[:connect_timeout] || 30 # Mutex for synchronizing pool access # TODO: remove this. diff --git a/lib/mongo/util/node.rb b/lib/mongo/util/node.rb index e29827d..f33e9f1 100644 --- a/lib/mongo/util/node.rb +++ b/lib/mongo/util/node.rb @@ -84,7 +84,13 @@ module Mongo # matches with the name provided. def set_config begin - @config = @connection['admin'].command({:ismaster => 1}, :socket => @socket) + if @connection.connect_timeout + Mongo::TimeoutHandler.timeout(@connection.connect_timeout, OperationTimeout) do + @config = @connection['admin'].command({:ismaster => 1}, :socket => @socket) + end + else + @config = @connection['admin'].command({:ismaster => 1}, :socket => @socket) + end if @config['msg'] && @logger @connection.log(:warn, "#{config['msg']}") @@ -95,6 +101,7 @@ module Mongo rescue ConnectionFailure, OperationFailure, OperationTimeout, SocketError, SystemCallError, IOError => ex @connection.log(:warn, "Attempted connection to node #{host_string} raised " + "#{ex.class}: #{ex.message}") + @socket.close unless @socket.closed? return nil end