@Override public void execute(final PoolEntry<T, C> entry) { if (entry.getUpdated() <= deadline) { entry.discardConnection(CloseMode.GRACEFUL); } }
HttpClientConnection getConnection() { final PoolEntry<HttpHost, HttpClientConnection> poolEntry = poolEntryRef.get(); return poolEntry != null ? poolEntry.getConnection() : null; }
@Override public void release(final PoolEntry<T, C> entry, final boolean reusable) { if (entry == null) { return; } if (isShutDown.get()) { return; } final PerRoutePool<T, C> routePool = getPool(entry.getRoute()); if (connPoolListener != null) { connPoolListener.onLease(entry.getRoute(), this); } routePool.release(entry, reusable); }
@Override public void execute(final PoolEntry<T, C> entry) { if (entry.getExpiryDeadline().isBefore(now)) { entry.discardConnection(CloseMode.GRACEFUL); } }
private PoolEntry<T, C> getAvailableEntry(final Object state) { final PoolEntry<T, C> entry = available.poll(); if (entry != null) { if (entry.getExpiryDeadline().isExpired()) { entry.discardConnection(CloseMode.GRACEFUL); } if (!LangUtils.equals(entry.getState(), state)) { entry.discardConnection(CloseMode.GRACEFUL); } } return entry; }
@Test public void testBasics() throws Exception { final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>( "route1", TimeValue.of(10L, TimeUnit.MILLISECONDS), currentTimeSupplier); Assert.assertEquals("route1", entry1.getRoute()); Assert.assertEquals(0, entry1.getUpdated()); Assert.assertEquals(Deadline.MIN_VALUE, entry1.getExpiryDeadline()); entry1.assignConnection(Mockito.mock(HttpConnection.class)); final long now = System.currentTimeMillis(); Assert.assertEquals("route1", entry1.getRoute()); Assert.assertTrue(now >= entry1.getUpdated()); Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiryDeadline()); Assert.assertEquals(entry1.getUpdated() + 10L, entry1.getValidityDeadline().getValue()); entry1.discardConnection(CloseMode.IMMEDIATE); Assert.assertEquals(0, entry1.getUpdated()); Assert.assertEquals(Deadline.MIN_VALUE, entry1.getExpiryDeadline()); }
return; final ManagedHttpClientConnection conn = entry.getConnection(); if (conn != null && keepAlive == null) { conn.close(CloseMode.GRACEFUL); try { if (reusable) { entry.updateState(state); entry.updateExpiry(keepAlive); conn.passivate(); if (this.log.isDebugEnabled()) { if (this.log.isDebugEnabled()) { this.log.debug("Connection released: " + ConnPoolSupport.formatStats( conn, entry.getRoute(), entry.getState(), pool));
@Test public void testExpiry() throws Exception { final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>( "route1", TimeValue.ZERO_MILLISECONDS, currentTimeSupplier); entry1.assignConnection(Mockito.mock(HttpConnection.class)); Assert.assertEquals(Deadline.MAX_VALUE, entry1.getExpiryDeadline()); entry1.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS)); Assert.assertEquals(entry1.getUpdated() + 50L, entry1.getExpiryDeadline().getValue()); entry1.updateExpiry(TimeValue.ZERO_MILLISECONDS); Assert.assertEquals(Deadline.MAX_VALUE, entry1.getExpiryDeadline()); final PoolEntry<String, HttpConnection> entry2 = new PoolEntry<>( "route1", TimeValue.of(100L, TimeUnit.MILLISECONDS), currentTimeSupplier); entry2.assignConnection(Mockito.mock(HttpConnection.class)); final Deadline validityDeadline = entry2.getValidityDeadline(); Assert.assertEquals(entry2.getUpdated() + 100L, entry2.getExpiryDeadline().getValue()); entry2.updateExpiry(TimeValue.of(50L, TimeUnit.MILLISECONDS)); Assert.assertEquals(entry2.getUpdated() + 50L, entry2.getExpiryDeadline().getValue()); entry2.updateExpiry(TimeValue.of(150L, TimeUnit.MILLISECONDS)); Assert.assertEquals(validityDeadline, entry2.getExpiryDeadline()); }
final ManagedHttpClientConnection conn = poolEntry.getConnection(); if (conn != null && poolEntry.getUpdated() + validateAfterInactivity.toMillis() <= System.currentTimeMillis()) { boolean stale; try { log.debug("Connection " + ConnPoolSupport.getId(conn) + " is stale"); poolEntry.discardConnection(CloseMode.IMMEDIATE); final ManagedHttpClientConnection conn = poolEntry.getConnection(); if (conn != null) { conn.activate(); } else { poolEntry.assignConnection(connFactory.createConnection(null)); poolEntry.getConnection(), route, state, pool));
public void release(final PoolEntry<T, C> releasedEntry, final boolean reusable) { removeLeased(releasedEntry); if (!reusable || releasedEntry.getExpiryDeadline().isExpired()) { releasedEntry.discardConnection(CloseMode.GRACEFUL); if (releasedEntry.hasConnection()) { switch (policy) { case LIFO: leaseRequest.completed(availableEntry); } else if (leased.size() < max) { final PoolEntry<T, C> newEntry = new PoolEntry<>(route, timeToLive); addLeased(newEntry); leaseRequest.completed(newEntry);
@Override public void connect(final ConnectionEndpoint endpoint, final TimeValue connectTimeout, final HttpContext context) throws IOException { Args.notNull(endpoint, "Managed endpoint"); final InternalConnectionEndpoint internalEndpoint = cast(endpoint); if (internalEndpoint.isConnected()) { return; } final PoolEntry<HttpRoute, ManagedHttpClientConnection> poolEntry = internalEndpoint.getPoolEntry(); if (!poolEntry.hasConnection()) { poolEntry.assignConnection(connFactory.createConnection(null)); } final HttpRoute route = poolEntry.getRoute(); final HttpHost host; if (route.getProxyHost() != null) { host = route.getProxyHost(); } else { host = route.getTargetHost(); } this.connectionOperator.connect( poolEntry.getConnection(), host, route.getLocalSocketAddress(), connectTimeout, defaultSocketConfig != null ? this.defaultSocketConfig : SocketConfig.DEFAULT, context); }
@Override public void completed(final PoolEntry<HttpHost, IOSession> poolEntry) { final AsyncClientEndpoint endpoint = new InternalAsyncClientEndpoint(poolEntry); final IOSession ioSession = poolEntry.getConnection(); if (ioSession != null && ioSession.isClosed()) { poolEntry.discardConnection(CloseMode.IMMEDIATE); if (poolEntry.hasConnection()) { resultFuture.completed(endpoint); } else {
entry.discardConnection(CloseMode.GRACEFUL); if (this.leased.remove(entry)) { if (this.connPoolListener != null) { this.connPoolListener.onRelease(entry.getRoute(), this); final PerRoutePool<T, C> pool = getPool(entry.getRoute()); final boolean keepAlive = entry.hasConnection() && reusable; pool.free(entry, keepAlive); if (keepAlive) { entry.discardConnection(CloseMode.GRACEFUL);
entry1.assignConnection(conn1); final PoolEntry<String, HttpConnection> entry2 = future2.get(); Assert.assertNotNull(entry2); entry2.assignConnection(conn2); final PoolEntry<String, HttpConnection> entry3 = future3.get(); Assert.assertNotNull(entry3); entry3.assignConnection(conn3); final PoolEntry<String, HttpConnection> entry4 = future4.get(); Assert.assertNotNull(entry4); Assert.assertSame(conn2, entry4.getConnection()); Assert.assertSame(conn1, entry5.getConnection()); Assert.assertSame(conn3, entry6.getConnection()); final PoolEntry<String, HttpConnection> entry7 = future7.get(); Assert.assertNotNull(entry7); Assert.assertSame(conn2, entry7.getConnection()); Assert.assertEquals(null, entry8.getConnection()); Assert.assertSame(conn3, entry9.getConnection());
break; if (entry.getExpiryDeadline().isExpired()) { entry.discardConnection(CloseMode.GRACEFUL); this.available.remove(entry); pool.free(entry, false); request.completed(entry); if (this.connPoolListener != null) { this.connPoolListener.onLease(entry.getRoute(), this); break; lastUsed.discardConnection(CloseMode.GRACEFUL); this.available.remove(lastUsed); pool.remove(lastUsed); if (!this.available.isEmpty()) { final PoolEntry<T, C> lastUsed = this.available.removeLast(); lastUsed.discardConnection(CloseMode.GRACEFUL); final PerRoutePool<T, C> otherpool = getPool(lastUsed.getRoute()); otherpool.remove(lastUsed); request.completed(entry); if (this.connPoolListener != null) { this.connPoolListener.onLease(entry.getRoute(), this);
@Override public void completed(final PoolEntry<HttpRoute, ManagedAsyncClientConnection> poolEntry) { final ManagedAsyncClientConnection connection = poolEntry.getConnection(); final TimeValue timeValue = PoolingAsyncClientConnectionManager.this.validateAfterInactivity; if (TimeValue.isPositive(timeValue) && connection != null && poolEntry.getUpdated() + timeValue.toMillis() <= System.currentTimeMillis()) { final ProtocolVersion protocolVersion = connection.getProtocolVersion(); if (HttpVersion.HTTP_2_0.greaterEquals(protocolVersion)) { log.debug("Connection " + ConnPoolSupport.getId(connection) + " is closed"); poolEntry.discardConnection(CloseMode.IMMEDIATE);
final PoolEntry<String, HttpConnection> entry1 = future1.get(); Assert.assertNotNull(entry1); entry1.assignConnection(conn1); Assert.assertTrue(future2.isDone()); final PoolEntry<String, HttpConnection> entry2 = future2.get(); Assert.assertNotNull(entry2); entry2.assignConnection(conn2); entry1.updateExpiry(TimeValue.of(1, TimeUnit.MILLISECONDS)); pool.release(entry1, true); entry2.updateExpiry(TimeValue.of(1000, TimeUnit.SECONDS)); pool.release(entry2, true);
public void shutdown(final CloseMode closeMode) { PoolEntry<T, C> availableEntry; while ((availableEntry = available.poll()) != null) { availableEntry.discardConnection(closeMode); } for (final PoolEntry<T, C> entry: this.leased) { entry.discardConnection(closeMode); } this.leased.clear(); }
@Test public void testValidInfinitely() throws Exception { final PoolEntry<String, HttpConnection> entry1 = new PoolEntry<>( "route1", TimeValue.ZERO_MILLISECONDS, currentTimeSupplier); entry1.assignConnection(Mockito.mock(HttpConnection.class)); Assert.assertEquals(Deadline.MAX_VALUE, entry1.getValidityDeadline()); Assert.assertEquals(entry1.getValidityDeadline(), entry1.getExpiryDeadline()); }
final PoolEntry<String, HttpConnection> entry1 = future1.get(); Assert.assertNotNull(entry1); Assert.assertFalse(entry1.hasConnection()); entry1.assignConnection(conn1); Assert.assertTrue(future2.isDone()); final PoolEntry<String, HttpConnection> entry2 = future2.get(); Assert.assertNotNull(entry2); Assert.assertFalse(entry2.hasConnection()); entry2.assignConnection(conn2); final PoolEntry<String, HttpConnection> entry3 = future3.get(); Assert.assertNotNull(entry3); Assert.assertFalse(entry3.hasConnection()); entry3.assignConnection(conn3); Assert.assertTrue(future4.isDone()); final PoolEntry<String, HttpConnection> entry4 = future4.get(); Assert.assertNotNull(entry4); Assert.assertFalse(entry4.hasConnection()); entry4.assignConnection(conn4); final PoolEntry<String, HttpConnection> entry5 = future5.get(); Assert.assertNotNull(entry5); Assert.assertFalse(entry5.hasConnection()); entry5.assignConnection(conn5); Assert.assertTrue(future6.isDone()); final PoolEntry<String, HttpConnection> entry6 = future6.get(); Assert.assertNotNull(entry6); Assert.assertTrue(entry6.hasConnection()); Assert.assertSame(conn4, entry6.getConnection());