class Mongo::Server::ConnectionBase

This class encapsulates common connection functionality.

@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

options[R]

@return [ Hash ] options The passed in options.

server[R]

@return [ Server ] The server that this connection is for.

@api private

Public Instance Methods

app_metadata() click to toggle source
# File lib/mongo/server/connection_base.rb, line 49
def app_metadata
  @app_metadata ||= begin
    same = true
    AppMetadata::AUTH_OPTION_KEYS.each do |key|
      if @server.options[key] != options[key]
        same = false
        break
      end
    end
    if same
      @server.app_metadata
    else
      AppMetadata.new(options)
    end
  end
end
dispatch(messages, operation_id = nil) click to toggle source

Dispatch a single message to the connection. If the message requires a response, a reply will be returned.

@example Dispatch the message.

connection.dispatch([ insert ])

@note This method is named dispatch since 'send' is a core Ruby method on

all objects.

@note For backwards compatibility, this method accepts the messages

as an array. However, exactly one message must be given per invocation.

@param [ Array<Message> ] messages A one-element array containing

the message to dispatch.

@param [ Integer ] operation_id The operation id to link messages.

@return [ Protocol::Message | nil ] The reply if needed.

@since 2.0.0

# File lib/mongo/server/connection_base.rb, line 85
def dispatch(messages, operation_id = nil)
  # The monitoring code does not correctly handle multiple messages,
  # and the driver internally does not send more than one message at
  # a time ever. Thus prohibit multiple message use for now.
  if messages.length != 1
    raise ArgumentError, 'Can only dispatch one message at a time'
  end
  message = messages.first
  deliver(message)
end

Private Instance Methods

deliver(message) click to toggle source
# File lib/mongo/server/connection_base.rb, line 98
def deliver(message)
  buffer = serialize(message)
  ensure_connected do |socket|
    operation_id = Monitoring.next_operation_id
    command_started(address, operation_id, message.payload,
      socket_object_id: socket.object_id, connection_id: id)
    start = Time.now
    result = nil
    begin
      socket.write(buffer.to_s)
      result = if message.replyable?
        Protocol::Message.deserialize(socket, max_message_size, message.request_id)
      else
        nil
      end
    rescue Exception => e
      total_duration = Time.now - start
      command_failed(nil, address, operation_id, message.payload, e.message, total_duration)
      raise
    else
      total_duration = Time.now - start
      command_completed(result, address, operation_id, message.payload, total_duration)
    end
    result
  end
end
serialize(message, buffer = BSON::ByteBuffer.new) click to toggle source
# File lib/mongo/server/connection_base.rb, line 125
def serialize(message, buffer = BSON::ByteBuffer.new)
  start_size = 0
  message.compress!(compressor, options[:zlib_compression_level]).serialize(buffer, max_bson_object_size)
  if max_message_size &&
    (buffer.length - start_size) > max_message_size
  then
    raise Error::MaxMessageSize.new(max_message_size)
  end
  buffer
end