class Mongo::Server::Monitor::Connection
This class models the monitor connections and their behavior.
@since 2.0.0
Constants
- CONNECT_TIMEOUT
The default time in seconds to timeout a connection attempt.
@since 2.1.2
@deprecated Please use Server::CONNECT_TIMEOUT instead. Will be removed in 3.0.0
- ISMASTER
The command used for determining server status.
@since 2.2.0
- ISMASTER_BYTES
The raw bytes for the ismaster message.
@since 2.2.0
- ISMASTER_MESSAGE
The constant for the ismaster command.
@since 2.2.0
Public Class Methods
Initialize a new socket connection from the client to the server.
@api private
@example Create the connection.
Connection.new(address)
@note Connection must never be directly instantiated outside of a
Monitor.
@param [ Mongo::Address ] address The address the connection is for. @param [ Hash ] options The connection options.
@since 2.0.0
# File lib/mongo/server/monitor/connection.rb, line 118 def initialize(address, options = {}) @address = address @options = options.freeze @app_metadata = options[:app_metadata] @ssl_options = options.reject { |k, v| !k.to_s.start_with?(SSL) } @socket = nil @pid = Process.pid end
Public Instance Methods
Tell the underlying socket to establish a connection to the host.
@example Connect to the host.
connection.connect!
@note This method mutates the connection class by setting a socket if
one previously did not exist.
@return [ true ] If the connection succeeded.
@since 2.0.0
# File lib/mongo/server/monitor/connection.rb, line 76 def connect! unless socket && socket.connectable? @socket = address.socket(socket_timeout, ssl_options) address.connect_socket!(socket) handshake! end true end
Disconnect the connection.
@example Disconnect from the host.
connection.disconnect!
@note This method mutates the connection by setting the socket to nil
if the closing succeeded.
@return [ true ] If the disconnect succeeded.
@since 2.0.0
# File lib/mongo/server/monitor/connection.rb, line 96 def disconnect! if socket socket.close @socket = nil end true end
Send the preserialized ismaster call.
@example Send a preserialized ismaster message.
connection.ismaster
@return [ BSON::Document ] The ismaster result.
@since 2.2.0
# File lib/mongo/server/monitor/connection.rb, line 56 def ismaster ensure_connected do |socket| read_with_one_retry do socket.write(ISMASTER_BYTES) Protocol::Reply.deserialize(socket).documents[0] end end end
Get the socket timeout.
@example Get the socket timeout.
connection.socket_timeout
@return [ Float ] The socket timeout in seconds. Note that the Monitor's connection
uses the connect timeout value for calling ismaster. See the Server Discovery and Monitoring specification for details.
@since 2.4.3
# File lib/mongo/server/monitor/connection.rb, line 137 def socket_timeout @timeout ||= options[:connect_timeout] || Server::CONNECT_TIMEOUT end
@deprecated Please use :socket_timeout instead. Will be removed in 3.0.0
Private Instance Methods
# File lib/mongo/server/monitor/connection.rb, line 145 def handshake! if @app_metadata socket.write(@app_metadata.ismaster_bytes) Protocol::Reply.deserialize(socket, Mongo::Protocol::Message::MAX_MESSAGE_SIZE).documents[0] end end