即使系統限制已增加,守護程序的開啟檔案限制仍已達到

即使系統限制已增加,守護程序的開啟檔案限制仍已達到

我正在運行 Debian 喘息。每個使用者的文件限制增加到 100000 個。 ulimit -aulimit -Hn/-Sn即使在螢幕上也顯示最大開啟檔案限制的正確數量。但由於某種原因,我無法擁有超過 4000 個連接/開啟檔案。

sysctl.conf

net.ipv4.tcp_fin_timeout = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 500 65000
net.core.somaxconn = 81920

輸出ulimit -a

core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 256639
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 999999
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) 256639
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

例如redis:

客戶:以 100 位客戶為基準

Writing to socket: Connection reset by peer
Writing to socket: Connection reset by peer
Writing to socket: Connection reset by peer
Writing to socket: Connection reset by peer
Error: Connection reset by peer

伺服器資訊:

127.0.0.1:6379> info clients
-Clients
connected_clients:4005
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

爪哇:

Caused by: io.netty.channel.ChannelException: Failed to open a socket.
Caused by: java.net.SocketException: Too many open files
        at sun.nio.ch.SelectorProviderImpl.openSocketChannel(Unknown Source)
java.io.IOException: Too many open files
Caused by: io.netty.channel.ChannelException: Failed to open a socket.
Caused by: java.net.SocketException: Too many open files
        at sun.nio.ch.SelectorProviderImpl.openSocketChannel(Unknown Source)
Caused by: io.netty.channel.ChannelException: Failed to open a socket.
Caused by: java.net.SocketException: Too many open files

ls -l /proc/[id]/fd | wc -l顯示約 4000 個描述符

答案1

有兩種設定可以限制開啟檔案的數量:每個進程的限制和系統範圍的限制。系統範圍的限制由 sysctl 設置fs.file-max,可以在/etc/sysctl.conf(啟動時讀取)或使用sysctl命令或透過寫入/proc/sys/fs/file-max.每個行程的限制由 設定ulimit -n

每個進程的限制由每個進程從其父進程繼承。可以在中設定預設值/etc/security/limits.conf,但這僅適用於互動式會話,不適用於在引導時啟動的守護程式。只有當守護程式透過互動式會話啟動時,它才適用於守護程式。

要增加(或減少)守護程序的每個進程限制,通常需要編輯其啟動腳本並ulimit在守護程序啟動之前添加一個呼叫。 Debian redis 軟體包在單獨的檔案中提供了配置設定:/etc/default/redis.註解掉該ULIMIT=行並根據需要增加該值。

相關內容