HTTP长连接与短连接使用方法及测试详解
2020-02-07 18:01:05 来源:易采站长站 作者:王振洲
客户端设置的有效期大于服务端的,那么实际连接的有效期呢?三分钟之后再次请求,从连接池中lease连接的时候,提示Connection expired @ Wed Apr 26 14:08:05,即在上一次请求之后的5s失效,说明是服务端的设置生效了。
[2017-04-26 14:11:00 DEBUG] (org.apache.http.impl.conn.PoolingHttpClientConnectionManager:?) - Connection request: [route: {}->http://bizdomain:80][total kept alive: 1; route allocated: 1 of 32; total allocated: 1 of 200]
[2017-04-26 14:11:00 DEBUG] (org.apache.http.impl.conn.CPool:?) - Connection [id:2][route:{}->http://bizdomain:80][state:null] expired @ Wed Apr 26 14:08:05 GMT+08:00 2017
源码分析
通过源代码了解一下连接失效时间的设置过程。
//org.apache.http.impl.execchain.MainClientExec#execute
......
//从连接池中lease connection
final HttpClientConnectionmanagedConn = connRequest.get(timeout > 0 ? timeout : 0, TimeUnit.MILLISECONDS);
......
//将conenction封装在ConnectionHolder中
final ConnectionHolder connHolder = new ConnectionHolder(this.log, this.connManager, managedConn);
......
// The connection is in or can be brought to a re-usable state.
//如果返回值消息头中connection设置为close,则返回false
if (reuseStrategy.keepAlive(response, context)) {
// Set the idle duration of this connection
//取出response消息头中,keep-alive的timeout值
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();
}
待读取响应之后,释放连接,即:connHolder.releaseConnection()。调用org.apache.http.impl.conn.PoolingHttpClientConnectionManager#releaseConnection方法。
@Override
public void releaseConnection(final HttpClientConnection managedConn,
final Object state,final long keepalive, final TimeUnit tunit) {
Args.notNull(managedConn, "Managed connection");
synchronized (managedConn) {
final CPoolEntry entry = CPoolProxy.detach(managedConn);
if (entry == null) {
return;
}
final ManagedHttpClientConnection conn = entry.getConnection();
try {
if (conn.isOpen()) {
final TimeUnit effectiveUnit = tunit != null ? tunit : TimeUnit.MILLISECONDS;
entry.setState(state);
//设置失效时间
entry.updateExpiry(keepalive, effectiveUnit);
}
} finally {
。。。。。。
}
}
}
}
然后再下一次HTTP操作,从连接池中获取连接时
//org.apache.http.impl.conn.PoolingHttpClientConnectionManager#requestConnection调用org.apache.http.pool.AbstractConnPool#lease,
//调用getPoolEntryBlocking,调用org.apache.http.impl.conn.CPoolEntry#isExpired
@Override
public boolean isExpired(final long now) {
final boolean expired = super.isExpired(now);
if (expired && this.log.isDebugEnabled()) {
//日志中看到的内容
this.log.debug("Connection " + this + " expired @ " + new Date(getExpiry()));
}
return expired;
}
暂时禁止评论













闽公网安备 35020302000061号