module Mongo::Server::Connectable

This provides common behaviour for connection objects.

@since 2.0.0

Constants

SSL

The ssl option prefix.

@since 2.1.0

TIMEOUT

The default time in seconds to timeout an operation executed on a socket.

@since 2.0.0

@deprecated Timeouts on Ruby sockets aren't effective so this default option is

no longer used.
Will be removed in driver version 3.0.

Attributes

address[R]

@return [ Mongo::Address ] address The address to connect to.

options[R]

@return [ Hash ] options The passed in options.

pid[R]

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

socket[R]

Public Instance Methods

connectable?() click to toggle source

Determine if the server is connectable. This will check not only if the connection exists, but if messages can send to it successfully.

@example Is the server connectable?

connection.connectable?

@return [ true, false ] If the connection is connectable.

@since 2.1.0

# File lib/mongo/server/connectable.rb, line 55
def connectable?
  begin; ping; rescue; false; end
end
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 Use connectable? instead

# File lib/mongo/server/connectable.rb, line 67
def connected?
  !!@socket && @socket.alive?
end

Private Instance Methods

ensure_connected() { |socket| ... } click to toggle source
# File lib/mongo/server/connectable.rb, line 81
def ensure_connected
  ensure_same_process!
  begin
    connect!
    result = yield socket
    success = true
    result
  ensure
    success or disconnect!
  end
end
ensure_same_process!() click to toggle source
# File lib/mongo/server/connectable.rb, line 93
def ensure_same_process!
  if pid != Process.pid
    disconnect!
    @pid = Process.pid
  end
end
read(request_id = nil) click to toggle source
# File lib/mongo/server/connectable.rb, line 100
def read(request_id = nil)
  ensure_connected do |socket|
    Protocol::Message.deserialize(socket, max_message_size, request_id)
  end
end
ssl_options() click to toggle source
# File lib/mongo/server/connectable.rb, line 77
def ssl_options
  @ssl_options[:ssl] == true ? @ssl_options : {}
end