lib/redic/client.rb in redic-1.3.0 vs lib/redic/client.rb in redic-1.4.0

- old
+ new

@@ -5,17 +5,20 @@ class Client attr :timeout def initialize(url, timeout) @semaphore = Mutex.new + @connection = false + configure(url, timeout) end def configure(url, timeout) + disconnect! + @uri = URI.parse(url) @timeout = timeout - @connection = nil end def read @connection.read end @@ -29,14 +32,36 @@ @semaphore.synchronize do yield end rescue Errno::ECONNRESET - @connection = nil + @connection = false retry end + def connected? + @connection && @connection.connected? + end + + def disconnect! + if connected? + @connection.disconnect + @connection = false + end + end + + def quit + if connected? + assert_ok(call("QUIT")) + disconnect! + + true + else + false + end + end + private def establish_connection begin @connection = Redic::Connection.new(@uri, @timeout) rescue StandardError => err @@ -52,13 +77,9 @@ def call(*args) @semaphore.synchronize do write(args) read end - end - - def connected? - @connection && @connection.connected? end def assert(value, error) raise error unless value end