Linux go 程序 http: Accept error: accept tcp accept4: too many open files 问题解决

977 记录 发表评论

这个问题主要是因为 Linux 系统限制了每个程序可以打开的文件数量,而我们的程序需要打开多于限定值的文件(包括网络套接字文件等,递归的时候会打开很多)。

用这个命令可查看当前系统打开文件的限制:

~ ulimit -a

core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 6992
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 6992
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

其中 open files1024 并不大,所以我们要调大一些。

打开配置文件:

vim /etc/security/limits.conf

增加下面的 2 行:

*       soft    nofile  65536
*       hard    nofile  65536

最后重启程序,就可以了。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

昵称 *