class Redis::Client

def ensure_connected

def ensure_connected
  tries = 0
  begin
    connect unless connected?
    tries += 1
    yield
  rescue Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EBADF, Errno::EINVAL
    disconnect
    if tries < 2 && @reconnect
      retry
    else
      raise Errno::ECONNRESET
    end
  rescue Exception
    disconnect
    raise
  end
end