转到套接字:打开的文件过多

When I send requests from the following code:

req, err := http.NewRequest("GET", "my_target:", nil)
if err != nil {
    panic(err)
}
req.Close = true
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
    panic(err)
}
defer resp.Body.Close()

After a few hours of sending average 10 requests per minutes, I get this error:

socket: too many open files

How do I find the cause of this problem?

Am I not closing the http.Request?

I thought req.Close = true does that job.

Thanks!

Why are you deferring the close? Are you actually reading from this body?

defer resp.Body.Close()

Do you actually return from the current function before performing another Get? If not, then the defer will never execute, and you'll never release this connection for reuse.

req.Close = true is an unusual choice here, as well. This also prevents connection reuse, which is something you'd probably want rather than forbid. This doesn't automatically close the request on your side. It forces the server to immediately close the connection, which you would otherwise reuse. You'll hold your side open until you close it.

Typically for a simple GET request like you have here, I would just do this:

resp, err := http.Get("...")
if err != nil {
    panic(err) // panic seems harsh, usually you'd just return the error. But either way...
}
resp.Body.Close()

There's no need for a special client here. Just use the default one. It'll take care of the rest as long as you make sure to close the response body. And there's no need to complicate things with a defer if you're going to immediately close the body. The reason for defer is to make sure you close the body if you have a bunch of processing later that might return or panic on error.