Class: Mongo::Server::Connection
- Inherits:
-
Object
- Object
- Mongo::Server::Connection
- 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.
Constant Summary collapse
- PING =
Deprecated.
No longer necessary with Server Selection specification.
The ping command.
{ :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).
{ :ping => 1, '$db' => Database::ADMIN }.freeze
- PING_MESSAGE =
Deprecated.
No longer necessary with Server Selection specification.
Ping message.
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).
Protocol::Msg.new([:none], {}, PING_OP_MSG)
- PING_BYTES =
Deprecated.
No longer necessary with Server Selection specification.
The ping message as raw bytes.
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).
PING_OP_MSG_MESSAGE.serialize.to_s.freeze
Constants included from Loggable
Constants included from Connectable
Mongo::Server::Connectable::SSL, Mongo::Server::Connectable::TIMEOUT
Instance Attribute Summary collapse
-
#last_checkin ⇒ Object
readonly
The last time the connection was checked back into a pool.
Attributes included from Monitoring::Publishable
Attributes included from Connectable
Instance Method Summary collapse
-
#connect! ⇒ true
Tell the underlying socket to establish a connection to the host.
-
#disconnect! ⇒ true
Disconnect the connection.
-
#dispatch(messages, operation_id = nil) ⇒ Protocol::Message | nil
Dispatch the provided messages to the connection.
-
#initialize(server, options = {}) ⇒ Connection
constructor
private
Initialize a new socket connection from the client to the server.
-
#ping ⇒ true, false
deprecated
Deprecated.
No longer necessary with Server Selection specification.
-
#record_checkin! ⇒ self
Record the last checkin time.
-
#socket_timeout ⇒ Float
(also: #timeout)
Get the timeout to execute an operation on a socket.
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 Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Methods included from Connectable
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.
Connection must never be directly instantiated outside of a Server.
Initialize a new socket connection from the client to the server.
165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/mongo/server/connection.rb', line 165 def initialize(server, = {}) @address = server.address @monitoring = server.monitoring @options = .freeze @server = server @ssl_options = .reject { |k, v| !k.to_s.start_with?(SSL) } @socket = nil @last_checkin = nil @auth_mechanism = nil @pid = Process.pid end |
Instance Attribute Details
#last_checkin ⇒ Object (readonly)
The last time the connection was checked back into a pool.
72 73 74 |
# File 'lib/mongo/server/connection.rb', line 72 def last_checkin @last_checkin end |
Instance Method Details
#connect! ⇒ true
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.
95 96 97 98 99 100 101 102 103 |
# File 'lib/mongo/server/connection.rb', line 95 def connect! unless socket && socket.connectable? @socket = address.socket(socket_timeout, ) address.connect_socket!(socket) handshake! authenticate! end true end |
#disconnect! ⇒ true
This method mutates the connection by setting the socket to nil if the closing succeeded.
Disconnect the connection.
116 117 118 119 120 121 122 123 124 |
# File 'lib/mongo/server/connection.rb', line 116 def disconnect! @auth_mechanism = nil @last_checkin = nil if socket socket.close @socket = nil end true end |
#dispatch(messages, operation_id = nil) ⇒ Protocol::Message | nil
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.
141 142 143 144 145 146 147 148 149 |
# File 'lib/mongo/server/connection.rb', line 141 def dispatch(, operation_id = nil) if monitoring.subscribers?(Monitoring::COMMAND) publish_command(, operation_id || Monitoring.next_operation_id) do |msgs| deliver(msgs) end else deliver() end end |
#ping ⇒ true, false
No longer necessary with Server Selection specification.
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.
190 191 192 193 194 195 196 197 |
# File 'lib/mongo/server/connection.rb', line 190 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, ) reply.documents[0][Operation::Result::OK] == 1 end end |
#record_checkin! ⇒ self
Record the last checkin time.
221 222 223 224 |
# File 'lib/mongo/server/connection.rb', line 221 def record_checkin! @last_checkin = Time.now self end |
#socket_timeout ⇒ Float Also known as: timeout
Get the timeout to execute an operation on a socket.
207 208 209 |
# File 'lib/mongo/server/connection.rb', line 207 def socket_timeout @timeout ||= [:socket_timeout] end |