douzhi9395 2019-05-02 03:20
浏览 23
已采纳

继续保存获取扫描文件错误:http:无效在封闭的正文上读取

When I read 1000 records file, I keep get error message every 10~20 records:

scan file error: http: invalid Read on closed Body

Here is my code

func parser(resp http.ResponseWriter, req *http.Request){

var count int

//....some of my code..

resp.Header().Set("Content-Type", "text/plain")
    scanner := bufio.NewScanner(req.Body)

    ctx := context.Background()
    for scanner.Scan() {
        itemID := scanner.Text()
        category := api.SearchAPI.FindCategory(itemID, lang, ctx)
        _, _ = fmt.Fprintf(resp, "%v,%v 
", itemID, category)
        count++
    }

    if err := scanner.Err(); err != nil {
        logger.Errorf("scan file error: %v", err)
        http.Error(resp, err.Error(), http.StatusBadRequest)
        return
    }

   //.....
}
  • 写回答

1条回答 默认 最新

  • dsshsta97935 2019-05-02 04:17
    关注

    Looks like your server is closing the connection. Check for any timeouts you have and why the request takes so long. You could process the scanner.Text() asynchronously so your scanning is not blocked for the searchAPI to respond, and the request body is not open for too long.

        resp.Header().Set("Content-Type", "text/plain")
        scanner := bufio.NewScanner(req.Body)
    
        ctx := context.Background()
        for scanner.Scan() {
            itemID := scanner.Text()
            go func(itemID string){
               category := api.SearchAPI.FindCategory(itemID, lang, ctx)
               _, _ = fmt.Fprintf(resp, "%v,%v 
    ", itemID, category)
               count++ //ENSURE YOU HAVE AN ATOMIC COUNTER INCREMENT, OR INCREMENT AFTER itemID IS READ
            }(itemID)
        }
    
        if err := scanner.Err(); err != nil {
            logger.Errorf("scan file error: %v", err)
            http.Error(resp, err.Error(), http.StatusBadRequest)
            return
        }
    
       //.....
    }
    

    Alternately you could simply collect all itemIDs in a slice, close the request body and then process them one by one.

    resp.Header().Set("Content-Type", "text/plain")
        scanner := bufio.NewScanner(req.Body)
    
        ctx := context.Background()
        itemIDs := make([]string, 0)
        for scanner.Scan() {
            itemID := scanner.Text()
            itemIDs = append(itemIDs, itemID)
        }
    
        if err := scanner.Err(); err != nil {
            logger.Errorf("scan file error: %v", err)
            http.Error(resp, err.Error(), http.StatusBadRequest)
            return
        }
    
        for _, itemID := range itemIDs {
            category := api.SearchAPI.FindCategory(itemID, lang, ctx)
            _, _ = fmt.Fprintf(resp, "%v,%v 
    ", itemID, category)
            count++
        }
    
       //.....
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效