Class: Mongo::Server::Connection
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Forwardable,
ConnectionBase ,
Forwardable,
ConnectionCommon
|
|
Instance Chain:
|
|
Inherits: |
Mongo::Server::ConnectionBase
|
Defined in: | lib/mongo/server/connection.rb |
Overview
This class models the socket connections for servers and their behavior.
Constant Summary
-
PING =
Deprecated.
No longer necessary with
::Mongo::Server
Selection specification.The ping command.
{ :ping => 1 }.freeze
-
PING_BYTES =
Deprecated.
No longer necessary with
::Mongo::Server
Selection specification.The ping message as raw bytes.
PING_MESSAGE.serialize.to_s.freeze
-
PING_MESSAGE =
Deprecated.
No longer necessary with
::Mongo::Server
Selection specification.Ping message.
Protocol::Query.new(Database::ADMIN, Database::COMMAND, PING, :limit => -1)
-
PING_OP_MSG =
Deprecated.
No longer necessary with
::Mongo::Server
Selection specification.The ping command for an OP_MSG (server versions >= 3.6).
{ :ping => 1, '$db' => Database::ADMIN }.freeze
-
PING_OP_MSG_BYTES =
Deprecated.
No longer necessary with
::Mongo::Server
Selection specification.The ping OP_MSG message as raw bytes (server versions >= 3.6).
PING_OP_MSG_MESSAGE.serialize.to_s.freeze
-
PING_OP_MSG_MESSAGE =
Deprecated.
No longer necessary with
::Mongo::Server
Selection specification.Ping message as an OP_MSG (server versions >= 3.6).
Protocol::Msg.new([], {}, PING_OP_MSG)
ConnectionCommon
- Inherited
::Mongo::Loggable
- Included
ConnectionBase
- Inherited
DEFAULT_MAX_BSON_OBJECT_SIZE, MAX_BSON_COMMAND_OVERHEAD, REDUCED_MAX_BSON_SIZE
Class Method Summary
-
.new(server, options = {}) ⇒ Connection
constructor
Internal use only
Internal use only
Creates a new connection object to the specified target address with the specified options.
Instance Attribute Summary
-
#closed? ⇒ true | false
readonly
Whether the connection was closed.
-
#connected? ⇒ true | false
readonly
Whether the connection was connected and was not interrupted, closed, or had an error raised.
- #error? ⇒ Boolean readonly Internal use only Internal use only
-
#global_id ⇒ Integer
readonly
across all connections.
-
#id ⇒ Integer
readonly
across connections to the same server object.
-
#interrupted? ⇒ true | false
readonly
Whether the connection was interrupted.
- #last_checkin ⇒ Time readonly
-
#pinned? ⇒ Boolean
readonly
Internal use only
Internal use only
Whether the connection is used by a transaction or cursor operations.
ConnectionBase
- Inherited
#description | Returns the server description for this connection, derived from the hello response for the handshake performed on this connection. |
#options, #server |
::Mongo::Monitoring::Publishable
- Included
ConnectionCommon
- Inherited
#compressor | The compressor negotiated during the handshake for this connection, if any. |
#connected? | Determine if the connection is currently connected. |
#pid, #socket |
Instance Method Summary
-
#connect!(context = nil) ⇒ true
Establishes a network connection to the target address.
-
#connection_pool
Internal use only
Internal use only
The connection pool from which this connection was created.
-
#disconnect!(options = nil) ⇒ true
Disconnect the connection.
-
#interrupted!
Mark the connection as interrupted.
-
#pin
Internal use only
Internal use only
Mark the connection as pinned.
-
#ping ⇒ true, false
deprecated
Deprecated.
No longer necessary with
::Mongo::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.
-
#timeout
Alias for #socket_timeout.
-
#unpin
Internal use only
Internal use only
Mark the connection as not pinned.
-
#create_socket(context = nil) ⇒ Socket
private
Creates the socket.
- #deliver(message, client, options = {}) private
-
#do_connect ⇒ Array<Server::Description, String | Symbol>
private
Separate method to permit easier mocking in the test suite.
- #handle_errors private
- #raise_if_closed! private
::Mongo::Retryable
- Included
#read_worker | Returns the read worker for handling retryable reads. |
#select_server | This is a separate method to make it possible for the test suite to assert that server selection is performed during retry attempts. |
#write_worker | Returns the write worker for handling retryable writes. |
ConnectionBase
- Inherited
#app_metadata, | |
#dispatch | Dispatch a single message to the connection. |
#generation |
|
#service_id, | |
#check_timeout! | If timeoutMS is set for the operation context, checks whether there is enough time left to send the corresponding message to the server (remaining timeout is bigger than minimum round trip time for the server). |
#deliver, #serialize |
::Mongo::Monitoring::Publishable
- Included
#publish_cmap_event, #publish_event, #publish_sdam_event, #command_completed, #command_failed, #command_started, #command_succeeded, #duration |
::Mongo::Loggable
- Included
#log_debug | Convenience method to log debug messages with the standard prefix. |
#log_error | Convenience method to log error messages with the standard prefix. |
#log_fatal | Convenience method to log fatal messages with the standard prefix. |
#log_info | Convenience method to log info messages with the standard prefix. |
#log_warn | Convenience method to log warn messages with the standard prefix. |
#logger | Get the logger instance. |
#_mongo_log_prefix, #format_message |
ConnectionCommon
- Inherited
#handshake_command | Build a command that should be used for connection handshake. |
#handshake_document | Build a document that should be used for connection handshake. |
#add_server_diagnostics | Yields to the block and, if the block raises an exception, adds a note to the exception with the address of the specified server. |
#ensure_connected, #set_compressor!, #ssl_options |
Instance Attribute Details
#closed? ⇒ true
| false
(readonly)
Whether the connection was closed.
Closed connections should no longer be used. Instead obtain a new connection from the connection pool.
# File 'lib/mongo/server/connection.rb', line 166
def closed? !!@closed end
#connected? ⇒ true
| false
(readonly)
Whether the connection was connected and was not interrupted, closed, or had an error raised.
# File 'lib/mongo/server/connection.rb', line 154
def connected? !closed? && !error? && !interrupted? && !!@socket end
#error? ⇒ Boolean
(readonly)
# File 'lib/mongo/server/connection.rb', line 186
def error? !!@error end
#global_id ⇒ Integer
(readonly)
across all connections.
# File 'lib/mongo/server/connection.rb', line 140
attr_reader :global_id
#id ⇒ Integer
(readonly)
across connections to the same server object.
# File 'lib/mongo/server/connection.rb', line 136
attr_reader :id
#interrupted? ⇒ true
| false
(readonly)
Whether the connection was interrupted.
Interrupted connections were already removed from the pool and should not be checked back into the pool.
# File 'lib/mongo/server/connection.rb', line 176
def interrupted? !!@interrupted end
#last_checkin ⇒ Time
(readonly)
# File 'lib/mongo/server/connection.rb', line 130
attr_reader :last_checkin
#pinned? ⇒ Boolean
(readonly)
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.
# File 'lib/mongo/server/connection.rb', line 198
def pinned? @pinned end
Instance Method Details
#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.
# File 'lib/mongo/server/connection.rb', line 229
def connect!(context = nil) raise_if_closed! unless @socket @socket = create_socket(context) @description, @compressor = do_connect if server.load_balancer? if Lint.enabled? unless service_id raise Error::InternalDriverError, "The connection is to a load balancer and it must have service_id set here, but does not" end 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
#connection_pool
The connection pool from which this connection was created. May be nil.
# File 'lib/mongo/server/connection.rb', line 146
def connection_pool [:connection_pool] end
#create_socket(context = nil) ⇒ Socket (private)
Creates the socket. The method is separate from do_connect, so that pending connections can be closed if they are interrupted during hello.
# File 'lib/mongo/server/connection.rb', line 259
private def create_socket(context = nil) add_server_diagnostics do opts = .merge( connection_address: address, connection_generation: generation, pipe: [:pipe], connect_timeout: context&.remaining_timeout_sec, csot: !!context&.csot? ) address.socket(socket_timeout, opts) end end
#deliver(message, client, options = {}) (private)
# File 'lib/mongo/server/connection.rb', line 393
def deliver(, client, = {}) handle_errors do super end 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 ::Mongo::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.
# File 'lib/mongo/server/connection.rb', line 311
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 socket.close rescue nil @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
#do_connect ⇒ Array
<Server::Description, String
| Symbol> (private)
Separate method to permit easier mocking in the test suite.
# File 'lib/mongo/server/connection.rb', line 277
private def do_connect raise_if_closed! begin pending_connection = PendingConnection.new( socket, @server, monitoring, .merge(id: id)) pending_connection.handshake_and_authenticate! rescue Exception socket&.close @socket = nil raise end [pending_connection.description, pending_connection.compressor] end
#handle_errors (private)
# File 'lib/mongo/server/connection.rb', line 399
def handle_errors begin yield rescue Error::SocketError => e @error = e @server.unknown!( generation: e.generation, # or description.service_id? service_id: e.service_id, stop_push_monitor: true, ) raise rescue Error::SocketTimeoutError => e @error = e raise end end
#interrupted!
Mark the connection as interrupted.
# File 'lib/mongo/server/connection.rb', line 181
def interrupted! @interrupted = true end
#pin
Mark the connection as pinned.
# File 'lib/mongo/server/connection.rb', line 205
def pin @pinned = true end
#ping ⇒ true
, false
No longer necessary with ::Mongo::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.
# File 'lib/mongo/server/connection.rb', line 356
def ping bytes = features.op_msg_enabled? ? PING_OP_MSG_BYTES : PING_BYTES ensure_connected do |socket| reply = add_server_diagnostics do socket.write(bytes) Protocol::Message.deserialize(socket, ) end reply.documents[0][Operation::Result::OK] == 1 end end
#raise_if_closed! (private)
# File 'lib/mongo/server/connection.rb', line 417
def raise_if_closed! if error? raise Error::ConnectionPerished, "Connection #{generation}:#{id} for #{address.seed} is perished. Reconnecting closed or errored connections is no longer supported" end if closed? raise Error::ConnectionPerished, "Connection #{generation}:#{id} for #{address.seed} is closed. Reconnecting closed or errored connections is no longer supported" end end
#record_checkin! ⇒ self
Record the last checkin time.
# File 'lib/mongo/server/connection.rb', line 386
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.
# File 'lib/mongo/server/connection.rb', line 372
def socket_timeout @timeout ||= [:socket_timeout] end
#timeout
Alias for #socket_timeout.
# File 'lib/mongo/server/connection.rb', line 376
alias :timeout :socket_timeout
#unpin
Mark the connection as not pinned.
# File 'lib/mongo/server/connection.rb', line 212
def unpin @pinned = false end