From 4f588c2b868b4861fa54ff8817631d815ea1a4eb Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Mon, 23 Sep 2024 10:22:39 +0200 Subject: [PATCH] Jersey update from 3.1.3 to 3.1.4 slows down our external service response times drastically #5738 Signed-off-by: Jorge Bescos Gascon --- .../jersey/client/HttpUrlConnectorProvider.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnectorProvider.java b/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnectorProvider.java index 90d138d06e..80318a4ed1 100644 --- a/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnectorProvider.java +++ b/core-client/src/main/java/org/glassfish/jersey/client/HttpUrlConnectorProvider.java @@ -21,6 +21,7 @@ import java.net.Proxy; import java.net.URL; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.logging.Logger; import jakarta.ws.rs.client.Client; @@ -295,9 +296,23 @@ default HttpURLConnection getConnection(URL url, Proxy proxy) throws IOException private static class DefaultConnectionFactory implements ConnectionFactory { + private final ConcurrentHashMap locks = new ConcurrentHashMap<>(); + @Override public HttpURLConnection getConnection(final URL url) throws IOException { - return (HttpURLConnection) url.openConnection(); + return connect(url, null); + } + + @Override + public HttpURLConnection getConnection(URL url, Proxy proxy) throws IOException { + return connect(url, proxy); + } + + private HttpURLConnection connect(URL url, Proxy proxy) throws IOException { + Object lock = locks.computeIfAbsent(url, u -> new Object()); + synchronized (lock){ + return (proxy == null) ? (HttpURLConnection) url.openConnection() : (HttpURLConnection) url.openConnection(proxy); + } } }