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

Inherits:
Object
  • Object
show all
Includes:
Loggable, Retryable, Connectable
Defined in:
lib/mongo/server/monitor/connection.rb

Overview

This class models the monitor connections and their behavior.

Since:

  • 2.0.0

Constant Summary collapse

ISMASTER =

The command used for determining server status.

Since:

  • 2.2.0

{ :ismaster => 1 }.freeze
ISMASTER_OP_MSG =

The command used for determining server status formatted for an OP_MSG (server versions >= 3.6).

Since:

  • 2.5.0

{ :ismaster => 1, '$db' => Database::ADMIN }.freeze
ISMASTER_MESSAGE =

The constant for the ismaster command.

Since:

  • 2.2.0

Protocol::Query.new(Database::ADMIN, Database::COMMAND, ISMASTER, :limit => -1)
ISMASTER_OP_MSG_MESSAGE =

The constant for the ismaster command as an OP_MSG (server versions >= 3.6).

Since:

  • 2.5.0

Protocol::Msg.new([], {}, ISMASTER_OP_MSG)
ISMASTER_BYTES =

The raw bytes for the ismaster message.

Since:

  • 2.2.0

ISMASTER_MESSAGE.serialize.to_s.freeze
ISMASTER_OP_MSG_BYTES =

The raw bytes for the ismaster OP_MSG message (server versions >= 3.6).

Since:

  • 2.5.0

ISMASTER_OP_MSG_MESSAGE.serialize.to_s.freeze
CONNECT_TIMEOUT =
Deprecated.

Please use Server::CONNECT_TIMEOUT instead. Will be removed in 3.0.0

The default time in seconds to timeout a connection attempt.

Since:

  • 2.1.2

10.freeze
COMPRESSION =

Key for compression algorithms in the response from the server during handshake.

Since:

  • 2.5.0

'compression'.freeze
COMPRESSION_WARNING =

Warning message that the server has no compression algorithms in common with those requested

by the client.

Since:

  • 2.5.0

'The server has no compression algorithms in common with those requested. ' +
'Compression will not be used.'.freeze

Constants included from Loggable

Loggable::PREFIX

Constants included from Connectable

Connectable::SSL, Connectable::TIMEOUT

Instance Attribute Summary collapse

Attributes included from Connectable

#pid

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger

Methods included from Connectable

#connectable?, #connected?

Methods included from Retryable

#read_with_one_retry, #read_with_retry, #write_with_retry

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.

Note:

Monitoring connections do not authenticate.

Note:

Connection must never be directly instantiated outside of a Monitor.

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.

Examples:

Create the connection.

Connection.new(address)

Parameters:

  • address (Mongo::Address)

    The address the connection is for.

  • options (Hash) (defaults to: {})

    The connection options.

Options Hash (options):

  • :app_metadata (Mongo::Server::Monitor::AppMetadata)

    Metadata to use for handshake. If missing or nil, handshake will not be performed. Although a Mongo::Server::AppMetadata instance will also work, monitoring connections are meant to use Mongo::Server::Monitor::AppMetadata instances in order to omit performing SCRAM negotiation with the server, as monitoring sockets do not authenticate.

  • :compressors (Array<String>)

    A list of potential compressors to use, in order of preference. The driver chooses the first compressor that is also supported by the server. Currently the driver only supports 'zlib'.

  • :connect_timeout (Float)

    The timeout, in seconds, to use for network operations. This timeout is used for all socket operations rather than connect calls only, contrary to what the name implies,

Since:

  • 2.0.0



113
114
115
116
117
118
119
120
121
# File 'lib/mongo/server/monitor/connection.rb', line 113

def initialize(address, options = {})
  @address = address
  @options = options.freeze
  @app_metadata = options[:app_metadata]
  @ssl_options = options.reject { |k, v| !k.to_s.start_with?(SSL) }
  @socket = nil
  @pid = Process.pid
  @compressor = nil
end

Instance Attribute Details

#addressMongo::Address (readonly)

Returns address The address to connect to.

Returns:

Since:

  • 2.0.0



127
128
129
# File 'lib/mongo/server/monitor/connection.rb', line 127

def address
  @address
end

#compressorObject (readonly)

The compressor, which is determined during the handshake.

Since:

  • 2.5.0



132
133
134
# File 'lib/mongo/server/monitor/connection.rb', line 132

def compressor
  @compressor
end

#optionsHash (readonly)

Returns options The passed in options.

Returns:

  • (Hash)

    options The passed in options.

Since:

  • 2.0.0



124
125
126
# File 'lib/mongo/server/monitor/connection.rb', line 124

def options
  @options
end

Instance Method Details

#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



164
165
166
167
168
169
170
171
172
# File 'lib/mongo/server/monitor/connection.rb', line 164

def connect!
  unless @socket
    socket = address.socket(socket_timeout, ssl_options,
      connect_timeout: address.connect_timeout)
    handshake!(socket)
    @socket = socket
  end
  true
end

#disconnect!true

Note:

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

Disconnect the connection.

Examples:

Disconnect from the host.

connection.disconnect!

Returns:

  • (true)

    If the disconnect succeeded.

Since:

  • 2.0.0



185
186
187
188
189
190
191
# File 'lib/mongo/server/monitor/connection.rb', line 185

def disconnect!
  if socket
    socket.close
    @socket = nil
  end
  true
end

#ismasterBSON::Document

Send the preserialized ismaster call.

Examples:

Send a preserialized ismaster message.

connection.ismaster

Returns:

  • (BSON::Document)

    The ismaster result.

Since:

  • 2.2.0



142
143
144
145
146
147
148
149
# File 'lib/mongo/server/monitor/connection.rb', line 142

def ismaster
  ensure_connected do |socket|
    read_with_one_retry(retry_message: retry_message) do
      socket.write(ISMASTER_BYTES)
      Protocol::Message.deserialize(socket).documents[0]
    end
  end
end

#socket_timeoutFloat Also known as: timeout

Get the socket timeout.

Examples:

Get the socket timeout.

connection.socket_timeout

Returns:

  • (Float)

    The socket timeout in seconds. Note that the Monitor's connection uses the connect timeout value for calling ismaster. See the Server Discovery and Monitoring specification for details.

Since:

  • 2.4.3



203
204
205
# File 'lib/mongo/server/monitor/connection.rb', line 203

def socket_timeout
  @timeout ||= options[:connect_timeout] || Server::CONNECT_TIMEOUT
end