From 4277759a851c18824878796cbe6596bfa962931d Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Mon, 1 Jun 2020 17:19:31 +0200 Subject: [PATCH] Fixes #4904 - WebsocketClient creates more connections than needed. After merge fixes. Signed-off-by: Simone Bordet --- .../jetty/client/AbstractConnectionPool.java | 19 ++++++++++++++++++- .../jetty/client/DuplexConnectionPool.java | 2 +- .../eclipse/jetty/client/HttpDestination.java | 2 +- .../jetty/client/MultiplexConnectionPool.java | 4 +--- .../client/RoundRobinConnectionPool.java | 2 +- .../http/HttpDestinationOverHTTPTest.java | 6 +++++- 6 files changed, 27 insertions(+), 8 deletions(-) diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractConnectionPool.java b/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractConnectionPool.java index ce8e62ad9e8..15470cc0794 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractConnectionPool.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/AbstractConnectionPool.java @@ -47,13 +47,30 @@ public abstract class AbstractConnectionPool implements ConnectionPool, Dumpable private final int maxConnections; private final Callback requester; + /** + * @param destination the correspondent destination + * @param maxConnections the max number of connections + * @param requester the callback to notify about new connection creation/failure + * @deprecated use {@link #AbstractConnectionPool(HttpDestination, int, Callback)} instead + */ + @Deprecated protected AbstractConnectionPool(Destination destination, int maxConnections, Callback requester) { - this.destination = (HttpDestination)destination; + this((HttpDestination)destination, maxConnections, requester); + } + + protected AbstractConnectionPool(HttpDestination destination, int maxConnections, Callback requester) + { + this.destination = destination; this.maxConnections = maxConnections; this.requester = requester; } + protected HttpDestination getHttpDestination() + { + return destination; + } + @ManagedAttribute(value = "The max number of connections", readonly = true) public int getMaxConnectionCount() { diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/DuplexConnectionPool.java b/jetty-client/src/main/java/org/eclipse/jetty/client/DuplexConnectionPool.java index 05332d3e189..c0cd9d6ebca 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/DuplexConnectionPool.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/DuplexConnectionPool.java @@ -52,7 +52,7 @@ public class DuplexConnectionPool extends AbstractConnectionPool implements Swee public DuplexConnectionPool(Destination destination, int maxConnections, Callback requester) { - super(destination, maxConnections, requester); + super((HttpDestination)destination, maxConnections, requester); this.idleConnections = new ArrayDeque<>(maxConnections); this.activeConnections = new HashSet<>(maxConnections); } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java index a2768d841ce..9fbf47474e5 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpDestination.java @@ -340,7 +340,7 @@ public abstract class HttpDestination extends ContainerLifeCycle implements Dest } } - public ProcessResult process(Connection connection) + private ProcessResult process(Connection connection) { HttpClient client = getHttpClient(); HttpExchange exchange = getHttpExchanges().poll(); diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/MultiplexConnectionPool.java b/jetty-client/src/main/java/org/eclipse/jetty/client/MultiplexConnectionPool.java index e65fc3e2259..a048629095c 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/MultiplexConnectionPool.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/MultiplexConnectionPool.java @@ -42,7 +42,6 @@ public class MultiplexConnectionPool extends AbstractConnectionPool implements C private static final Logger LOG = Log.getLogger(MultiplexConnectionPool.class); private final ReentrantLock lock = new ReentrantLock(); - private final HttpDestination destination; private final Deque idleConnections; private final Map muxedConnections; private final Map busyConnections; @@ -51,7 +50,6 @@ public class MultiplexConnectionPool extends AbstractConnectionPool implements C public MultiplexConnectionPool(HttpDestination destination, int maxConnections, Callback requester, int maxMultiplex) { super(destination, maxConnections, requester); - this.destination = destination; this.idleConnections = new ArrayDeque<>(maxConnections); this.muxedConnections = new HashMap<>(maxConnections); this.busyConnections = new HashMap<>(maxConnections); @@ -64,7 +62,7 @@ public class MultiplexConnectionPool extends AbstractConnectionPool implements C Connection connection = activate(); if (connection == null) { - int queuedRequests = destination.getQueuedRequestCount(); + int queuedRequests = getHttpDestination().getQueuedRequestCount(); int maxMultiplex = getMaxMultiplex(); int maxPending = ceilDiv(queuedRequests, maxMultiplex); tryCreate(maxPending); diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/RoundRobinConnectionPool.java b/jetty-client/src/main/java/org/eclipse/jetty/client/RoundRobinConnectionPool.java index 1a9564b2af7..181fecf5dcb 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/RoundRobinConnectionPool.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/RoundRobinConnectionPool.java @@ -42,7 +42,7 @@ public class RoundRobinConnectionPool extends AbstractConnectionPool implements public RoundRobinConnectionPool(Destination destination, int maxConnections, Callback requester, int maxMultiplex) { - super(destination, maxConnections, requester); + super((HttpDestination)destination, maxConnections, requester); entries = new ArrayList<>(maxConnections); for (int i = 0; i < maxConnections; ++i) { diff --git a/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpDestinationOverHTTPTest.java b/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpDestinationOverHTTPTest.java index 1ea236b8e48..a00363683e8 100644 --- a/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpDestinationOverHTTPTest.java +++ b/jetty-client/src/test/java/org/eclipse/jetty/client/http/HttpDestinationOverHTTPTest.java @@ -18,6 +18,7 @@ package org.eclipse.jetty.client.http; +import java.lang.reflect.Method; import java.util.concurrent.CountDownLatch; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.TimeUnit; @@ -28,6 +29,7 @@ import org.eclipse.jetty.client.ConnectionPool; import org.eclipse.jetty.client.DuplexConnectionPool; import org.eclipse.jetty.client.EmptyServerHandler; import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.client.HttpDestination; import org.eclipse.jetty.client.Origin; import org.eclipse.jetty.client.api.Connection; import org.eclipse.jetty.client.api.ContentResponse; @@ -205,7 +207,9 @@ public class HttpDestinationOverHTTPTest extends AbstractHttpClientServerTest } // There are no exchanges so process() is a no-op. - destination.process(connection1); + Method process = HttpDestination.class.getDeclaredMethod("process", Connection.class); + process.setAccessible(true); + process.invoke(destination, connection1); destination.release(connection1); Connection connection2 = connectionPool.acquire();