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
@return [ Mongo::Address ] address The address to connect to.
@return [ Hash ] options The passed in options.
@return [ Integer ] pid The process id when the connection was created.
Public Instance Methods
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
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
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
# 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
# File lib/mongo/server/connectable.rb, line 98 def ensure_same_process! if pid != Process.pid disconnect! @pid = Process.pid end end
# File lib/mongo/server/connectable.rb, line 105 def read ensure_connected do |socket| Protocol::Reply.deserialize(socket, max_message_size) end end
# File lib/mongo/server/connectable.rb, line 83 def ssl_options @ssl_options[:ssl] == true ? @ssl_options : {} end