解决端口耗尽问题: tcp_tw_reuse、tcp_timestamps
一、本地端口有哪些可用
首先,需要了解到TCP协议中确定一条TCP连接有4要素:local IP, local PORT, remote IP, remote PORT。这个四元组应该是唯一的。
在我们发送HTTP请求的时候,local IP remote IP remote PORT是固定的,只有local PORT是可变的,可用的local PORT的数量就限制了client和server之间TCP连接数的数量。
TCP协议中PORT部分是用两个字节来表示的,也就是说可用的端口数量肯定不能超过65536个。
sysctl -a|grep net.ipv4.ip_local_port_range net.ipv4.ip_local_port_range =
表示client可用的端口是[32768, 61000],共28233个。那么这台机器和另外任意一台机器,同时只能建立28233个TCP连接。
可打开的最大文件数
ulimit -a
core file size (blocks, -c)
data seg size (kbytes, -d) unlimited
scheduling priority (-e)
file size (blocks, -f) unlimited
pending signals (-i)
max locked memory (kbytes, -l)
max memory size (kbytes, -m) unlimited
open files (-n)
pipe size ( bytes, -p)
POSIX message queues (bytes, -q)
real-time priority (-r)
stack size (kbytes, -s)
cpu time (seconds, -t) unlimited
max user processes (-u)
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
/etc/security/limits.conf
#Where:
#<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
#
#<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 file descriptors
# - 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: [-, ]
# - rtprio - max realtime priority
#
#<domain> <type> <item> <value>
# #* soft core
#* hard rss
#@student hard nproc
#@faculty soft nproc
#@faculty hard nproc
#ftp hard nproc
#@student - maxlogins # End of file
root soft nofile
root hard nofile
* soft nofile
* hard nofile
二、短连接并不会同时存在大量TCP连接,端口为什么还是耗尽了?
上一步我们分析到,client和server之间只能同时存在28233个TCP连接,但是我们的压测用的是短连接,连接用完就释放掉了,端口应该也会释放掉,为啥还会产生端口耗尽的问题呢?
这就需要提到TIME_WAIT这个状态了,TCP连接断开的时候,主动发起连接断开操作的一方,最后会停留在TIME_WAIT状态,会持续2*MSL的时长,
这个状态的端口是不能被使用的,准确的说是当新的TCP连接的local IP remote IP和remote PORT和TIME_WAIT状态的连接一致时这个端口不能被使用。
sysctl -a|grep net.ipv4.tcp_fin_timeout net.ipv4.tcp_fin_timeout =
可以推论:
如果client机器有28233端口可用,TIME_WAIT 60秒,短连接的方式发起请求,那么这个client发起的请求的QPS是不能超过28233/60的。
三、为什么有TIME_WAIT状态
想象这么一个场景:
- A和B建立了一个TCP连接,A向B发送消息包1 2 3,消息包3传给B的时候延迟了,A又重传了消息包3,A和B完成通信断开连接,双方都很happy。
2. A和B又建立一个TCP连接,用了相同的local IPlocal PORTremote IPremote PORT,A向B发送消息包1 2,B收到1 2之后
上一个连接延迟的消息包3来了,B无法区分这个消息包是上一个TCP连接的
TIME_WAIT状态主要就是为了解决这个问题:防止延迟的无效消息包被误认为是合法的。
TIME_WAIT状态持续2倍MSL之后,可以确保老的TCP连接逗留在网络上的消息包已经全部消失了。
四、单台机器发送请求每秒要超过(可用端口数/tcp_fin_timeout)怎么办
使用长连接或者打开tcp_tw_reuse。
那么为什么打开tcp_tw_reuse就可以使用TIME_WAIT状态的端口呢?不怕延迟的消息包被误认为合法的么?
这里就需要提到另外一个选项了:tcp_timestamps,打开这个选项之后,TCP包里面会带上发包机器的当前时间戳。
可以这么想象一下:如果每个TCP包带上原始发包时的时间戳,如果一个延迟的消息包到达B,B可以拿这个消息包的时间和TCP连接建立的时间做一个对比,
如果消息包的时间比较早,那么这就是上一个TCP连接延迟的消息包,丢弃掉就好。
通过时间戳的比较,也可以解决延迟消息包被误认为合法的问题。因此同时打开tcp_timestamps和tcp_tw_reuse之后,client端的TIME_WAIT端口是可以被复用的。
但要注意的是:
1. tcp_timestamps需要client和server端同时打开才生效(我们这次遇到端口耗尽的问题就是因为server端没有打开tcp_timestamps选项)
2. tcp_timestamps精确到秒,也就是TIME_WAIT状态的端口也在下一秒才可以重用,如果继续使用短连接的话,发送请求的QPS是不能超过可用端口数的
参考:
解决端口耗尽问题: tcp_tw_reuse、tcp_timestamps的更多相关文章
- 避免 TCP/IP 端口耗尽
转载:http://www.cnblogs.com/tianzhiliang/archive/2011/06/27/2091214.html 当客户端启动到服务器的 TCP/IP 套接字连接时,客户端 ...
- 系统 TIME_WAIT累积与端口耗尽的问题
调整内核参数 net.ipv4.tcp_tw_reuse = net.ipv4.tcp_tw_recycle = 这两个参数可以让 tcp 连接回收.再利用. 摘录 『HTTP 权威指南』page ...
- Windows解决端口占用问题
Windows解决端口占用问题 步骤 1. win + R,输入cmd回车进入dos界面 2. 输入netstat -ano|findstr 8080 查看占用8080端口的进程 3. 输入taskk ...
- 【Azure 应用服务】App Service/Azure Function的出站连接过多而引起了SNAT端口耗尽,导致一些新的请求出现超时错误(Timeout)
问题描述 当需要在应用中有大量的出站连接时候,就会涉及到SNAT(源地址网络转换)耗尽的问题.而通过Azure App Service/Function的默认监控指标图表中,却没有可以直接查看到SNA ...
- tomcat 解决端口8080冲突
这样的问题有时会因为eclipse等IDE使用bug导致. 解决方法: 使用dos 命令 运行---cmd--netstat -ano|findstr 8080 键入命令后,dos下会显示正在使用80 ...
- sping获取bean方法 解决资源耗尽
// ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"applicationCon ...
- windows和linux下杀死Tomcat进程,解决端口占用
windows和linux下解决Tomcat进程 windows下启动Tomcat报错,8080端口号被占用,报错信息如下 两种解决方法,一种是关闭了这个端口号,另外一种是修改Tomcat下的serv ...
- Windows解决端口被占用问题
第一种解决方法,以8080端口为例 打开命令行输入 cmd ,输入netstat -ano 会显示所有已经在运行的端口情况.PID为进程id 输入你想要查的正在占用的端口号,netstat -ano ...
- 解决端口被占用问题(端口timewait)
当jmeter做千级并发时,有报错的接口,查看是不是本地端口被占用完了 netstat -an 查看是否有端口在 timewait timewait是知道用那个端口,但是端口被别人占用着 见tcp ...
随机推荐
- Atitti 互联网时代三大竞争战略 ——平台化战略 锚”战略、价值领先战略
Atitti 互联网时代三大竞争战略 ——平台化战略 锚”战略.价值领先战略 美国著名管理学家迈克尔•波特在<竞争战略>一书中提出了集中化战略和差异化战略.成本领先战略三种基本竞争战略,从 ...
- 华为AR配置内部服务器示例(只有1个公网IP)
AR配置公网和私网用户都可以通过公网地址访问内部服务器示例(只有1个公网IP) 适用于:V200R003C01及以后的系统软件版本. 组网需求: 由于只有1个公网IP(100.100.1.2),想实现 ...
- Syncfusion SfDataGrid 导出Excel
var options = new ExcelExportingOptions { ExcelVersion = ExcelVersion.Excel2013, }; //不需要导出的字段 optio ...
- [转]剑指offer之Java源代码
一.引言 <剑指offer>可谓是程序猿面试的神书了,在面试中帮了我很多,大部分面试的算法题都会遇到原题或者是类似的题.但是书上的代码都是C版的,我在这里整理了一份Java版的代码供大家学 ...
- mybatis中_parameter使用和常用sql
mybatis中_parameter使用和常用sql mybatis中_parameter使用和常用sql 在用自动生成工具生成的mybatis代码中,总是能看到这样的情况,如下: <sel ...
- [Z] 从Uncaught SyntaxError: Unexpected token ")" 问题看javascript:void的作用
https://blog.csdn.net/hongweigg/article/details/78094338 问题 在前端编程中,突然出现Uncaught SyntaxError: Unex ...
- eclipse如何安裝JPA 和Data Source Explorer
安裝Data Source Explorer https://blog.csdn.net/XIAOZHI0999/article/details/61199801?utm_source=blogxgw ...
- 内建模块collections的使用
# -*-coding:utf-8 -*- from collections import namedtuple Point=namedtuple('Point',['x','y']) p=Point ...
- 获取mssqlserver数据库表的字段名称,字段说明,数据类型,主键等表的信息
sql脚本: SELECT TableName then d.name else '' end,---表名 TableShowsThat then isnull(f.value,'') else '' ...
- SublimeText插件Pandoc导出PDF中文报错或者中文不显示解决方法
按照如下自定义配置(主要是指定latex的字体[仅限本机字体]) { "user": { // "pandoc-path":"C:\\Program ...