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

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 51
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 63
def connected?
  !!@socket && @socket.alive?
end
timeout() click to toggle source

Get the timeout to execute an operation on a socket.

@example Get the timeout to execute an operation on a socket.

connection.timeout

@return [ Float ] The operation timeout in seconds.

@since 2.0.0

# File lib/mongo/server/connectable.rb, line 75
def timeout
  @timeout ||= options[:socket_timeout] || TIMEOUT
end

Private Instance Methods

ensure_connected() { |socket| ... } click to toggle source
# File lib/mongo/server/connectable.rb, line 87
def ensure_connected
  ensure_same_process!
  connect!
  begin
    yield socket
  rescue Exception => e
    disconnect!
    raise e
  end
end
ensure_same_process!() click to toggle source
# File lib/mongo/server/connectable.rb, line 98
def ensure_same_process!
  if pid != Process.pid
    disconnect!
    @pid = Process.pid
  end
end
read() click to toggle source
# File lib/mongo/server/connectable.rb, line 105
def read
  ensure_connected do |socket|
    Protocol::Reply.deserialize(socket, max_message_size)
  end
end
ssl_options() click to toggle source
# File lib/mongo/server/connectable.rb, line 83
def ssl_options
  @ssl_options[:ssl] == true ? @ssl_options : {}
end