class Redis::Client

def ensure_connected

def ensure_connected
  disconnect if @pending_reads > 0
  attempts = 0
  begin
    attempts += 1
    if connected?
      unless inherit_socket? || Process.pid == @pid
        raise InheritedError,
              "Tried to use a connection from a child process without reconnecting. " \
              "You need to reconnect to Redis after forking " \
              "or set :inherit_socket to true."
      end
    else
      connect
    end
    yield
  rescue BaseConnectionError
    disconnect
    if attempts <= @options[:reconnect_attempts] && @reconnect
      sleep_t = [(@options[:reconnect_delay] * 2**(attempts - 1)),
                 @options[:reconnect_delay_max]].min
      Kernel.sleep(sleep_t)
      retry
    else
      raise
    end
  rescue Exception
    disconnect
    raise
  end
end