tomcat能创建多少个tcp连接,这由三个因素控制:
NO. | 参数 | 默认值 | 说明 |
1 | OS参数:/proc/sys/net/core/somaxconn | 4096 | ubuntu 18.04 :~$ uname -a
|
2 | tomcat参数:acceptCount https://tomcat.apache.org/tomcat-8.5-doc/config/http.html#Standard_Implementation | 100 | The maximum queue length for incoming connection requests when all possible request processing threads are in use. Any requests received when the queue is full will be refused. The default value is 100.
|
3 | tomcat参数:maxConnections https://tomcat.apache.org/tomcat-8.5-doc/config/http.html#Standard_Implementation | 10000 | The maximum number of connections that the server will accept and process at any given time. When this number has been reached, the server will accept, but not process, one further connection. This additional connection be blocked until the number of connections being processed falls below maxConnections at which point the server will start accepting and processing new connections again. Note that once the limit has been reached, the operating system may still accept connections based on the acceptCount setting.
The default value varies by connector type. For NIO and NIO2 the default is 10000. For APR/native, the default is 8192.
For NIO/NIO2 only, setting the value to -1, will disable the maxConnections feature and connections will not be counted. |
如上面表格所说
- tomcat可以创建 maxConnections+1,达到该数值后,tomcat不再接受新的连接
- 但,OS还会继续接受min(acceptCount, /proc/sys/net/core/somaxconn)个连接
所以,最终是:maxConnections + 1 + min(acceptCount, /proc/sys/net/core/somaxconn)
我们来试试:
使用Jmeter发送100个请求:http://192.168.86.204:8080/myboot224/greeting?cost=3600
@RequestMapping("/greeting")
public @ResponseBody
String greeting(HttpServletResponse rsp, @RequestParam(name = "cost") long cost) {
try {
Thread.sleep(cost * 1000L);
} catch (Exception e) {
e.printStackTrace();
}
String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
LOG.info(format);
return format;
}
somaxconn | 4096 |
acceptCount | 3 |
maxConnections | 6 |
按着上面的推理:maxConnections + 1 + min(acceptCount, /proc/sys/net/core/somaxconn) == 6 + 1 + min(3, 4096) == 10
然后,我们看下OS中的实际数据
确实是10个,但,数据有些不同
- acceptCount==3,但,OS持有了 4个(后面没有java进程号,即黄色框框内的)
- maxConnections==6,tomcat持有了6个(并没有one further more connection)
也就是说,tomcat能创建的tcp连接的确切个数是:
maxConnections + ( min(acceptCount, /proc/sys/net/core/somaxconn) + 1 )
所以,这就有两个问题:
1. maxConnections为什么没有one further connection呢?
在这个里面:org.apache.tomcat.util.net.Acceptor#run。后面在扒拉吧
2. OS为什么多持有了一个呢?
https://segmentfault.com/a/1190000019252960
完