Class: Mongo::Server::Monitor::Connection Private
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
Mongo::Server::PushMonitor::Connection
|
|
| Super Chains via Extension / Inclusion / Inheritance | |
|
Class Chain:
|
|
|
Instance Chain:
|
|
| Inherits: |
Mongo::Server::ConnectionCommon
|
| Defined in: | lib/mongo/server/monitor/connection.rb |
Overview
This class models the monitor connections and their behavior.
Constant Summary
::Mongo::Server::ConnectionCommon - Inherited
::Mongo::Loggable - Included
Class Method Summary
-
.new(address, options = {}) ⇒ Connection
constructor
Internal use only
Creates a new connection object to the specified target address with the specified options.
Instance Attribute Summary
- #address ⇒ Mongo::Address readonly Internal use only
- #options ⇒ Hash readonly Internal use only
- #server_connection_id ⇒ Integer readonly Internal use only
- #hello_ok? ⇒ Boolean readonly private Internal use only
::Mongo::Server::ConnectionCommon - Inherited
| #compressor | The compressor negotiated during the handshake for this connection, if any. |
| #connected? | Determine if the connection is currently connected. |
| #pid, #socket | |
Instance Method Summary
-
#check_document ⇒ BSON::Document
Internal use only
Build a document that should be used for connection check.
-
#connect! ⇒ true
Internal use only
Establishes a network connection to the target address.
-
#disconnect!(_options = nil) ⇒ true
Internal use only
Disconnect the connection.
-
#dispatch(message) ⇒ Protocol::Message
Internal use only
Sends a message and returns the result.
-
#dispatch_bytes(bytes, **opts) ⇒ Protocol::Message
Internal use only
Sends a preserialized message and returns the result.
-
#handshake! ⇒ BSON::Document
Internal use only
Send handshake command to connected host and validate the response.
- #read_response(**opts) Internal use only
-
#socket_timeout ⇒ Float
Internal use only
Returns the monitoring socket timeout.
- #write_bytes(bytes) Internal use only
- #add_server_connection_id private Internal use only
-
#set_hello_ok!(reply)
private
Internal use only
Update @hello_ok flag according to server reply to legacy hello command.
::Mongo::Loggable - Included
| #log_debug | Convenience method to log debug messages with the standard prefix. |
| #log_error | Convenience method to log error messages with the standard prefix. |
| #log_fatal | Convenience method to log fatal messages with the standard prefix. |
| #log_info | Convenience method to log info messages with the standard prefix. |
| #log_warn | Convenience method to log warn messages with the standard prefix. |
| #logger | Get the logger instance. |
| #_mongo_log_prefix, #format_message | |
::Mongo::Server::ConnectionCommon - Inherited
| #handshake_command | Build a command that should be used for connection handshake. |
| #handshake_document | Build a document that should be used for connection handshake. |
| #add_server_diagnostics | Yields to the block and, if the block raises an exception, adds a note to the exception with the address of the specified server. |
| #ensure_connected, #set_compressor!, #ssl_options | |
Instance Attribute Details
#address ⇒ Mongo::Address (readonly)
# File 'lib/mongo/server/monitor/connection.rb', line 71
attr_reader :address
#hello_ok? ⇒ Boolean (readonly, private)
# File 'lib/mongo/server/monitor/connection.rb', line 262
def hello_ok? @hello_ok end
#options ⇒ Hash (readonly)
# File 'lib/mongo/server/monitor/connection.rb', line 68
attr_reader :
#server_connection_id ⇒ Integer (readonly)
# File 'lib/mongo/server/monitor/connection.rb', line 87
attr_reader :server_connection_id
Instance Method Details
#add_server_connection_id (private)
# File 'lib/mongo/server/monitor/connection.rb', line 243
def add_server_connection_id yield rescue Mongo::Error => e if server_connection_id note = "sconn:#{server_connection_id}" e.add_note(note) end raise e end
#check_document ⇒ BSON::Document
Build a document that should be used for connection check.
# 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 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.
# 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 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.
# 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
Sends a message and returns the result.
# File 'lib/mongo/server/monitor/connection.rb', line 94
def dispatch() dispatch_bytes(.serialize.to_s) end
#dispatch_bytes(bytes, **opts) ⇒ Protocol::Message
Sends a preserialized message and returns the result.
# 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
Send handshake command to connected host and validate the response.
# 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)
# 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
#set_hello_ok!(reply) (private)
Update @hello_ok flag according to server reply to legacy hello command. The flag will be set to true if connected server supports hello command, otherwise the flag will be set to false.
# File 'lib/mongo/server/monitor/connection.rb', line 258
def set_hello_ok!(reply) @hello_ok = !!reply[:helloOk] end
#socket_timeout ⇒ Float
Returns the monitoring socket timeout.
Note that monitoring connections use the connect timeout value as the socket timeout value. See the ::Mongo::Server Discovery and ::Mongo::Monitoring specification for details.
# File 'lib/mongo/server/monitor/connection.rb', line 82
def socket_timeout [:connect_timeout] || Server::CONNECT_TIMEOUT end
#write_bytes(bytes)
# 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