tomcat能创建tcp连接的确切个数

tomcat能创建多少个tcp连接,这由三个因素控制:

NO.参数默认值说明
1

OS参数:/proc/sys/net/core/somaxconn

4096

ubuntu 18.04

:~$ uname -a
Linux vm208 5.4.0-48-generic #52~18.04.1-Ubuntu SMP Thu Sep 10 12:50:22 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

 

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.

如上面表格所说

  1. tomcat可以创建 maxConnections+1,达到该数值后,tomcat不再接受新的连接
  2. 但,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;
    }
somaxconn4096
acceptCount3
maxConnections6

按着上面的推理:maxConnections + 1 + min(acceptCount, /proc/sys/net/core/somaxconn) == 6 + 1 + min(3, 4096) == 10

 

然后,我们看下OS中的实际数据

确实是10个,但,数据有些不同

  1. acceptCount==3,但,OS持有了 4个(后面没有java进程号,即黄色框框内的)
  2. 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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值