# HG changeset patch # User chegar # Date 1519985715 0 # Node ID 87781d9c49969462b8d076c1c6e0880b222d7747 # Parent 377c5ebfc3196441aaf64fb6bdb590f785f30324 http-client-branch: improve HTTP/2 caching diff -r 377c5ebfc319 -r 87781d9c4996 src/java.net.http/share/classes/jdk/internal/net/http/Http2ClientImpl.java --- a/src/java.net.http/share/classes/jdk/internal/net/http/Http2ClientImpl.java Fri Mar 02 10:11:30 2018 +0000 +++ b/src/java.net.http/share/classes/jdk/internal/net/http/Http2ClientImpl.java Fri Mar 02 10:15:15 2018 +0000 @@ -97,13 +97,21 @@ synchronized (this) { Http2Connection connection = connections.get(key); - if (connection != null) { // fast path if connection already exists - return MinimalFuture.completedFuture(connection); + if (connection != null) { + if (connection.closed) { + debug.log(Level.DEBUG, "removing found closed connection: %s", connection); + connections.remove(key); + } else { + // fast path if connection already exists + debug.log(Level.DEBUG, "found connection in the pool: %s", connection); + return MinimalFuture.completedFuture(connection); + } } if (!req.secure() || failures.contains(key)) { // secure: negotiate failed before. Use http/1.1 // !secure: no connection available in cache. Attempt upgrade + debug.log(Level.DEBUG, "not found in connection pool"); return MinimalFuture.completedFuture(null); } } @@ -130,17 +138,31 @@ * has not been sent as part of the initial alpn negotiation */ boolean offerConnection(Http2Connection c) { - String key = c.key(); - Http2Connection c1 = connections.putIfAbsent(key, c); - if (c1 != null) { - c.setSingleStream(true); + debug.log(Level.DEBUG, "offering to the connection pool: %s", c); + if (c.closed) { + debug.log(Level.DEBUG, "skipping offered closed connection: %s", c); return false; } - return true; + + String key = c.key(); + synchronized(this) { + Http2Connection c1 = connections.putIfAbsent(key, c); + if (c1 != null) { + c.setSingleStream(true); + debug.log(Level.DEBUG, "existing entry in connection pool for %s", key); + return false; + } + debug.log(Level.DEBUG, "put in the connection pool: %s", c); + return true; + } } void deleteConnection(Http2Connection c) { - connections.remove(c.key()); + debug.log(Level.DEBUG, "removing from the connection pool: %s", c); + synchronized (this) { + connections.remove(c.key()); + debug.log(Level.DEBUG, "removed from the connection pool: %s", c); + } } void stop() {