dongyi1490 2014-09-17 23:29
浏览 59
已采纳

是ioutil.ReadAll阻止我的服务器?

I'm trying to write a server in Go, using the net/http package. I only have one route, and it's pretty simple. It downloads a file from S3 and returns it to the client:

response, err := http.Get("some S3 url")
if err != nil {
    return
}
body, err := ioutil.ReadAll(response.Body)
w.Write(body)

Downloading the url myself takes about 0.25 seconds. So I start this server and send it 250 requests/sec. Initially I get responses back within 0.25 seconds. But that number keeps going up until it starts taking 45 seconds for a response. I'm running this on a 40 core machine, with GOMAXPROCS=40. I started wondering if somehow the downloads aren't happening in parallel.

But if I comment out this line:

body, err := ioutil.ReadAll(response.Body)

And just return some garbage data of equal length, suddenly my server consistently responds in 0.25 seconds. Why is it faster after removing the ReadAll?

  • 写回答

1条回答 默认 最新

  • duanjiwei1283 2014-09-17 23:52
    关注

    Few things comes to mind:

    1. You're not closing response.Body and the server is running out of FDs.
    2. The garbage collector is being slow and you're running out of memory for reading so many files with ReadAll.
    3. You're choking the networking because of #1.

    Try something like this and see if it helps:

    response, err := http.Get("some S3 url")
    if err != nil {
        return
    }
    defer response.Body.Close()
    _, err := io.Copy(w, response.Body)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决