前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HttpComponents HttpClient连接池(4)-连接的重用和KeepAlive

HttpComponents HttpClient连接池(4)-连接的重用和KeepAlive

作者头像
TA码字
发布2020-04-01 15:00:30
2.7K0
发布2020-04-01 15:00:30
举报
文章被收录于专栏:TA码字TA码字

上一篇文章里我们介绍了 httpclient 连接池中对于连接的申请和释放,这里我们主要介绍连接的重用,以及 keep alive。

http连接的重用

上一篇文章 http 连接的释放中 ConnectionHolder的releaseConnection() 方法会根据是否重用有不同的处理,那么 ConnectionHolders 是如何决定是否重用呢。就在 MainClientExec类 的 execute() 方法里,核心代码如下:

 if (reuseStrategy.keepAlive(response, context)) {
// Set the idle duration of this connection
    final long duration = keepAliveStrategy.getKeepAliveDuration(response, context);
    if (this.log.isDebugEnabled()) {
         final String s;
         if (duration > 0) {
             s = "for " + duration + " " + TimeUnit.MILLISECONDS;
          } else {
              s = "indefinitely";
           }
               this.log.debug("Connection can be kept alive " + s);
        }
            connHolder.setValidFor(duration, TimeUnit.MILLISECONDS);
            connHolder.markReusable();
} else {
    connHolder.markNonReusable();
}

分析以上代码的核心本质是调用 reuseStrategy的keepAlive() 方法来决定是否重用。reuseStrategy的值 在 HttpClientBuilder 进行构建 httpclient 连接池的默认值为 DefaultClientConnectionReuseStrategy ,核心代码如下:

public boolean keepAlive(final HttpResponse response, final HttpContext context) {
    final HttpRequest request = (HttpRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST);
    if (request != null) {
        final Header[] connHeaders = request.getHeaders(HttpHeaders.CONNECTION);
        if (connHeaders.length != 0) {
            final TokenIterator ti = new BasicTokenIterator(new BasicHeaderIterator(connHeaders, null));
            while (ti.hasNext()) {
                final String token = ti.nextToken();
                if (HTTP.CONN_CLOSE.equalsIgnoreCase(token)) {
                    return false;
                }
            }
        }
    }
    return super.keepAlive(response, context);
}
  • 分析如上代码,如果 http 请求的请求头中包含项 Connection:Close ,那么不重用。
  • 另外该方法还调用了父类的keepAlive() 方法,其父类是 DefaultConnectionReuseStrategy 类型,对父类 keep alive有下面分析。
  • 对于父类的逻辑中,如果 http 响应的响应头中包含项 Connection:Close ,那么不重用。
  • 对于父类的逻辑中,如果 http 响应的响应头中包含项 Transfer-Encoding ,但是如果它的值不为 chunked ,那么不重用。
  • 对于父类的逻辑中,如果响应状态码为204表示没有数据,但是响应头里Content-Length的值大于0或者不为数字,或者 http 响应头里有 Transfer-Encoding项 ,那么不重用。

http连接的Keep Alive

  • 在上面的 http 连接重用代码中我们不难发现,在确定重用的基础上, keep alive 的时间长短是由keepAliveStrategy的getKeepAliveDuration()方法所决定的。对于 keepAliveStrategy 实例, 在 HttpClientBuilder 进行构建 httpclient 时默认策略为 DefaultConnectionKeepAliveStrategy ,其核心代码如下:
public long getKeepAliveDuration(final HttpResponse response, final HttpContext context) {
    Args.notNull(response, "HTTP response");
    final HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
    while (it.hasNext()) {
        final HeaderElement he = it.nextElement();
        final String param = he.getName();
        final String value = he.getValue();
        if (value != null && param.equalsIgnoreCase("timeout")) {
            try {
                return Long.parseLong(value) * 1000;
            } catch(final NumberFormatException ignore) {
            }
        }
    }
    return -1;
}
  • 通过分析上述代码,核心本质上是取得响应头 response header 项的 Keep-Alive: timeout 的值,单位为秒,如果没有那么取值-1。然后利用该值更新 CpoolEntry 对象的过期时间,如果该值为-1,则过期时间更新为Long.MAX_VALUE。
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 TA码字 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档