tomcat根据繁忙线程数对keepalive进行动态调整

97 篇文章 3 订阅
46 篇文章 0 订阅

众所周知,Keep-Alive功能使客户端到服务器端的连接持续有效,当出现对服务器的后继请求时,Keep-Alive功能避免了建立或者重新建立连接。我们经常所用的tomcat服务器就支持HTTP Keep-Alive。在http1.1中,keepalive默认是开启的。如果需要自定义配置keepalive参数,我们可以在tomcat的server.xml中做如下配置:

<Connector port="8080" protocol="HTTP/1.1"
              maxThreads="600"
              minSpareThreads="100"
              acceptCount="700"

                                        maxConnections="300" //与tomcat建立的最大socket连接数
              connectionTimeout="20000"

                                        maxKeppAliveRequests="100"  //请求个数超过这个数,强制关闭掉socket链接

                                        keepAliveTimeOut="60000"  //下次请求过来之前,socket链接保持多久
              redirectPort="8443" 
              URIEncoding="utf-8"

  />

但是tomcat在实际处理请求的过程中会根据工作线程池中繁忙线程数动态的对keepalive进行开启或者关闭,tomcat源码如下:

在org.apache.coyote.http11.AbstractHttp11Processor这个类的process方法中可以看到

会通过disableKeepAlive这个函数判断,我们再来看看disableKeepAlive这个函数,如下:

通过代码会算出 繁忙线程数/最大线程数的比例,即threadRatio = (threadsBusy * 100) / maxThreads; 接着看getDisableKeepAlivePercentage这个函数

可以看出,如果繁忙线程数/最大线程数 >75%,那么就会主动将keepalive关掉。

 

实际上这种自动调节措施也是一种防护措施,当客户端并发量比较大的时候,如果一致保持长连接,实际上是很耗服务端资源的,会严重影响性能,所以这里tomcat会动态的根据繁忙线程数来决定是否使用keepalive。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 的 `keep-alive` 组件提供了动态缓存的功能,可以根据组件的属性值动态地缓存组件。 在 Vue2 中,我们可以使用 `include` 和 `exclude` 属性来控制缓存哪些组件和不缓存哪些组件。但是这种方式无法根据组件的属性值进行动态缓存。 在 Vue3 中,我们可以使用 `v-bind` 指令来动态绑定 `keep-alive` 组件的 `include` 和 `exclude` 属性,实现动态缓存的功能。例如: ```html <template> <div> <button @click="toggle">Toggle</button> <keep-alive :include="includeList" :exclude="excludeList"> <router-view /> </keep-alive> </div> </template> <script> export default { data() { return { includeList: [], excludeList: [], show: false, }; }, methods: { toggle() { this.show = !this.show; if (this.show) { this.includeList.push("ComponentA"); this.excludeList = this.excludeList.filter((name) => name !== "ComponentA"); } else { this.excludeList.push("ComponentA"); this.includeList = this.includeList.filter((name) => name !== "ComponentA"); } }, }, }; </script> ``` 在上面的代码中,我们使用 `includeList` 和 `excludeList` 组来动态控制缓存哪些组件和不缓存哪些组件。在 `toggle` 方法中,根据 `show` 属性的值来决定是否缓存 `ComponentA` 组件。如果 `show` 为 `true`,则将 `ComponentA` 添加到 `includeList` 中,并从 `excludeList` 中删除;如果 `show` 为 `false`,则将 `ComponentA` 添加到 `excludeList` 中,并从 `includeList` 中删除。 这样就可以根据组件的属性值动态地缓存组件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值