123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Server::Monitor::Connection Private

Do not use. This class is for internal use only.
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.

Since:

  • 2.0.0

Constant Summary

::Mongo::Server::ConnectionCommon - Inherited

HELLO_DOC, LEGACY_HELLO_DOC

::Mongo::Loggable - Included

PREFIX

Class Method Summary

Instance Attribute Summary

::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

::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

#addressMongo::Address (readonly)

Returns:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/monitor/connection.rb', line 71

attr_reader :address

#hello_ok?Boolean (readonly, private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/monitor/connection.rb', line 262

def hello_ok?
  @hello_ok
end

#optionsHash (readonly)

Returns:

  • (Hash)

    options The passed in options.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/monitor/connection.rb', line 68

attr_reader :options

#server_connection_idInteger (readonly)

Returns:

  • (Integer)

    server_connection_id The server connection id.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/monitor/connection.rb', line 87

attr_reader :server_connection_id

Instance Method Details

#add_server_connection_id (private)

Since:

  • 2.0.0

[ GitHub ]

  
# 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_documentBSON::Document

Build a document that should be used for connection check.

Returns:

  • (BSON::Document)

    Document that should be sent to a server for connection check.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/monitor/connection.rb', line 224

def check_document
  server_api = @app_metadata.server_api || options[: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 = options[:compressors]
    doc = doc.merge(compression: compressors)
  end
  doc
end

#connect!true

Note:

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.

Examples:

Connect to the host.

connection.connect!

Returns:

  • (true)

    If the connection succeeded.

Raises:

  • (ArgumentError)

Since:

  • 2.0.0

[ GitHub ]

  
# 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, ssl_options.merge(
                                     connection_address: address, monitor: true
                                   ))
  end
  true
end

#disconnect!(_options = nil) ⇒ true

Note:

This method mutates the connection by setting the socket to nil if the closing succeeded.

Note:

This method accepts an options argument for compatibility with Server::Connections. However, all options are ignored.

Disconnect the connection.

Examples:

Disconnect from the host.

connection.disconnect!

Returns:

  • (true)

    If the disconnect succeeded.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/monitor/connection.rb', line 176

def disconnect!(_options = 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.

Parameters:

Returns:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/monitor/connection.rb', line 94

def dispatch(message)
  dispatch_bytes(message.serialize.to_s)
end

#dispatch_bytes(bytes, **opts) ⇒ Protocol::Message

Sends a preserialized message and returns the result.

Parameters:

  • bytes (String)

    The serialized message to send.

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :read_socket_timeout (Numeric)

    The timeout to use for each read operation.

Returns:

Since:

  • 2.0.0

[ GitHub ]

  
# 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.

Returns:

  • (BSON::Document)

    Handshake response from server

Raises:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/monitor/connection.rb', line 193

def handshake!
  command = handshake_command(
    handshake_document(
      @app_metadata,
      server_api: options[:server_api]
    )
  )
  payload = command.serialize.to_s
  message = dispatch_bytes(payload)
  result = Operation::Result.new(message)
  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: options[:logger],
                          log_prefix: options[:log_prefix],
                          bg_error_backtrace: options[:bg_error_backtrace])
  raise
end

#read_response(**opts)

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :socket_timeout (Numeric)

    The timeout to use for each read operation.

Raises:

  • (ArgumentError)

Since:

  • 2.0.0

[ GitHub ]

  
# 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.

Parameters:

  • reply (BSON::Document)

    ::Mongo::Server reply to legacy hello command.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/monitor/connection.rb', line 258

def set_hello_ok!(reply)
  @hello_ok = !!reply[:helloOk]
end

#socket_timeoutFloat

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.

Returns:

  • (Float)

    The socket timeout in seconds.

Since:

  • 2.4.3

[ GitHub ]

  
# File 'lib/mongo/server/monitor/connection.rb', line 82

def socket_timeout
  options[:connect_timeout] || Server::CONNECT_TIMEOUT
end

#write_bytes(bytes)

Raises:

  • (ArgumentError)

Since:

  • 2.0.0

[ GitHub ]

  
# 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