class RedisCluster::Client

Client is a decorator object for Redis::Client. It add queue to support pipelining and another useful addition

Attributes

client[R]
middlewares[RW]
queue[R]
url[R]

Public Class Methods

new(opts) click to toggle source
# File lib/redis_cluster/client.rb, line 17
def initialize(opts)
  @client = Redis::Client.new(opts)
  @queue = []
  @url = "#{client.host}:#{client.port}"

  @healthy = true
  @ban_from = nil
end

Public Instance Methods

call(command) click to toggle source
# File lib/redis_cluster/client.rb, line 38
def call(command)
  push(command)
  commit.last
end
close() click to toggle source
# File lib/redis_cluster/client.rb, line 34
def close
  client.disconnect
end
commit() click to toggle source
# File lib/redis_cluster/client.rb, line 47
def commit
  return _commit unless middlewares

  middlewares.invoke(:commit, self) do
    _commit
  end
end
connected?() click to toggle source
# File lib/redis_cluster/client.rb, line 30
def connected?
  client.connected?
end
healthy() click to toggle source
# File lib/redis_cluster/client.rb, line 55
def healthy
  return true if @healthy

  # ban for 60 seconds for unhealthy state
  if Time.now - @ban_from > 60
    @healthy = true
    @ban_from = nil
  end

  @healthy
end
inspect() click to toggle source
# File lib/redis_cluster/client.rb, line 26
def inspect
  "#<RedisCluster client v#{RedisCluster::VERSION} for #{url}>"
end
push(command) click to toggle source
# File lib/redis_cluster/client.rb, line 43
def push(command)
  queue << command
end

Private Instance Methods

_commit() click to toggle source
# File lib/redis_cluster/client.rb, line 69
def _commit
  return nil if queue.empty?

  result = Array.new(queue.size)
  client.process(queue) do
    queue.size.times do |i|
      result[i] = client.read

      unhealthy! if result[i].is_a?(Redis::CommandError) && result[i].message['LOADING']
    end
  end

  result
ensure
  @queue = []
end
unhealthy!() click to toggle source
# File lib/redis_cluster/client.rb, line 86
def unhealthy!
  @healthy = false
  @ban_from = Time.now
  raise LoadingStateError
end