Class: Mongo::Server::Connection

Inherits:
Object
  • Object
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

PING =

The ping command.

Since:

  • 2.1.0

{ :ping => 1 }.freeze
PING_MESSAGE =

Ping message.

Since:

  • 2.1.0

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

The ping message as raw bytes.

Since:

  • 2.1.0

PING_MESSAGE.serialize.to_s.freeze

Constants included from Connectable

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

Instance Attribute Summary

Attributes included from Monitoring::Publishable

#monitoring

Attributes included from Connectable

#address, #options, #pid

Instance Method Summary collapse

Methods included from Retryable

#read_with_one_retry, #read_with_retry, #write_with_retry

Methods included from Monitoring::Publishable

#publish_command, #publish_event, #publish_sdam_event

Methods included from Connectable

#connectable?, #connected?

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.

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

Examples:

Create the connection.

Connection.new(server)

Parameters:

  • server (Mongo::Server)

    The server the connection is for.

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

    The connection options.

Since:

  • 2.0.0



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

def initialize(server, options = {})
  @address = server.address
  @monitoring = server.monitoring
  @options = options.freeze
  @server = server
  @ssl_options = options.reject { |k, v| !k.to_s.start_with?(SSL) }
  @socket = nil
  @auth_mechanism = 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



60
61
62
63
64
65
66
67
68
# File 'lib/mongo/server/connection.rb', line 60

def connect!
  unless socket && socket.connectable?
    @socket = address.socket(socket_timeout, ssl_options)
    address.connect_socket!(socket)
    handshake!
    authenticate!
  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



81
82
83
84
85
86
87
88
# File 'lib/mongo/server/connection.rb', line 81

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

#dispatch(messages, operation_id = nil) ⇒ Protocol::Reply

Note:

This method is named dispatch since 'send' is a core Ruby method on all objects.

Dispatch the provided messages to the connection. If the last message requires a response a reply will be returned.

Examples:

Dispatch the messages.

connection.dispatch([ insert, command ])

Parameters:

  • messages (Array<Message>)

    The messages to dispatch.

  • operation_id (Integer) (defaults to: nil)

    The operation id to link messages.

Returns:

Since:

  • 2.0.0



105
106
107
108
109
110
111
112
113
# File 'lib/mongo/server/connection.rb', line 105

def dispatch(messages, operation_id = nil)
  if monitoring.subscribers?(Monitoring::COMMAND)
    publish_command(messages, operation_id || Monitoring.next_operation_id) do |msgs|
      deliver(msgs)
    end
  else
    deliver(messages)
  end
end

#pingtrue, false

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



151
152
153
154
155
156
157
# File 'lib/mongo/server/connection.rb', line 151

def ping
  ensure_connected do |socket|
    socket.write(PING_BYTES)
    reply = Protocol::Reply.deserialize(socket, max_message_size)
    reply.documents[0][Operation::Result::OK] == 1
  end
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



167
168
169
# File 'lib/mongo/server/connection.rb', line 167

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