lib/mongo/server_selector/selectable.rb in mongo-2.1.2 vs lib/mongo/server_selector/selectable.rb in mongo-2.2.0.rc0

- old
+ new

@@ -65,27 +65,46 @@ tag_sets = options[:tag_sets] || [] validate_tag_sets!(tag_sets) @tag_sets = tag_sets.freeze end + # Inspect the server selector. + # + # @example Inspect the server selector. + # selector.inspect + # + # @return [ String ] The inspection. + # + # @since 2.2.0 + def inspect + "#<#{self.class.name}:0x#{object_id} tag_sets=#{tag_sets.inspect} " + + "server_selection_timeout=#{server_selection_timeout} local_threshold=#{local_threshold}>" + end + # Select a server from eligible candidates. # # @example Select a server from the cluster. # selector.select_server(cluster) # # @param [ Mongo::Cluster ] cluster The cluster from which to select an eligible server. # # @return [ Mongo::Server ] A server matching the server preference. # # @since 2.0.0 - def select_server(cluster) + def select_server(cluster, ping = true) deadline = Time.now + server_selection_timeout while (deadline - Time.now) > 0 servers = candidates(cluster) if servers && !servers.compact.empty? server = servers.first - return server if server.connectable? + # There is no point pinging a standalone as the subsequent scan is + # not going to change anything about the cluster. + if ping && !cluster.single? + return server if server.connectable? + else + return server + end end cluster.scan! end raise Error::NoServerAvailable.new(self) end @@ -185,11 +204,9 @@ matches = candidates.select { |server| server.matches_tag_set?(tag_set) } !matches.empty? end matches || [] end - - private def validate_tag_sets!(tag_sets) if !tag_sets.all? { |set| set.empty? } && !tags_allowed? raise Error::InvalidServerPreference.new(name) end