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

Inherits:
Object
  • Object
show all
Includes:
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_MESSAGE =

The constant for the ismaster command.

Since:

  • 2.2.0

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

The raw bytes for the ismaster message.

Since:

  • 2.2.0

ISMASTER_MESSAGE.serialize.to_s.freeze
CONNECT_TIMEOUT =

The default time in seconds to timeout a connection attempt.

Since:

  • 2.1.2

10.freeze

Constants included from Connectable

Connectable::SSL, Connectable::TIMEOUT

Instance Attribute Summary

Attributes included from Connectable

#address, #options, #pid

Instance Method Summary collapse

Methods included from Connectable

#connectable?, #connected?

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:

Connection must never be directly instantiated outside of a Monitor.

Initialize a new socket connection from the client to the server.

Examples:

Create the connection.

Connection.new(address)

Parameters:

  • address (Mongo::Address)

    The address the connection is for.

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

    The connection options.

Since:

  • 2.0.0



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

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

Instance Method Details

#connect!true

Note:

This method mutates the connection class by setting a socket if one previously did not exist.

Tell the underlying socket to establish a connection to the host.

Examples:

Connect to the host.

connection.connect!

Returns:

  • (true)

    If the connection succeeded.

Since:

  • 2.0.0



71
72
73
74
75
76
77
# File 'lib/mongo/server/monitor/connection.rb', line 71

def connect!
  unless socket
    @socket = address.socket(timeout, ssl_options)
    socket.connect!
  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



90
91
92
93
94
95
96
# File 'lib/mongo/server/monitor/connection.rb', line 90

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



53
54
55
56
57
58
# File 'lib/mongo/server/monitor/connection.rb', line 53

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

#timeoutFloat

Get the connection timeout.

Examples:

Get the connection timeout.

connection.timeout

Returns:

  • (Float)

    The connection timeout in seconds.

Since:

  • 2.0.0



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

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