Class: Mongo::Server::Monitor::Connection Private
- Inherits:
-
ConnectionCommon
- Object
- ConnectionCommon
- Mongo::Server::Monitor::Connection
- Includes:
- Loggable
- Defined in:
- lib/mongo/server/monitor/connection.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
This class models the monitor connections and their behavior.
Direct Known Subclasses
Constant Summary
Constants included from Loggable
Instance Attribute Summary collapse
-
#address ⇒ Mongo::Address
readonly
private
Address The address to connect to.
-
#options ⇒ Hash
readonly
private
Options The passed in options.
-
#server_connection_id ⇒ Integer
readonly
private
Server_connection_id The server connection id.
Attributes inherited from ConnectionCommon
Instance Method Summary collapse
-
#check_document ⇒ BSON::Document
private
Build a document that should be used for connection check.
-
#connect! ⇒ true
private
Establishes a network connection to the target address.
-
#disconnect!(_options = nil) ⇒ true
private
Disconnect the connection.
-
#dispatch(message) ⇒ Protocol::Message
private
Sends a message and returns the result.
-
#dispatch_bytes(bytes, **opts) ⇒ Protocol::Message
private
Sends a preserialized message and returns the result.
-
#handshake! ⇒ BSON::Document
private
Send handshake command to connected host and validate the response.
-
#initialize(address, options = {}) ⇒ Connection
constructor
private
Creates a new connection object to the specified target address with the specified options.
- #read_response(**opts) ⇒ Object private
-
#socket_timeout ⇒ Float
private
Returns the monitoring socket timeout.
- #write_bytes(bytes) ⇒ Object private
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Methods inherited from ConnectionCommon
#connected?, #handshake_command, #handshake_document
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.
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.
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mongo/server/monitor/connection.rb', line 56 def initialize(address, = {}) @address = address @options = .dup.freeze raise ArgumentError, 'App metadata is required' unless @app_metadata = [:app_metadata] @socket = nil @pid = Process.pid @compressor = nil @hello_ok = false end |
Instance Attribute Details
#address ⇒ Mongo::Address (readonly)
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.
Returns address The address to connect to.
71 72 73 |
# File 'lib/mongo/server/monitor/connection.rb', line 71 def address @address end |
#options ⇒ Hash (readonly)
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.
Returns options The passed in options.
68 69 70 |
# File 'lib/mongo/server/monitor/connection.rb', line 68 def @options end |
#server_connection_id ⇒ Integer (readonly)
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.
Returns server_connection_id The server connection id.
87 88 89 |
# File 'lib/mongo/server/monitor/connection.rb', line 87 def server_connection_id @server_connection_id end |
Instance Method Details
#check_document ⇒ BSON::Document
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.
Build a document that should be used for connection check.
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/mongo/server/monitor/connection.rb', line 224 def check_document server_api = @app_metadata.server_api || [:server_api] doc = if hello_ok? || server_api _doc = HELLO_DOC _doc = _doc.merge(Utils.transform_server_api(server_api)) if server_api _doc else LEGACY_HELLO_DOC end # compressors must be set to maintain correct compression status # in the server description. See RUBY-2427 if compressors = [:compressors] doc = doc.merge(compression: compressors) end doc end |
#connect! ⇒ true
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.
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.
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/mongo/server/monitor/connection.rb', line 151 def connect! raise ArgumentError, 'Monitoring connection already connected' if @socket @socket = add_server_diagnostics do address.socket(socket_timeout, .merge( connection_address: address, monitor: true )) end true end |
#disconnect!(_options = nil) ⇒ true
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.
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.
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/mongo/server/monitor/connection.rb', line 176 def disconnect!( = nil) if socket begin socket.close rescue StandardError nil end @socket = nil end true end |
#dispatch(message) ⇒ Protocol::Message
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.
Sends a message and returns the result.
94 95 96 |
# File 'lib/mongo/server/monitor/connection.rb', line 94 def dispatch() dispatch_bytes(.serialize.to_s) end |
#dispatch_bytes(bytes, **opts) ⇒ Protocol::Message
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.
Sends a preserialized message and returns the result.
106 107 108 109 110 111 |
# File 'lib/mongo/server/monitor/connection.rb', line 106 def dispatch_bytes(bytes, **opts) write_bytes(bytes) read_response( socket_timeout: opts[:read_socket_timeout] ) end |
#handshake! ⇒ BSON::Document
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.
Send handshake command to connected host and validate the response.
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/mongo/server/monitor/connection.rb', line 193 def handshake! command = handshake_command( handshake_document( @app_metadata, server_api: [:server_api] ) ) payload = command.serialize.to_s = dispatch_bytes(payload) result = Operation::Result.new() result.validate! reply = result.documents.first set_compressor!(reply) set_hello_ok!(reply) @server_connection_id = reply['connectionId'] reply rescue StandardError => e msg = "Failed to handshake with #{address}" Utils.warn_bg_exception(msg, e, logger: [:logger], log_prefix: [:log_prefix], bg_error_backtrace: [:bg_error_backtrace]) raise end |
#read_response(**opts) ⇒ Object
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.
125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/mongo/server/monitor/connection.rb', line 125 def read_response(**opts) raise ArgumentError, "Trying to read on an unconnected connection #{self}" unless connected? add_server_connection_id do add_server_diagnostics do Protocol::Message.deserialize(socket, Protocol::Message::MAX_MESSAGE_SIZE, nil, **opts) end end end |
#socket_timeout ⇒ Float
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.
Returns the monitoring socket timeout.
Note that monitoring connections use the connect timeout value as the socket timeout value. See the Server Discovery and Monitoring specification for details.
82 83 84 |
# File 'lib/mongo/server/monitor/connection.rb', line 82 def socket_timeout [:connect_timeout] || Server::CONNECT_TIMEOUT end |
#write_bytes(bytes) ⇒ Object
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.
113 114 115 116 117 118 119 120 121 |
# File 'lib/mongo/server/monitor/connection.rb', line 113 def write_bytes(bytes) raise ArgumentError, "Trying to dispatch on an unconnected connection #{self}" unless connected? add_server_connection_id do add_server_diagnostics do socket.write(bytes) end end end |