class RedisClient

def connect

def connect
  @pid = PIDCache.pid
  if @raw_connection
    @middlewares.connect(config) do
      @raw_connection.reconnect
    end
  else
    @raw_connection = @middlewares.connect(config) do
      config.driver.new(
        config,
        connect_timeout: connect_timeout,
        read_timeout: read_timeout,
        write_timeout: write_timeout,
      )
    end
  end
  prelude = config.connection_prelude.dup
  if id
    prelude << ["CLIENT", "SETNAME", id]
  end
  # The connection prelude is deliberately not sent to Middlewares
  if config.sentinel?
    prelude << ["ROLE"]
    role, = @middlewares.call_pipelined(prelude, config) do
      @raw_connection.call_pipelined(prelude, nil).last
    end
    config.check_role!(role)
  else
    unless prelude.empty?
      @middlewares.call_pipelined(prelude, config) do
        @raw_connection.call_pipelined(prelude, nil)
      end
    end
  end
rescue FailoverError, CannotConnectError
  raise
rescue ConnectionError => error
  raise CannotConnectError, error.message, error.backtrace
rescue CommandError => error
  if error.message.match?(/ERR unknown command ['`]HELLO['`]/)
    raise UnsupportedServer,
      "redis-client requires Redis 6+ with HELLO command available (#{config.server_url})"
  else
    raise
  end
end