Class: Mongo::Server::Monitor::Connection
- Inherits:
-
ConnectionCommon
- Object
- ConnectionCommon
- Mongo::Server::Monitor::Connection
- Includes:
- Loggable, Retryable, Connectable
- Defined in:
- lib/mongo/server/monitor/connection.rb
Overview
This class models the monitor connections and their behavior.
Constant Summary collapse
- ISMASTER =
The command used for determining server status.
{ :ismaster => 1 }.freeze
- ISMASTER_OP_MSG =
The command used for determining server status formatted for an OP_MSG (server versions >= 3.6).
{ :ismaster => 1, '$db' => Database::ADMIN }.freeze
- ISMASTER_MESSAGE =
The constant for the ismaster command.
Protocol::Query.new(Database::ADMIN, Database::COMMAND, ISMASTER, :limit => -1)
- ISMASTER_OP_MSG_MESSAGE =
The constant for the ismaster command as an OP_MSG (server versions >= 3.6).
Protocol::Msg.new([], {}, ISMASTER_OP_MSG)
- ISMASTER_BYTES =
The raw bytes for the ismaster message.
ISMASTER_MESSAGE.serialize.to_s.freeze
- ISMASTER_OP_MSG_BYTES =
The raw bytes for the ismaster OP_MSG message (server versions >= 3.6).
ISMASTER_OP_MSG_MESSAGE.serialize.to_s.freeze
- CONNECT_TIMEOUT =
Deprecated.
Please use Server::CONNECT_TIMEOUT instead. Will be removed in 3.0.0
The default time in seconds to timeout a connection attempt.
10.freeze
- COMPRESSION =
Deprecated.
Key for compression algorithms in the response from the server during handshake.
'compression'.freeze
- COMPRESSION_WARNING =
Deprecated.
Warning message that the server has no compression algorithms in common with those requested
by the client. 'The server has no compression algorithms in common with those requested. ' + 'Compression will not be used.'.freeze
Constants included from Loggable
Constants included from Connectable
Connectable::SSL, Connectable::TIMEOUT
Instance Attribute Summary collapse
-
#address ⇒ Mongo::Address
readonly
Address The address to connect to.
-
#options ⇒ Hash
readonly
Options The passed in options.
Attributes included from Connectable
Attributes inherited from ConnectionCommon
Instance Method Summary collapse
-
#connect! ⇒ true
Establishes a network connection to the target address.
-
#disconnect!(options = nil) ⇒ true
Disconnect the connection.
-
#initialize(address, options = {}) ⇒ Connection
constructor
private
Creates a new connection object to the specified target address with the specified options.
-
#ismaster ⇒ BSON::Document
Send the preserialized ismaster call.
-
#socket_timeout ⇒ Float
(also: #timeout)
Get the socket timeout.
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Methods included from Connectable
Methods included from Retryable
#legacy_write_with_retry, #nro_write_with_retry, #read_with_one_retry, #read_with_retry, #read_with_retry_cursor, #write_with_retry
Constructor Details
#initialize(address, options = {}) ⇒ Connection
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Monitoring connections do not authenticate.
Connection must never be directly instantiated outside of a Monitor.
Creates a new connection object to the specified target address with the specified options.
The constructor does not perform any I/O (and thus does not create sockets nor handshakes); call connect! method on the connection object to create the network connection.
115 116 117 118 119 120 121 122 |
# File 'lib/mongo/server/monitor/connection.rb', line 115 def initialize(address, = {}) @address = address @options = .freeze @app_metadata = [:app_metadata] @socket = nil @pid = Process.pid @compressor = nil end |
Instance Attribute Details
#address ⇒ Mongo::Address (readonly)
Returns address The address to connect to.
128 129 130 |
# File 'lib/mongo/server/monitor/connection.rb', line 128 def address @address end |
#options ⇒ Hash (readonly)
Returns options The passed in options.
125 126 127 |
# File 'lib/mongo/server/monitor/connection.rb', line 125 def @options end |
Instance Method Details
#connect! ⇒ true
This method mutates the connection class by setting a socket if one previously did not exist.
Establishes a network connection to the target address.
If the connection is already established, this method does nothing.
160 161 162 163 164 165 166 167 |
# File 'lib/mongo/server/monitor/connection.rb', line 160 def connect! unless @socket socket = address.socket(socket_timeout, , address.) handshake!(socket) @socket = socket end true end |
#disconnect!(options = nil) ⇒ true
This method mutates the connection by setting the socket to nil if the closing succeeded.
This method accepts an options argument for compatibility with Server::Connections. However, all options are ignored.
Disconnect the connection.
183 184 185 186 187 188 189 |
# File 'lib/mongo/server/monitor/connection.rb', line 183 def disconnect!( = nil) if socket socket.close @socket = nil end true end |
#ismaster ⇒ BSON::Document
Send the preserialized ismaster call.
138 139 140 141 142 143 144 145 |
# File 'lib/mongo/server/monitor/connection.rb', line 138 def ismaster ensure_connected do |socket| read_with_one_retry(retry_message: ) do socket.write(ISMASTER_BYTES) Protocol::Message.deserialize(socket).documents[0] end end end |
#socket_timeout ⇒ Float Also known as: timeout
Get the socket timeout.
201 202 203 |
# File 'lib/mongo/server/monitor/connection.rb', line 201 def socket_timeout @timeout ||= [:connect_timeout] || Server::CONNECT_TIMEOUT end |