TCP百万并发之参数设置

要想服务器能够支持百万个client的连接,我们要知道,是什么会限制服务器连接的数量。这里我们需要搞清楚这几个问题;

  1. 操作系统能否支持百万连接?
  2. 操作系统维持百万连接需要多少内存?

1. 操作系统能否支持百万连接?

大多数的操作系统是不能达到百万级别的连接,是因为系统参数限制了这一操作,达到数十万以及数百万的连接的时候,会大量地消耗操作系统的资源。但是我们可以修改。

全局修改

我们使用cat /proc/sys/fs/file-nr,查看全局最大文件打开数Max open files.
在这里插入图片描述
我们可以看到第三个数字1048576,有100w,说明此操作系统可以达到100w的连接。如果有的操作系统没有达到这个数值,怎么办呢?
我们可以用 root 权限修改 /etc/sysctl.conf 文件:添加这两个参数

fs.file-max = 1048576
net.nf_conntrack_max = 1048576

fs.file-max:是打开文件fd最大值
net.nf_conntrack_max:表示网络netfilter可以容纳的最多文件个数。

使用:sudo sysctl -p /etc/sysctl.conf 使文件生效。

进程限制

在一个进程中,我们怎么修改参数使之暂时生效呢。
执行:

ulimit -a 

在这里插入图片描述
可以看到,这里有个open files参数,我们可以使用 -n来设置

ulimit -n 1048576

但是这个机器重启就会恢复到原来的数值。

我们怎么样可以永久修改呢?
修改 /etc/security/limits.conf 文件

在文件的最后一行添加:

 * soft nofile 1048576
 * hard nofile 1048576

这四个分别代表 domain, type , item, value 四个值。

#<domain> can be:
#        - a user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#        - NOTE: group and wildcard limits are not applied to root.
#          To apply a limit to the root user, <domain> must be
#          the literal username root.
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open files
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#        - chroot - change root to directory (Debian-specific)

注意: Linux 内核源码中有一个常量(NR_OPEN in /usr/include/linux/fs.h), 限制了最大打开文件数, 如 RHEL 5 是 1048576(2^20), 所以, 要想支持 C1000K, 你可能还需要重新编译内核,但是一般不需要 哈哈。

2. 操作系统维持百万连接需要多少内存?

由于操作系统维护这些连接需要占用大量的内存,所以我们也要给当前的内存,包括发送缓冲区和接收缓冲区设置大小。
同样在/etc/sysctl.conf 文件中,添加:

net.ipv4.tcp_mem = 262144 524288 786432
net.ipv4.tcp_wmem = 2048 2048 4096
net.ipv4.tcp_rmem = 2048 2048 4096

net.ipv4.tcp_mem :内核分配给TCP连接的内存,单位是Page,1 Page = 4096 Bytes,可用命令查看:

getconf PAGESIZE

第一个数字表示,当 tcp 使用的 page 少于 196608 时,kernel 不对其进行任何的干预
第二个数字表示,当 tcp 使用了超过 262144 的 pages 时,kernel 会进入 “memory pressure” 压力模式
第三个数字表示,当 tcp 使用的 pages 超过 393216 时(相当于1.6GB内存),就会报:Out of socket memory

net.ipv4.tcp_rmem 和 net.ipv4.tcp_wmem

为每个TCP连接分配的读、写缓冲区内存大小,单位是Byte
第一个数字表示,为TCP连接分配的最小内存
第二个数字表示,为TCP连接分配的缺省内存
第三个数字表示,为TCP连接分配的最大内存

net.ipv4.tcp_max_orphans
最大孤儿套接字(orphan sockets)数,单位是个
net.ipv4.tcp_max_orphans = 65536
表示最多65536个
注意:当cat /proc/net/sockstat看到的orphans数量达到net.ipv4.tcp_max_orphans的约一半时,就会报:Out of socket memory

使之生效:

sudo modprobe ip_conntrack

参考:
https://www.cnblogs.com/huanxiyun/articles/6594714.html
https://www.cnblogs.com/94cool/p/5631905.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值