Class: Mongo::Server::Connection

Inherits:
ConnectionBase show all
Extended by:
Forwardable
Includes:
Monitoring::Publishable, Retryable, Connectable
Defined in:
lib/mongo/server/connection.rb

Overview

This class models the socket connections for servers and their behavior.

Since:

  • 2.0.0

Constant Summary collapse

PING =
Deprecated.

No longer necessary with Server Selection specification.

The ping command.

Since:

  • 2.1.0

{ :ping => 1 }.freeze
PING_OP_MSG =
Deprecated.

No longer necessary with Server Selection specification.

The ping command for an OP_MSG (server versions >= 3.6).

Since:

  • 2.5.0

{ :ping => 1, '$db' => Database::ADMIN }.freeze
PING_MESSAGE =
Deprecated.

No longer necessary with Server Selection specification.

Ping message.

Since:

  • 2.1.0

Protocol::Query.new(Database::ADMIN, Database::COMMAND, PING, :limit => -1)
PING_OP_MSG_MESSAGE =
Deprecated.

No longer necessary with Server Selection specification.

Ping message as an OP_MSG (server versions >= 3.6).

Since:

  • 2.5.0

Protocol::Msg.new([], {}, PING_OP_MSG)
PING_BYTES =
Deprecated.

No longer necessary with Server Selection specification.

The ping message as raw bytes.

Since:

  • 2.1.0

PING_MESSAGE.serialize.to_s.freeze
PING_OP_MSG_BYTES =
Deprecated.

No longer necessary with Server Selection specification.

The ping OP_MSG message as raw bytes (server versions >= 3.6).

Since:

  • 2.5.0

PING_OP_MSG_MESSAGE.serialize.to_s.freeze

Constants included from Loggable

Loggable::PREFIX

Constants included from Connectable

Mongo::Server::Connectable::SSL, Mongo::Server::Connectable::TIMEOUT

Instance Attribute Summary collapse

Attributes included from Monitoring::Publishable

#monitoring

Attributes included from Connectable

#pid

Attributes inherited from ConnectionBase

#options

Instance Method Summary collapse

Methods included from Retryable

#read_with_one_retry, #read_with_retry, #write_with_retry

Methods included from Monitoring::Publishable

#publish_event, #publish_sdam_event

Methods included from Loggable

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

Methods included from Connectable

#connectable?, #connected?

Methods inherited from ConnectionBase

#address, #app_metadata, #dispatch

Constructor Details

#initialize(server, 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 Server.

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, handshakes nor authenticates); call connect! method on the connection object to create the network connection.

Examples:

Create the connection.

Connection.new(server)

Parameters:

  • server (Mongo::Server)

    The server the connection is for.

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

    The connection options.

Options Hash (options):

  • :generation (Integer)

    Connection pool's generation for this connection.

Since:

  • 2.0.0



91
92
93
94
95
96
97
98
99
100
# File 'lib/mongo/server/connection.rb', line 91

def initialize(server, options = {})
  @monitoring = server.monitoring
  @options = options.freeze
  @server = server
  @ssl_options = options.select { |k, v| k.to_s.start_with?(SSL) }.freeze
  @socket = nil
  @last_checkin = nil
  @auth_mechanism = nil
  @pid = Process.pid
end

Instance Attribute Details

#last_checkinObject (readonly)

The last time the connection was checked back into a pool.

Since:

  • 2.5.0



105
106
107
# File 'lib/mongo/server/connection.rb', line 105

def last_checkin
  @last_checkin
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



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/mongo/server/connection.rb', line 129

def connect!
  unless @socket
    socket = address.socket(socket_timeout, ssl_options,
      connect_timeout: address.connect_timeout)
    handshake!(socket)
    pending_connection = PendingConnection.new(socket, @server, monitoring, options)
    authenticate!(pending_connection)
    # When @socket is assigned, the socket should have handshaken and
    # authenticated and be usable.
    @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



154
155
156
157
158
159
160
161
162
# File 'lib/mongo/server/connection.rb', line 154

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

#generationObject

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.

Connection pool generation from which this connection was created. May be nil.

Since:

  • 2.7.0



112
113
114
# File 'lib/mongo/server/connection.rb', line 112

def generation
  options[:generation]
end

#pingtrue, false

Deprecated.

No longer necessary with Server Selection specification.

Note:

This uses a pre-serialized ping message for optimization.

Ping the connection to see if the server is responding to commands. This is non-blocking on the server side.

Examples:

Ping the connection.

connection.ping

Returns:

  • (true, false)

    If the server is accepting connections.

Since:

  • 2.1.0



177
178
179
180
181
182
183
184
# File 'lib/mongo/server/connection.rb', line 177

def ping
  bytes = features.op_msg_enabled? ? PING_OP_MSG_BYTES : PING_BYTES
  ensure_connected do |socket|
    socket.write(bytes)
    reply = Protocol::Message.deserialize(socket, max_message_size)
    reply.documents[0][Operation::Result::OK] == 1
  end
end

#record_checkin!self

Record the last checkin time.

Examples:

Record the checkin time on this connection.

connection.record_checkin!

Returns:

  • (self)

Since:

  • 2.5.0



208
209
210
211
# File 'lib/mongo/server/connection.rb', line 208

def record_checkin!
  @last_checkin = Time.now
  self
end

#socket_timeoutFloat Also known as: timeout

Get the timeout to execute an operation on a socket.

Examples:

Get the timeout to execute an operation on a socket.

connection.timeout

Returns:

  • (Float)

    The operation timeout in seconds.

Since:

  • 2.0.0



194
195
196
# File 'lib/mongo/server/connection.rb', line 194

def socket_timeout
  @timeout ||= options[:socket_timeout]
end