tomcat连接数相关的配置
以下是tomcat7的一些配置说明
tomcat交互图
maxConnections
tomcat接受的最大连接的个数,超过这个连接个数,acceptor就会阻塞。
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 BIO the default is the value of maxThreads unless an Executor is used in which case the default will be the value of maxThreads from the executor. For NIO the default is 10000. For APR/native, the default is 8192.
需要注意的是,在BIO模式下,maxConnections
的值默认等于maxThreads
的值!!!
达到maxConnections之后,acceptor线程就会阻塞,用jstack查看堆栈会发现Acceptor线程阻塞在下面的代码
1 | sudo -u tomcat jstack `pgrep -f 'tomcat'` | less |
tomcat 7的源码中相应的代码
1 | //if we have reached max connections, wait |
函数的具体实现
1 | protected void countUpOrAwaitConnection() throws InterruptedException { |
其中LimitLatch
是tomcat自己实现的一个类似CountDownLatch
的东西。
1 | /** |
它的初始化过程:
1 | protected LimitLatch initializeConnectionLatch() { |
maxThreads
tomcat的连接线程最大个数。
The maximum number of request processing threads to be created by this Connector, which therefore determines the maximum number of simultaneous requests that can be handled. If not specified, this attribute is set to 200. If an executor is associated with this connector, this attribute is ignored as the connector will execute tasks using the executor rather than an internal thread pool. Note that if an executor is configured any value set for this attribute will be recorded correctly but it will be reported (e.g. via JMX) as -1 to make clear that it is not used.
maxThreads、minSpareThreads是tomcat工作线程池的配置参数,maxThreads就相当于jdk线程池的maxPoolSize,而minSpareThreads就相当于jdk线程池的corePoolSize。
相应的代码如下:
1 | public void createExecutor() { |
acceptCount
系统积压队列的大小。
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.
tomcat7的源码中有这么一段,大概就是别名的替换。acceptCount
被替换成了backlog
,backlog
的意思是积压的东西。
1 | static { |
acceptCount
是在初始bind
的时候传给jdk的bind
函数的,最终会传递到系统层。
以NioEndpoint
为例,大概如下:
1 | /** |
看下getBackLog
的实现(AbstractEndpoint
):
1 | /** |
默认值大小是100
。
总结
tomcat的Acceptor
线程会不停的从系统的全连接队列里去拿对应的socket连接,直到达到了maxConnections
的值。
之后Acceptor
会阻塞在那里,直到处理的连接小于maxConnections
的值。如果一直阻塞的话,就会在系统的tcp
连接队列中阻塞,这个队列的长度是acceptCount
控制的,默认是100
。如果仍然处理不过来,系统可能就会丢掉
一些建立的连接了。
所以,大致可以估计下最多能处理的连接数:
最大处理连接数 = acceptCount + maxConnection
#参考