Merged branch 'jetty-9.4.x' into 'jetty-10.0.x'.

This commit is contained in:
Simone Bordet 2020-08-12 16:17:06 +02:00
commit ae713d65d9
3 changed files with 47 additions and 13 deletions

View File

@ -237,10 +237,10 @@ public abstract class AbstractConnectionPool implements ConnectionPool, Dumpable
protected Connection activate() protected Connection activate()
{ {
Pool<Connection>.Entry entry = pool.acquire(); Pool<Connection>.Entry entry = pool.acquire();
if (LOG.isDebugEnabled())
LOG.debug("activated '{}'", entry);
if (entry != null) if (entry != null)
{ {
if (LOG.isDebugEnabled())
LOG.debug("activated {}", entry);
Connection connection = entry.getPooled(); Connection connection = entry.getPooled();
acquired(connection); acquired(connection);
return connection; return connection;
@ -281,15 +281,13 @@ public abstract class AbstractConnectionPool implements ConnectionPool, Dumpable
Pool<Connection>.Entry entry = (Pool<Connection>.Entry)attachable.getAttachment(); Pool<Connection>.Entry entry = (Pool<Connection>.Entry)attachable.getAttachment();
if (entry == null) if (entry == null)
return true; return true;
if (LOG.isDebugEnabled())
LOG.debug("releasing {}", entry);
boolean reusable = pool.release(entry); boolean reusable = pool.release(entry);
if (!reusable) if (LOG.isDebugEnabled())
{ LOG.debug("Released ({}) {}", reusable, entry);
remove(connection); if (reusable)
return false; return true;
} remove(connection);
return true; return false;
} }
@Override @Override
@ -308,9 +306,9 @@ public abstract class AbstractConnectionPool implements ConnectionPool, Dumpable
if (entry == null) if (entry == null)
return false; return false;
attachable.setAttachment(null); attachable.setAttachment(null);
if (LOG.isDebugEnabled())
LOG.debug("removing {}", entry);
boolean removed = pool.remove(entry); boolean removed = pool.remove(entry);
if (LOG.isDebugEnabled())
LOG.debug("Removed ({}) {}", removed, entry);
if (removed || force) if (removed || force)
{ {
released(connection); released(connection);

View File

@ -360,6 +360,42 @@ public class ConnectionPoolTest
assertThat(connectionPool.getConnectionCount(), Matchers.lessThanOrEqualTo(count)); 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 static class ConnectionPoolFactory
{ {
private final String name; private final String name;

View File

@ -545,7 +545,7 @@ public class Pool<T> implements AutoCloseable, Dumpable
public String toString() public String toString()
{ {
long encoded = state.get(); 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(), getClass().getSimpleName(),
hashCode(), hashCode(),
AtomicBiInteger.getHi(encoded), AtomicBiInteger.getHi(encoded),