From 4f588c2b868b4861fa54ff8817631d815ea1a4eb Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Mon, 23 Sep 2024 10:22:39 +0200 Subject: [PATCH 1/2] 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); + } } } From 415b187329fefd8773fc95bf735b11eaa8179730 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Wed, 25 Sep 2024 08:32:07 +0200 Subject: [PATCH 2/2] Use reentrant locks Signed-off-by: Jorge Bescos Gascon --- .../jersey/client/HttpUrlConnectorProvider.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 80318a4ed1..306e336cef 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 @@ -22,6 +22,8 @@ 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 jakarta.ws.rs.client.Client; @@ -296,7 +298,7 @@ default HttpURLConnection getConnection(URL url, Proxy proxy) throws IOException private static class DefaultConnectionFactory implements ConnectionFactory { - private final ConcurrentHashMap locks = new ConcurrentHashMap<>(); + private final ConcurrentHashMap locks = new ConcurrentHashMap<>(); @Override public HttpURLConnection getConnection(final URL url) throws IOException { @@ -309,9 +311,12 @@ public HttpURLConnection getConnection(URL url, Proxy proxy) throws IOException } private HttpURLConnection connect(URL url, Proxy proxy) throws IOException { - Object lock = locks.computeIfAbsent(url, u -> new Object()); - synchronized (lock){ + Lock lock = locks.computeIfAbsent(url, u -> new ReentrantLock()); + lock.lock(); + try { return (proxy == null) ? (HttpURLConnection) url.openConnection() : (HttpURLConnection) url.openConnection(proxy); + } finally { + lock.unlock(); } } }