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 74

attr_reader :address

#hello_ok?Boolean (readonly, private)

Since:

  • 2.0.0

[ GitHub ]

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

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 71

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 90

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 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_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 229

def check_document
  server_api = @app_metadata.server_api || options[: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 = 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.

Since:

  • 2.0.0

[ GitHub ]

  
# 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, 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 184

def disconnect!(options = nil)
  if socket
    socket.close rescue nil
    @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 97

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

Returns:

  • (BSON::Document)

    Handshake response from server

Raises:

Since:

  • 2.0.0

[ GitHub ]

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

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 => exc
  msg = "Failed to handshake with #{address}"
  Utils.warn_bg_exception(msg, exc,
    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.

Since:

  • 2.0.0

[ GitHub ]

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

Parameters:

  • reply (BSON::Document)

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

Since:

  • 2.0.0

[ GitHub ]

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

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 85

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

#write_bytes(bytes)

Since:

  • 2.0.0

[ GitHub ]

  
# 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