Skip to content

Commit

Permalink
Closes #4809 - Set a max number of requests per connection.
Browse files Browse the repository at this point in the history
Implemented as part of #4975.

Added a test case that proves that the connection is closed
when the max usage count is reached.

Improved logging.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed Aug 12, 2020
1 parent 3c6c507 commit 4a0af04
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,10 @@ protected void proceed()
protected Connection activate()
{
Pool<Connection>.Entry entry = pool.acquire();
if (LOG.isDebugEnabled())
LOG.debug("activated '{}'", entry);
if (entry != null)
{
if (LOG.isDebugEnabled())
LOG.debug("activated {}", entry);
Connection connection = entry.getPooled();
acquired(connection);
return connection;
Expand Down Expand Up @@ -298,15 +298,13 @@ protected boolean deactivate(Connection connection)
Pool<Connection>.Entry entry = (Pool<Connection>.Entry)attachable.getAttachment();
if (entry == null)
return true;
if (LOG.isDebugEnabled())
LOG.debug("releasing {}", entry);
boolean reusable = pool.release(entry);
if (!reusable)
{
remove(connection);
return false;
}
return true;
if (LOG.isDebugEnabled())
LOG.debug("Released ({}) {}", reusable, entry);
if (reusable)
return true;
remove(connection);
return false;
}

@Override
Expand All @@ -325,9 +323,9 @@ protected boolean remove(Connection connection, boolean force)
if (entry == null)
return false;
attachable.setAttachment(null);
if (LOG.isDebugEnabled())
LOG.debug("removing {}", entry);
boolean removed = pool.remove(entry);
if (LOG.isDebugEnabled())
LOG.debug("Removed ({}) {}", removed, entry);
if (removed || force)
{
released(connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,42 @@ public void resolve(String host, int port, Promise<List<InetSocketAddress>> prom
assertThat(connectionPool.getConnectionCount(), Matchers.lessThanOrEqualTo(count));
}

@ParameterizedTest
@MethodSource("pools")
public void testConnectionMaxUsage(ConnectionPoolFactory factory) throws Exception
{
startServer(new EmptyServerHandler());

int maxUsageCount = 2;
startClient(destination ->
{
AbstractConnectionPool connectionPool = (AbstractConnectionPool)factory.factory.newConnectionPool(destination);
connectionPool.setMaxUsageCount(maxUsageCount);
return connectionPool;
});
client.setMaxConnectionsPerDestination(1);

// Send first request, we are within the max usage count.
ContentResponse response1 = client.newRequest("localhost", connector.getLocalPort()).send();
assertEquals(HttpStatus.OK_200, response1.getStatus());

HttpDestination destination = (HttpDestination)client.getDestinations().get(0);
AbstractConnectionPool connectionPool = (AbstractConnectionPool)destination.getConnectionPool();

assertEquals(0, connectionPool.getActiveConnectionCount());
assertEquals(1, connectionPool.getIdleConnectionCount());
assertEquals(1, connectionPool.getConnectionCount());

// Send second request, max usage count will be reached,
// the only connection must be closed.
ContentResponse response2 = client.newRequest("localhost", connector.getLocalPort()).send();
assertEquals(HttpStatus.OK_200, response2.getStatus());

assertEquals(0, connectionPool.getActiveConnectionCount());
assertEquals(0, connectionPool.getIdleConnectionCount());
assertEquals(0, connectionPool.getConnectionCount());
}

private static class ConnectionPoolFactory
{
private final String name;
Expand Down
2 changes: 1 addition & 1 deletion jetty-util/src/main/java/org/eclipse/jetty/util/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public int getUsageCount()
public String toString()
{
long encoded = state.get();
return String.format("%s@%x{hi=%d,lo=%d.p=%s}",
return String.format("%s@%x{usage=%d,multiplex=%d,pooled=%s}",
getClass().getSimpleName(),
hashCode(),
AtomicBiInteger.getHi(encoded),
Expand Down

0 comments on commit 4a0af04

Please sign in to comment.