Class: Mongo::Server::ConnectionCommon
| Relationships & Source Files | |
| Extension / Inclusion / Inheritance Descendants | |
|
Subclasses:
Connection, ConnectionBase, Mongo::Server::PendingConnection, Mongo::Server::Monitor::Connection, Mongo::Server::PushMonitor::Connection
|
|
| Inherits: | Object |
| Defined in: | lib/mongo/server/connection_common.rb |
Overview
Although methods of this module are part of the public API, the fact that these methods are defined on this module and not on the classes which include this module is not part of the public API.
Common methods used by both monitoring and non-monitoring connections.
Constant Summary
-
HELLO_DOC =
# File 'lib/mongo/server/connection_common.rb', line 104
BSON::Document.new({ hello: 1 }).freeze
-
LEGACY_HELLO_DOC =
# File 'lib/mongo/server/connection_common.rb', line 106
BSON::Document.new({ isMaster: 1, helloOk: true }).freeze
Instance Attribute Summary
-
#compressor ⇒ String | nil
readonly
The compressor negotiated during the handshake for this connection, if any.
- #connected? ⇒ true, false readonly deprecated Deprecated.
- #pid ⇒ Integer readonly Internal use only Internal use only
- #socket readonly private
Instance Method Summary
-
#handshake_command(handshake_document) ⇒ Protocol::Message
Internal use only
Internal use only
Build a command that should be used for connection handshake.
-
#handshake_document(app_metadata, speculative_auth_doc: nil, load_balancer: false, server_api: nil) ⇒ BSON::Document
Internal use only
Internal use only
Build a document that should be used for connection handshake.
-
#add_server_diagnostics
private
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 private
- #set_compressor!(reply) private
- #ssl_options private
Instance Attribute Details
#compressor ⇒ String | nil (readonly)
The compressor negotiated during the handshake for this connection, if any.
This attribute is nil for connections that haven’t completed the handshake yet, and for connections that negotiated no compression.
# File 'lib/mongo/server/connection_common.rb', line 34
attr_reader :compressor
#connected? ⇒ true, false (readonly)
Determine if the connection is currently connected.
# File 'lib/mongo/server/connection_common.rb', line 44
def connected? !!socket end
#pid ⇒ Integer (readonly)
# File 'lib/mongo/server/connection_common.rb', line 50
attr_reader :pid
#socket (readonly, private)
# File 'lib/mongo/server/connection_common.rb', line 108
attr_reader :socket
Instance Method Details
#add_server_diagnostics (private)
Yields to the block and, if the block raises an exception, adds a note to the exception with the address of the specified server.
This method is intended to add server address information to exceptions raised during execution of operations on servers.
# File 'lib/mongo/server/connection_common.rb', line 137
def add_server_diagnostics yield # Note that the exception should already have been mapped to a # Mongo::Error subclass when it gets to this method. rescue Error::SocketError, Error::SocketTimeoutError => e # Server::Monitor::Connection does not reference its server, but # knows its address. Server::Connection delegates the address to its # server. note = +"on #{address.seed}" note << ", connection #{generation}:#{id}" if respond_to?(:id) # Non-monitoring connections have service id. # Monitoring connections do not. note << ", service id #{service_id}" if respond_to?(:service_id) && service_id e.add_note(note) if respond_to?(:generation) # Non-monitoring connections e.generation = generation e.connection_global_id = global_id if respond_to?(:global_id) e.service_id = service_id if respond_to?(:description) end raise e end
#ensure_connected (private)
# File 'lib/mongo/server/connection_common.rb', line 173
def ensure_connected raise ArgumentError, "Connection #{generation}:#{id} for #{address.seed} is not connected" unless socket raise Error::ConnectionPerished, "Connection #{generation}:#{id} for #{address.seed} is perished" if @error result = yield socket success = true result ensure @error = true unless success end
#handshake_command(handshake_document) ⇒ Protocol::Message
Build a command that should be used for connection handshake.
# File 'lib/mongo/server/connection_common.rb', line 87
def handshake_command(handshake_document) if handshake_document['apiVersion'] || handshake_document['loadBalanced'] Protocol::Msg.new( [], {}, handshake_document.merge({ '$db' => Database::ADMIN }) ) else Protocol::Query.new( Database::ADMIN, Database::COMMAND, handshake_document, limit: -1 ) end end
#handshake_document(app_metadata, speculative_auth_doc: nil, load_balancer: false, server_api: nil) ⇒ BSON::Document
Build a document that should be used for connection handshake.
# File 'lib/mongo/server/connection_common.rb', line 65
def handshake_document(, speculative_auth_doc: nil, load_balancer: false, server_api: nil) serv_api = .server_api || server_api document = if serv_api HELLO_DOC.merge(Utils.transform_server_api(serv_api)) else LEGACY_HELLO_DOC end document.merge(.validated_document).tap do |doc| doc.update(speculativeAuthenticate: speculative_auth_doc) if speculative_auth_doc doc.update(loadBalanced: true) if load_balancer end end
#set_compressor!(reply) (private)
# File 'lib/mongo/server/connection_common.rb', line 110
def set_compressor!(reply) server_compressors = reply['compression'] return unless [:compressors] if intersection = (server_compressors & [:compressors]) @compressor = intersection.first else msg = if server_compressors "The server at #{address} has no compression algorithms in common with those requested. " + "Server algorithms: #{server_compressors.join(', ')}; " + "Requested algorithms: #{[:compressors].join(', ')}. " + 'Compression will not be used' else "The server at #{address} did not advertise compression support. " + "Requested algorithms: #{[:compressors].join(', ')}. " + 'Compression will not be used' end log_warn(msg) end end
#ssl_options (private)
# File 'lib/mongo/server/connection_common.rb', line 160
def @ssl_options ||= if [:ssl] .select { |k, _v| k.to_s.start_with?('ssl') } else # Due to the way options are propagated from the client, if we # decide that we don't want to use TLS we need to have the :ssl # option explicitly set to false or the value provided to the # connection might be overwritten by the default inherited from # the client. { ssl: false } end.freeze end