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 74
attr_reader :address
#hello_ok? ⇒ Boolean
(readonly, private)
# File 'lib/mongo/server/monitor/connection.rb', line 269
def hello_ok? @hello_ok end
#options ⇒ Hash
(readonly)
# File 'lib/mongo/server/monitor/connection.rb', line 71
attr_reader :
#server_connection_id ⇒ Integer
(readonly)
# File 'lib/mongo/server/monitor/connection.rb', line 90
attr_reader :server_connection_id
Instance Method Details
#add_server_connection_id (private)
# File 'lib/mongo/server/monitor/connection.rb', line 250
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 229
def check_document server_api = @app_metadata.server_api || [:server_api] doc = if hello_ok? || server_api _doc = HELLO_DOC if server_api _doc = _doc.merge(Utils.transform_server_api(server_api)) end _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 158
def connect! if @socket raise ArgumentError, 'Monitoring connection already connected' end @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 184
def disconnect!( = nil) if socket socket.close rescue nil @socket = nil end true end
#dispatch(message) ⇒ Protocol::Message
Sends a message and returns the result.
# File 'lib/mongo/server/monitor/connection.rb', line 97
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 109
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 197
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 => exc msg = "Failed to handshake with #{address}" Utils.warn_bg_exception(msg, exc, 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 130
def read_response(**opts) unless connected? raise ArgumentError, "Trying to read on an unconnected connection #{self}" end 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 265
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 85
def socket_timeout [:connect_timeout] || Server::CONNECT_TIMEOUT end
#write_bytes(bytes)
# File 'lib/mongo/server/monitor/connection.rb', line 116
def write_bytes(bytes) unless connected? raise ArgumentError, "Trying to dispatch on an unconnected connection #{self}" end add_server_connection_id do add_server_diagnostics do socket.write(bytes) end end end