class Mongo::Server::ConnectionCommon

Common methods used by both monitoring and non-monitoring connections.

@note 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.

@api semipublic

Attributes

compressor[R]

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.

@return [ String | nil ] The compressor.

pid[R]

@return [ Integer ] pid The process id when the connection was created. @api private

socket[R]

Public Instance Methods

connected?() click to toggle source

Determine if the connection is currently connected.

@example Is the connection connected?

connection.connected?

@return [ true, false ] If connected.

@deprecated

# File lib/mongo/server/connection_common.rb, line 44
def connected?
  !!socket
end

Private Instance Methods

add_server_diagnostics() { || ... } click to toggle source

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 83
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}"
  if respond_to?(:id)
    note << ", connection #{generation}:#{id}"
  end
  e.add_note(note)
  if respond_to?(:generation)
    # Non-monitoring connections
    e.generation = generation
  end
  raise e
end
ensure_connected() { |socket| ... } click to toggle source
# File lib/mongo/server/connection_common.rb, line 116
def ensure_connected
  begin
    unless socket
      raise ArgumentError, "Connection #{generation}:#{id} for #{address.seed} is not connected"
    end
    if @error
      raise Error::ConnectionPerished, "Connection #{generation}:#{id} for #{address.seed} is perished"
    end
    result = yield socket
    success = true
    result
  ensure
    unless success
      @error = true
    end
  end
end
set_compressor!(reply) click to toggle source
# File lib/mongo/server/connection_common.rb, line 56
def set_compressor!(reply)
  server_compressors = reply['compression']

  if options[:compressors]
    if intersection = (server_compressors & options[: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: #{options[:compressors].join(', ')}. " +
          "Compression will not be used"
      else
        "The server at #{address} did not advertise compression support. " +
          "Requested algorithms: #{options[:compressors].join(', ')}. " +
          "Compression will not be used"
      end
      log_warn(msg)
    end
  end
end
ssl_options() click to toggle source
# File lib/mongo/server/connection_common.rb, line 103
def ssl_options
  @ssl_options ||= if options[:ssl]
    options.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