去,tcp太多打开的文件调试 [英] Go, tcp too many open files debug

查看:27
本文介绍了去,tcp太多打开的文件调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个简单的 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:获取http://127.0.0.1:33202:拨打tcp 127.0.0.1:33202:打开的文件太多

其他帖子说要确保关闭连接,我在这里做这一切.还有人说用ulimit增加最大连接限制,或者试试sudo sysctl -w fs.inotify.max_user_watches=100000但还是不行.

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 连接 goroutine?它仅在 2,000 个连接时崩溃.

How do I run millions of tcp connection goroutines in a single server? It crashes only with 2,000 connections.

谢谢,

推荐答案

我认为您需要更改最大文件描述符.我之前在我的一个开发虚拟机上遇到过同样的问题,需要更改文件描述符最大值,而不是使用 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

但是在我跑完之后

·> ulimit -n 500
·> ulimit -n
500

我明白了:

panic: Get http://127.0.0.1:51227: dial tcp 127.0.0.1:51227: socket: too many open files

** 不要落入 Praveen 做的陷阱 **

** Don't fall into the trap that Praveen did **

注意 ulimit != ulimit -n.

➜  cmd git:(wip-poop) ✗ ulimit -a
-t: cpu time (seconds)              unlimited
-f: file size (blocks)              unlimited
-d: data seg size (kbytes)          unlimited
-s: stack size (kbytes)             8192
-c: core file size (blocks)         0
-v: address space (kbytes)          unlimited
-l: locked-in-memory size (kbytes)  unlimited
-u: processes                       1418
-n: file descriptors                4864

这篇关于去,tcp太多打开的文件调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆