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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?