Sha256: 2e417dc3fadc6903cc499bdb2ffe6eed335cc6db0df99115bad939f9e10a7bea

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 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
      @semaphore.synchronize do
        establish_connection unless connected?
        yield
      rescue Errno::ECONNRESET
        @connection = false
        retry
      end
    end

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

    def disconnect!
      if connected?
        @connection.disconnect
        @connection = false
      end
    end

    def quit
      if connected?
        assert_ok(@semaphore.synchronize { 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)
      write(args)
      read
    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.2 lib/redic/client.rb