Skip to content

Commit f12e907

Browse files
Brandon BlackTylerBrock
Brandon Black
authored andcommitted
cleanup: refactor, making check_is_master a public instance method
1 parent 8d575cf commit f12e907

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

lib/mongo/mongo_client.rb

+28-27
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def self.multi(nodes, opts={})
160160

161161
# Initialize a connection to MongoDB using the MongoDB URI spec.
162162
#
163-
# Since MongoClient.new cannot be used with any <code>ENV["MONGODB_URI"]</code> that has multiple hosts (implying a replicaset),
163+
# Since MongoClient.new cannot be used with any <code>ENV["MONGODB_URI"]</code> that has multiple hosts (implying a replicaset),
164164
# you may use this when the type of your connection varies by environment and should be determined solely from <code>ENV["MONGODB_URI"]</code>.
165165
#
166166
# @param uri [String]
@@ -469,7 +469,7 @@ def connect
469469
# NOTE: Do check if this needs to be more stringent.
470470
# Probably not since if any node raises a connection failure, all nodes will be closed.
471471
def connected?
472-
@primary_pool && !@primary_pool.closed?
472+
!!(@primary_pool && !@primary_pool.closed?)
473473
end
474474

475475
# Determine if the connection is active. In a normal case the *server_info* operation
@@ -545,6 +545,32 @@ def checkin(socket)
545545
end
546546
end
547547

548+
# Internal method for checking isMaster() on a given node.
549+
#
550+
# @param node [Array] Port and host for the target node
551+
# @return [Hash] Response from isMaster()
552+
#
553+
# @private
554+
def check_is_master(node)
555+
begin
556+
host, port = *node
557+
config = nil
558+
socket = @socket_class.new(host, port, @op_timeout, @connect_timeout)
559+
if @connect_timeout
560+
Timeout::timeout(@connect_timeout, OperationTimeout) do
561+
config = self['admin'].command({:ismaster => 1}, :socket => socket)
562+
end
563+
else
564+
config = self['admin'].command({:ismaster => 1}, :socket => socket)
565+
end
566+
rescue OperationFailure, SocketError, SystemCallError, IOError
567+
close
568+
ensure
569+
socket.close unless socket.nil? || socket.closed?
570+
end
571+
config
572+
end
573+
548574
protected
549575

550576
def valid_opts
@@ -621,31 +647,6 @@ def setup(opts)
621647

622648
private
623649

624-
def check_is_master(node)
625-
begin
626-
host, port = *node
627-
socket = nil
628-
config = nil
629-
630-
socket = @socket_class.new(host, port, @op_timeout, @connect_timeout)
631-
if(@connect_timeout)
632-
Timeout::timeout(@connect_timeout, OperationTimeout) do
633-
config = self['admin'].command({:ismaster => 1}, :socket => socket)
634-
end
635-
else
636-
config = self['admin'].command({:ismaster => 1}, :socket => socket)
637-
end
638-
rescue OperationFailure, SocketError, SystemCallError, IOError
639-
close
640-
ensure
641-
if socket
642-
socket.close unless socket.closed?
643-
end
644-
end
645-
646-
config
647-
end
648-
649650
# Set the specified node as primary.
650651
def set_primary(node)
651652
host, port = *node

lib/mongo/mongo_sharded_client.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def hard_refresh!
104104
end
105105

106106
def connected?
107-
@connected && @manager.primary_pool
107+
!!(@connected && @manager.primary_pool)
108108
end
109109

110110
# Returns +true+ if it's okay to read from a secondary node.

0 commit comments

Comments
 (0)