lib/moped/connection.rb in moped-1.5.3 vs lib/moped/connection.rb in moped-2.0.0.beta
- old
+ new
@@ -1,19 +1,36 @@
-require "timeout"
-require "moped/sockets/connectable"
-require "moped/sockets/tcp"
-require "moped/sockets/ssl"
+# encoding: utf-8
+require "moped/connection/manager"
+require "moped/connection/pool"
+require "moped/connection/queue"
+require "moped/connection/reaper"
+require "moped/connection/sockets"
module Moped
# This class contains behaviour of database socket connections.
#
- # @api private
+ # @since 2.0.0
class Connection
- attr_reader :host, :port, :timeout, :options
+ # The default connection timeout, in seconds.
+ #
+ # @since 2.0.0
+ TIMEOUT = 5
+ # @!attribute host
+ # @return [ String ] The ip address of the host.
+ # @!attribute options
+ # @return [ Hash ] The connection options.
+ # @!attribute port
+ # @return [ String ] The port the connection connects on.
+ # @!attribute timeout
+ # @return [ Integer ] The timeout in seconds.
+ # @!attribute last_use
+ # @return [ Time ] The time the connection was last checked out.
+ attr_reader :host, :options, :port, :timeout, :last_use
+
# Is the connection alive?
#
# @example Is the connection alive?
# connection.alive?
#
@@ -32,13 +49,13 @@
# @return [ TCPSocket ] The socket.
#
# @since 1.0.0
def connect
@sock = if !!options[:ssl]
- Sockets::SSL.connect(host, port, timeout)
+ Socket::SSL.connect(host, port, timeout)
else
- Sockets::TCP.connect(host, port, timeout)
+ Socket::TCP.connect(host, port, timeout)
end
end
# Is the connection connected?
#
@@ -78,15 +95,55 @@
# @param [ Hash ] options Options for the connection.
#
# @option options [ Boolean ] :ssl Connect using SSL
# @since 1.0.0
def initialize(host, port, timeout, options = {})
+ @host = host
+ @port = port
+ @timeout = timeout
+ @options = options
@sock = nil
+ @last_use = nil
@request_id = 0
- @host, @port, @timeout, @options = host, port, timeout, options
end
+ # Expiring a connection means returning it to the connection pool.
+ #
+ # @example Expire the connection.
+ # connection.expire
+ #
+ # @return [ nil ] nil.
+ #
+ # @since 2.0.0
+ def expire
+ @last_use = nil
+ end
+
+ # An expired connection is not currently being used.
+ #
+ # @example Is the connection expired?
+ # connection.expired?
+ #
+ # @return [ true, false ] If the connection is expired.
+ #
+ # @since 2.0.0
+ def expired?
+ @last_use.nil?
+ end
+
+ # A leased connection is currently checkout out from the connection pool.
+ #
+ # @example Lease the connection.
+ # connection.lease
+ #
+ # @return [ Time ] The current time of leasing.
+ #
+ # @since 2.0.0
+ def lease
+ @last_use = Time.now
+ end
+
# Read from the connection.
#
# @example Read from the connection.
# connection.read
#
@@ -111,11 +168,11 @@
reply.documents = []
else
sock_read = read_data(socket, reply.length - 36)
buffer = StringIO.new(sock_read)
reply.documents = reply.count.times.map do
- BSON::Document.deserialize(buffer)
+ BSON::Document.from_bson(buffer)
end
end
reply
end
end
@@ -186,9 +243,11 @@
data
end
# Yields a connected socket to the calling back. It will attempt to reconnect
# the socket if it is not connected.
+ #
+ # @api private
#
# @example Write to the connection.
# with_connection do |socket|
# socket.write(buf)
# end