Golang创建HTTP请求错误too many open files

Golang写了一个小程序抓信息然后发送一个HTTP请求到指定的服务器

http.Get("/someapi")

服务运行了大概三天,发现有多处错误日志打印,并且转发没有工作

xxx dial tcp x.x.x.x:8070: socket: too many open files

判断了半天,发现是因为http创建的请求没有Close,导致连接数量过多,不能继续创建新的socket连接,解决方法很简单,在所有的请求都要Close

res, err := http.Get("/someapi")
if err != nil {
  panic(err)
}

// 这句话很重要,就算你不需要响应数据,也需要总是关闭
defer res.Body.Close()
res.Close = true

重新编译上线,服务正常

分享

TITLE: Golang创建HTTP请求错误too many open files

LINK: https://www.qttc.net/309-golang-http-too-many-open-files.html

NOTE: 原创内容,转载请注明出自琼台博客