levigross / grequests

A Go "clone" of the great and famous Requests library
Apache License 2.0
2.13k stars 137 forks source link

`too many open files` error #59

Closed OwenLIULIU closed 6 years ago

OwenLIULIU commented 6 years ago
resp, err := grequests.Post(url, options)
if err != nil || resp.StatusCode != 200 {
    ...
    return
}

above is my code, and after about 2 weeks, I could get an error as socket: too many open files. I don't know whether it due to the response unclosed. resp only used to get StatusCode, no resp.JSON or any other usages.
Should i must close the connection? like defer resp.Close(). Or what should I do to avoid this socket error?

zwczou commented 6 years ago
resp, err := grequests.Post(url, options)
if err != nil {
    //
}
if resp.StatusCode != 200 {
    // resp.RawResponse.Body.Close()  or  resp.String()
}
levigross commented 6 years ago

You need to close the resp if you use it.

OwenLIULIU commented 6 years ago

@levigross In my case, I don't care about the content of resp, what I want is just check error or not 200, in other word, the request fails. Then do I still have to close the resp manually either?

levigross commented 6 years ago

Yes