在上一篇文章里我们介绍了 httpclient 连接池中连接的申请,在这里我们主要介绍连接的和释放。
http连接的释放
httpclient 连接池中连接对象的释放主要涉及了ConnectionHolder 对象实例的 releaseConnection() 方法,PoolingHttpClientConnectionManager 对象实例的 releaseConnection() 方法以及Cpool 对象实例的 release() 方法,核心代码如下:
/*ConnectionHolder*/
public void releaseConnection() {
releaseConnection(this.reusable);
}
private void releaseConnection(final boolean reusable) {
if (this.released.compareAndSet(false, true)) {
synchronized (this.managedConn) {
if (reusable) {
this.manager.releaseConnection(this.managedConn,this.state, this.validDuration, this.timeUnit);
} else {
try {
this.managedConn.close();
log.debug("Connection discarded");
} catch (final IOException ex) {
if (this.log.isDebugEnabled()) {
this.log.debug(ex.getMessage(), ex);
}
} finally {
this.manager.releaseConnection(this.managedConn, null, 0, TimeUnit.MILLISECONDS);
}
}
}
}
}
/*PoolingHttpClientConnectionManager*/
public void releaseConnection(final HttpClientConnection managedConn, final Object state, final long keepalive, final TimeUnit timeUnit) {
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 = timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS;
entry.setState(state);
entry.updateExpiry(keepalive, effectiveUnit);
if (this.log.isDebugEnabled()) {
final String s;
if (keepalive > 0) {
s = "for " + (double) effectiveUnit.toMillis(keepalive) / 1000 + " seconds";
} else {
s = "indefinitely";
}
this.log.debug("Connection " + format(entry) + " can be kept alive " + s);
}
conn.setSocketTimeout(0);
}
} finally {
this.pool.release(entry, conn.isOpen() && entry.isRouteComplete());
if (this.log.isDebugEnabled()) {
this.log.debug("Connection released: " + format(entry) + formatStats(entry.getRoute()));
}
}
}
}
/*Cpool*/
public void release(final E entry, final boolean reusable) {
this.lock.lock();
try {
if (this.leased.remove(entry)) {
final RouteSpecificPool<T, C, E> pool = getPool(entry.getRoute());
pool.free(entry, reusable);
if (reusable && !this.isShutDown) {
this.available.addFirst(entry);
} else {
entry.close();
}
onRelease(entry);
Future<E> future = pool.nextPending();
if (future != null) {
this.pending.remove(future);
} else {
future = this.pending.poll();
}
if (future != null) {
this.condition.signalAll();
}
}
} finally {
this.lock.unlock();
}
}
个人觉得在连接申请和释放的时候还有一定的优化空间,申请连接的时候,当连接池中不能申请到可用连接,会把当前线程在对象 condition 上等待,对象 condition 是 global 连接池 Cpool 的属性。释放连接的时候,归还连接到invidual route pool 和 global pool 之后,通过condition.signalAll()方法唤醒在 condition 对象上等待的所有线程。试想如下情况:
如果 condition 是 individual pool 级别的,那么就可以做到针对每个 route 单独的等待和唤醒请求连接的线程,从而避免上述 thread-b 唤醒之后依然得不到连接而再次进入等待状态。以上也是自己对此设计的想法,欢迎大家一起讨论学习。
目前先写到这里,在下一篇文章里我们介绍 http 连接的重用和 keep-alive 。