Added methods to passivate and activate managed connections. These methods are used to restore / store the original socket timeout value upon connection lease / release

This commit is contained in:
Oleg Kalnichevski 2017-11-17 17:49:14 +01:00
parent 5da1bd8f8d
commit f182b73e48
7 changed files with 65 additions and 3 deletions

View File

@ -245,6 +245,8 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
checkExpiry();
if (this.conn == null) {
this.conn = this.connFactory.createConnection(null);
} else {
this.conn.activate();
}
this.leased = true;
return this.conn;
@ -284,6 +286,7 @@ public class BasicHttpClientConnectionManager implements HttpClientConnectionMan
}
} else {
this.state = state;
conn.passivate();
if (TimeValue.isPositive(keepAlive)) {
if (this.log.isDebugEnabled()) {
this.log.debug("Connection can be kept alive for " + keepAlive);

View File

@ -69,6 +69,8 @@ final class DefaultManagedHttpClientConnection
private final String id;
private final AtomicBoolean closed;
private int socketTimeout;
public DefaultManagedHttpClientConnection(
final String id,
final CharsetDecoder chardecoder,
@ -101,6 +103,7 @@ final class DefaultManagedHttpClientConnection
throw new InterruptedIOException("Connection already shutdown");
}
super.bind(socketHolder);
socketTimeout = socketHolder.getSocket().getSoTimeout();
}
@Override
@ -150,6 +153,7 @@ final class DefaultManagedHttpClientConnection
@Override
public void bind(final Socket socket) throws IOException {
super.bind(this.wirelog.isDebugEnabled() ? new LoggingSocketHolder(socket, this.id, this.wirelog) : new SocketHolder(socket));
socketTimeout = socket.getSoTimeout();
}
@Override
@ -174,4 +178,14 @@ final class DefaultManagedHttpClientConnection
}
}
@Override
public void passivate() {
super.setSocketTimeout(0);
}
@Override
public void activate() {
super.setSocketTimeout(socketTimeout);
}
}

View File

@ -291,7 +291,10 @@ public class PoolingHttpClientConnectionManager
}
}
}
if (!poolEntry.hasConnection()) {
final ManagedHttpClientConnection conn = poolEntry.getConnection();
if (conn != null) {
conn.activate();
} else {
poolEntry.assignConnection(connFactory.createConnection(null));
}
if (log.isDebugEnabled()) {
@ -335,6 +338,7 @@ public class PoolingHttpClientConnectionManager
if (reusable) {
entry.updateState(state);
entry.updateExpiry(keepAlive);
conn.passivate();
if (this.log.isDebugEnabled()) {
final String s;
if (TimeValue.isPositive(keepAlive)) {

View File

@ -66,10 +66,12 @@ final class DefaultManagedAsyncClientConnection implements ManagedAsyncClientCon
private final Logger log = LogManager.getLogger(getClass());
private final IOSession ioSession;
private final int socketTimeout;
private final AtomicBoolean closed;
public DefaultManagedAsyncClientConnection(final IOSession ioSession) {
this.ioSession = ioSession;
this.socketTimeout = ioSession.getSocketTimeout();
this.closed = new AtomicBoolean();
}
@ -186,4 +188,14 @@ final class DefaultManagedAsyncClientConnection implements ManagedAsyncClientCon
ioSession.addLast(command);
}
@Override
public void passivate() {
ioSession.setSocketTimeout(0);
}
@Override
public void activate() {
ioSession.setSocketTimeout(socketTimeout);
}
}

View File

@ -217,12 +217,16 @@ public class PoolingAsyncClientConnectionManager implements AsyncClientConnectio
route, state, requestTimeout, new FutureCallback<PoolEntry<HttpRoute, ManagedAsyncClientConnection>>() {
void leaseCompleted(final PoolEntry<HttpRoute, ManagedAsyncClientConnection> poolEntry) {
final ManagedAsyncClientConnection connection = poolEntry.getConnection();
if (connection != null) {
connection.activate();
}
if (log.isDebugEnabled()) {
log.debug("Connection leased: " + ConnPoolSupport.formatStats(poolEntry.getConnection(), route, state, pool));
log.debug("Connection leased: " + ConnPoolSupport.formatStats(connection, route, state, pool));
}
final AsyncConnectionEndpoint endpoint = new InternalConnectionEndpoint(poolEntry);
if (log.isDebugEnabled()) {
log.debug(ConnPoolSupport.getId(endpoint) + ": acquired " + ConnPoolSupport.getId(poolEntry.getConnection()));
log.debug(ConnPoolSupport.getId(endpoint) + ": acquired " + ConnPoolSupport.getId(connection));
}
resultFuture.completed(endpoint);
}
@ -296,6 +300,7 @@ public class PoolingAsyncClientConnectionManager implements AsyncClientConnectio
if (reusable) {
entry.updateState(state);
entry.updateExpiry(keepAlive);
connection.passivate();
if (log.isDebugEnabled()) {
final String s;
if (TimeValue.isPositive(keepAlive)) {

View File

@ -72,4 +72,18 @@ public interface ManagedHttpClientConnection extends HttpClientConnection {
*/
SSLSession getSSLSession();
/**
* Puts the connection into idle mode.
*
* @since 5.0
*/
void passivate();
/**
* Restores the connection from idle mode.
*
* @since 5.0
*/
void activate();
}

View File

@ -39,4 +39,14 @@ public interface ManagedAsyncClientConnection extends HttpConnection, TransportS
void submitCommand(Command command);
/**
* Puts the connection into idle mode.
*/
void passivate();
/**
* Restores the connection from idle mode.
*/
void activate();
}