Sha256: d1488ef0cd8ea4a8f6fc9519c147541c236d83aeabe8d26191302706f0494d70

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require_relative "connection"
require "uri"

class Redic
  class Client
    attr :timeout

    def initialize(url, timeout)
      @uri = URI.parse(url)
      @timeout = timeout
      @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, @timeout)
      rescue StandardError => err
        raise err, "Can't connect to: %s" % @uri
      end

      authenticate
      select
    end

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

    def select
      if @uri.path
        @semaphore.synchronize do
          write ["SELECT", @uri.path[1..-1]]
          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-1.0.0 lib/redic/client.rb