Sha256: f824b7b3f8b0285f5f57252ab99f4094c5d5e30853ef697755999493709e52d3

Contents?: true

Size: 1.69 KB

Versions: 1

Compression:

Stored size: 1.69 KB

Contents

require_relative "connection"
require "uri"

class Redic
  class Client
    EMPTY = "".freeze
    SLASH = "/".freeze

    attr_accessor :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
    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 = 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
        raise err, "Can't connect to: %s" % @uri
      end

      if @uri.scheme != "unix"
        if @uri.password
          assert_ok(call("AUTH", @uri.password))
        end

        if @uri.path != EMPTY && @uri.path != SLASH
          assert_ok(call("SELECT", @uri.path[1..-1]))
        end
      end
    end

    def call(*args)
      @semaphore.synchronize do
        write(args)
        read
      end
    end

    def assert(value, error)
      raise error unless value
    end

    def assert_ok(reply)
      assert(reply == "OK", reply)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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