Skip to content

Commit

Permalink
Use reentrant locks
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Sep 25, 2024
1 parent 4f588c2 commit 415b187
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -296,7 +298,7 @@ default HttpURLConnection getConnection(URL url, Proxy proxy) throws IOException

private static class DefaultConnectionFactory implements ConnectionFactory {

private final ConcurrentHashMap<URL, Object> locks = new ConcurrentHashMap<>();
private final ConcurrentHashMap<URL, Lock> locks = new ConcurrentHashMap<>();

@Override
public HttpURLConnection getConnection(final URL url) throws IOException {
Expand All @@ -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();
}
}
}
Expand Down

0 comments on commit 415b187

Please sign in to comment.