From 425bc883d8d623ef8d3c448fafd36729f7741bcb Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Thu, 26 Sep 2024 11:23:26 +0200 Subject: [PATCH] =?UTF-8?q?Jersey=20update=20from=203.1.3=20to=203.1.4=20s?= =?UTF-8?q?lows=20down=20our=20external=20service=20res=E2=80=A6=20#5746?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jorge Bescos Gascon --- .../client/HttpUrlConnectorProvider.java | 22 ++++++++++++++++++- 1 file changed, 21 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 10dbe3da89..66925cf551 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,9 @@ import java.net.Proxy; import java.net.URL; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.logging.Logger; import javax.ws.rs.client.Client; @@ -295,9 +298,26 @@ 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 { + Lock lock = locks.computeIfAbsent(url, u -> new ReentrantLock()); + lock.lock(); + try { + return (proxy == null) ? (HttpURLConnection) url.openConnection() : (HttpURLConnection) url.openConnection(proxy); + } finally { + lock.unlock(); + } } }