Sha256: 10444cbd0190c79c5650a545ef460ae38c193e4fade4ba148994a6a1b4ee764b

Contents?: true

Size: 952 Bytes

Versions: 1

Compression:

Stored size: 952 Bytes

Contents

require "redic/connection"
require "uri"

class Redic
  class Client
    def initialize(url)
      @uri = URI.parse(url)
      @connection = nil
      @semaphore = Mutex.new
    end

    def read
      @connection.read
    end

    def write(command)
      @connection.write(command)
    end

    def connect
      establish_connection unless connected?

      @semaphore.synchronize do
        yield
      end
    rescue Errno::ECONNRESET
      @connection = nil
      retry
    end

  private
    def establish_connection
      begin
        @connection = Redic::Connection.new(@uri)
      rescue StandardError => err
        raise err, "Can't connect to: %s" % @uri
      end

      authenticate
    end

    def authenticate
      if @uri.password
        @semaphore.synchronize do
          write [:auth, @uri.password]
          read
        end
      end
    end

    def connected?
      @connection && @connection.connected?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redic-0.0.8 lib/redic/client.rb