马提尼Go服务器内置网络爬虫在几个小时后打开了太多的文件。

huangapple go评论172阅读模式
英文:

Martini Go server with built in web crawler get too many open files after a few hourse

问题

我建立了一个网络爬虫,用于提供其发现的一些HTTP信息。该爬虫作为一个Go协程运行,而Martini则运行Web服务器。过了一段时间后,我开始收到以下错误信息:

2014/08/01 10:23:51 http: Accept error: accept tcp [::]:3000: too many open files; retrying in 1s.

我读到说我应该尝试增加最大打开文件数,但我对这个级别的配置还不熟悉,不知道该如何操作。我在Ubuntu 14.04上运行这个程序。请问如何更改Martini服务器的最大打开文件数?谢谢!

英文:

I built a web crawler that serves up some http info on its findings. The crawler runs as a go routine and martini runs the web server. After a while I start getting

2014/08/01 10:23:51 http: Accept error: accept tcp [::]:3000: too many open files; retrying in 1s.

I read I should try increasing the max open files only I am new to this level of configuration and have no idea how to do this. I am running this on Ubuntu 14.04. How do you change a martini servers max open files please and thank you.

答案1

得分: 4

确保你不要忘记关闭从http.Get获取的响应,就像这个问题中所示。

这个示例展示了更好的响应管理:

resp, _ := http.Get("http://127.0.0.1:3000" + path)
s, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()

如果问题仍然存在,你可以尝试在/etc/sysctl.conf中增加fs.file-max的值,具体方法请参考这里

英文:

Make sure you don't forget to close the response you get from an http.Get, as in this issue.

This example shows a better response management:

	resp, _ := http.Get("http://127.0.0.1:3000"+path)
	s, _ := ioutil.ReadAll(resp.Body)
	resp.Body.Close()

If the issue really still persists, then you can try and increase the fs.file-max in /etc/sysctl.conf.

答案2

得分: 0

我通过将以下代码移出函数来解决了这个问题:

var tr = &http.Transport{
    TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
var client = &http.Client{Transport: tr}
英文:

I solved this problem by moving the following code out side of the function:

var tr = &http.Transport{
    TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
var client = &http.Client{Transport: tr}

huangapple
  • 本文由 发表于 2014年8月1日 18:27:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/25078169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定