Class: Mongo::Server::Connection
- Inherits:
-
ConnectionBase
- Object
- ConnectionCommon
- ConnectionBase
- Mongo::Server::Connection
- Extended by:
- Forwardable
- Includes:
- Id, Monitoring::Publishable, Retryable
- 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
{ :ping => 1, '$db' => Database::ADMIN }.freeze
- PING_OP_MSG_MESSAGE =
Deprecated.
No longer necessary with Server Selection specification.
Ping message as an OP_MSG
Protocol::Msg.new([], {}, PING_OP_MSG)
- PING_OP_MSG_BYTES =
Deprecated.
No longer necessary with Server Selection specification.
The ping OP_MSG message as raw bytes
PING_OP_MSG_MESSAGE.serialize.to_s.freeze
Constants included from Loggable
Constants inherited from ConnectionBase
Mongo::Server::ConnectionBase::DEFAULT_MAX_BSON_OBJECT_SIZE, Mongo::Server::ConnectionBase::MAX_BSON_COMMAND_OVERHEAD, Mongo::Server::ConnectionBase::REDUCED_MAX_BSON_SIZE
Instance Attribute Summary collapse
-
#global_id ⇒ Integer
readonly
across all connections.
-
#id ⇒ Integer
readonly
across connections to the same server object.
-
#last_checkin ⇒ Time
readonly
The last time the connection was checked back into a pool.
-
#tracing_attributes ⇒ Hash | nil
private
OpenTelemetry span attributes describing this connection.
Attributes included from Monitoring::Publishable
Attributes inherited from ConnectionBase
#description, #options, #server
Attributes inherited from ConnectionCommon
Instance Method Summary collapse
-
#closed? ⇒ true | false
Whether the connection was closed.
-
#connect!(context = nil) ⇒ true
Establishes a network connection to the target address.
-
#connected? ⇒ true | false
Whether the connection was connected and was not interrupted, closed, or had an error raised.
-
#connection_pool ⇒ Object
private
The connection pool from which this connection was created.
-
#disconnect!(options = nil) ⇒ true
Disconnect the connection.
- #error? ⇒ Boolean private
-
#initialize(server, options = {}) ⇒ Connection
constructor
private
Creates a new connection object to the specified target address with the specified options.
-
#interrupted! ⇒ Object
Mark the connection as interrupted.
-
#interrupted? ⇒ true | false
Whether the connection was interrupted.
-
#pin(reason = :cursor) ⇒ Object
private
Mark the connection as pinned for the given reason.
-
#ping ⇒ true, false
deprecated
Deprecated.
No longer necessary with Server Selection specification.
-
#pinned? ⇒ Boolean
private
Whether the connection is used by a transaction or cursor operations.
-
#record_checkin! ⇒ self
Record the last checkin time.
-
#socket_timeout ⇒ Float
(also: #timeout)
Get the timeout to execute an operation on a socket.
-
#transport ⇒ Symbol | nil
private
Get the transport type for this connection.
-
#unpin(reason = :cursor) ⇒ Object
private
Remove a pin from the connection for the given reason.
-
#unpin_all ⇒ Object
private
Remove all pins from the connection.
Methods included from Id
Methods included from Retryable
#read_worker, #select_server, #with_overload_retry, #write_worker
Methods included from Monitoring::Publishable
#publish_cmap_event, #publish_event, #publish_sdam_event
Methods included from Loggable
#log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger
Methods inherited from ConnectionBase
#app_metadata, #dispatch, #generation, #service_id
Methods inherited from ConnectionCommon
#handshake_command, #handshake_document, #interrupt_socket
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.
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.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/mongo/server/connection.rb', line 90 def initialize(server, = {}) super() if server.load_balancer? && [:generation] raise ArgumentError, 'Generation cannot be set when server is a load balancer' end @id = server.next_connection_id @global_id = self.class.next_id @monitoring = server.monitoring @options = .freeze @server = server @socket = nil @last_checkin = nil @auth_mechanism = nil @pid = Process.pid @pin_reasons = Set.new publish_cmap_event( Monitoring::Event::Cmap::ConnectionCreated.new(address, id) ) end |
Instance Attribute Details
#global_id ⇒ Integer (readonly)
across all connections.
125 126 127 |
# File 'lib/mongo/server/connection.rb', line 125 def global_id @global_id end |
#id ⇒ Integer (readonly)
across connections to the same server object.
121 122 123 |
# File 'lib/mongo/server/connection.rb', line 121 def id @id end |
#last_checkin ⇒ Time (readonly)
Returns The last time the connection was checked back into a pool.
115 116 117 |
# File 'lib/mongo/server/connection.rb', line 115 def last_checkin @last_checkin end |
#tracing_attributes ⇒ Hash | nil
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.
Returns OpenTelemetry span attributes describing this connection. Built once by the tracer and reused for every command on the connection.
132 133 134 |
# File 'lib/mongo/server/connection.rb', line 132 def tracing_attributes @tracing_attributes end |
Instance Method Details
#closed? ⇒ true | false
Whether the connection was closed.
Closed connections should no longer be used. Instead obtain a new connection from the connection pool.
160 161 162 |
# File 'lib/mongo/server/connection.rb', line 160 def closed? !!@closed end |
#connect!(context = nil) ⇒ true
This method mutates the connection object 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.
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/mongo/server/connection.rb', line 236 def connect!(context = nil) raise_if_closed! unless @socket @socket = create_socket(context) @description, @compressor = do_connect if server.load_balancer? if Lint.enabled? && !service_id raise Error::InternalDriverError, 'The connection is to a load balancer and it must have service_id set here, but does not' end @generation = connection_pool.generation_manager.generation(service_id: service_id) end publish_cmap_event( Monitoring::Event::Cmap::ConnectionReady.new(address, id) ) @close_event_published = false end true end |
#connected? ⇒ true | false
Whether the connection was connected and was not interrupted, closed, or had an error raised.
148 149 150 |
# File 'lib/mongo/server/connection.rb', line 148 def connected? !closed? && !error? && !interrupted? && !!@socket end |
#connection_pool ⇒ Object
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.
The connection pool from which this connection was created. May be nil.
140 141 142 |
# File 'lib/mongo/server/connection.rb', line 140 def connection_pool [:connection_pool] end |
#disconnect!(options = nil) ⇒ true
Once a connection is disconnected, it should no longer be used. A new connection should be obtained from the connection pool which will either return a ready connection or create a new connection. If linting is enabled, reusing a disconnected connection will raise Error::LintError. If linting is not enabled, a warning will be logged.
This method mutates the connection object by setting the socket to nil if the closing succeeded.
Disconnect the connection.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/mongo/server/connection.rb', line 318 def disconnect!( = nil) # NOTE: @closed may be true here but we also may have a socket. # Check the socket and not @closed flag. @auth_mechanism = nil @last_checkin = nil if socket begin socket.close rescue StandardError nil end @socket = nil end @closed = true interrupted! if && [:interrupted] # To satisfy CMAP spec tests, publish close events even if the # socket was never connected (and thus the ready event was never # published). But track whether we published close event and do not # publish it multiple times, unless the socket was reconnected - # in that case publish the close event once per socket close. unless @close_event_published reason = && [:reason] publish_cmap_event( Monitoring::Event::Cmap::ConnectionClosed.new( address, id, reason ) ) @close_event_published = true end true end |
#error? ⇒ Boolean
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.
180 181 182 |
# File 'lib/mongo/server/connection.rb', line 180 def error? !!@error end |
#interrupted! ⇒ Object
Mark the connection as interrupted.
175 176 177 |
# File 'lib/mongo/server/connection.rb', line 175 def interrupted! @interrupted = true end |
#interrupted? ⇒ true | false
Whether the connection was interrupted.
Interrupted connections were already removed from the pool and should not be checked back into the pool.
170 171 172 |
# File 'lib/mongo/server/connection.rb', line 170 def interrupted? !!@interrupted end |
#pin(reason = :cursor) ⇒ Object
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.
Mark the connection as pinned for the given reason.
202 203 204 |
# File 'lib/mongo/server/connection.rb', line 202 def pin(reason = :cursor) @pin_reasons << reason 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.
367 368 369 370 371 372 373 374 375 |
# File 'lib/mongo/server/connection.rb', line 367 def ping ensure_connected do |socket| reply = add_server_diagnostics do socket.write(PING_OP_MSG_BYTES) Protocol::Message.deserialize(socket, ) end reply.documents[0][Operation::Result::OK] == 1 end end |
#pinned? ⇒ Boolean
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.
Whether the connection is used by a transaction or cursor operations.
Pinned connections should not be disconnected and removed from a connection pool if they are idle or stale.
@return [ true | false ] Whether connection is pinned.
192 193 194 |
# File 'lib/mongo/server/connection.rb', line 192 def pinned? !@pin_reasons.empty? end |
#record_checkin! ⇒ self
Record the last checkin time.
396 397 398 399 |
# File 'lib/mongo/server/connection.rb', line 396 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.
382 383 384 |
# File 'lib/mongo/server/connection.rb', line 382 def socket_timeout @timeout ||= [:socket_timeout] end |
#transport ⇒ Symbol | nil
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.
Get the transport type for this connection.
406 407 408 409 410 411 412 413 414 415 |
# File 'lib/mongo/server/connection.rb', line 406 def transport return nil if @socket.nil? case @socket when Mongo::Socket::Unix :unix else :tcp end end |
#unpin(reason = :cursor) ⇒ Object
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.
Remove a pin from the connection for the given reason.
212 213 214 |
# File 'lib/mongo/server/connection.rb', line 212 def unpin(reason = :cursor) @pin_reasons.delete(reason) end |
#unpin_all ⇒ Object
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.
Remove all pins from the connection.
219 220 221 |
# File 'lib/mongo/server/connection.rb', line 219 def unpin_all @pin_reasons.clear end |