去吧,tcp太多打开文件调试 [英] Go, tcp too many open files debug
问题描述
下面是一个简单的Go http(tcp)连接测试脚本
Here's a straightforward Go http (tcp) connection test script
func main() {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, client")
}))
defer ts.Close()
var wg sync.WaitGroup
for i := 0; i < 2000; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
resp, err := http.Get(ts.URL)
if err != nil {
panic(err)
}
greeting, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
panic(err)
}
fmt.Printf("%s", i, greeting)
}(i)
}
wg.Wait()
}
如果我在Ubuntu中运行这个命令我得到:
And If I run this in Ubuntu I get:
panic:Get http://127.0.0.1:33202:dial tcp 127.0.0.1:33202:too许多打开的文件
其他职位说确保关闭
连接,我我在这里做这一切。
和其他人说增加与 ulimit
的最大连接限制或尝试 sudo sysctl -w fs.inotify.max_user_watches = 100000 code>但仍然无效。
Other posts say to make sure Close
the connection, which I am doing it all here.
And others say to increase the limit of maximum connection with ulimit
or try sudo sysctl -w fs.inotify.max_user_watches=100000
but still does not work.
如何在单个服务器上运行数百万个tcp连接程序?
只有2000个连接才会崩溃。
How do I run millions of tcp connection goroutines in a single server? It crashes only with 2,000 connections.
感谢,
推荐答案
我认为你需要改变你的最大文件描述符。我之前在其中一个开发虚拟机上遇到了同样的问题,需要更改文件描述符max,而不是inotify设置。
I think you need to change your max file descriptors. I have run into the same problem on one of my development VMs before and needed to change the file descriptors max, not anything with inotify settings.
FWIW,您的程序在我的虚拟机上运行良好。
FWIW, your program runs fine on my VM.
·> ulimit -n
120000
但是在运行
But after I run
·> ulimit -n 500
·> ulimit -n
500
我得到:
I get:
panic: Get http://127.0.0.1:51227: dial tcp 127.0.0.1:51227: socket: too many open files
这篇关于去吧,tcp太多打开文件调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!