8289366: Improve HTTP/2 client usage

Reviewed-by: dfuchs, rhalade
This commit is contained in:
Daniel Jeliński 2022-07-12 11:17:57 +00:00 committed by Henry Jen
parent 1ae6836521
commit 2cee77444f
7 changed files with 31 additions and 17 deletions

View File

@ -103,7 +103,7 @@ class AsyncSSLConnection extends AbstractAsyncSSLConnection {
@Override
ConnectionPool.CacheKey cacheKey() {
return ConnectionPool.cacheKey(address, null);
return ConnectionPool.cacheKey(true, address, null);
}
@Override

View File

@ -28,7 +28,6 @@ package jdk.internal.net.http;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.util.concurrent.CompletableFuture;
import java.net.http.HttpHeaders;
import java.util.function.Function;
import jdk.internal.net.http.common.MinimalFuture;
import jdk.internal.net.http.common.SSLTube;
@ -106,7 +105,7 @@ class AsyncSSLTunnelConnection extends AbstractAsyncSSLConnection {
@Override
ConnectionPool.CacheKey cacheKey() {
return ConnectionPool.cacheKey(address, plainConnection.proxyAddr);
return ConnectionPool.cacheKey(true, address, plainConnection.proxyAddr);
}
@Override

View File

@ -26,7 +26,6 @@
package jdk.internal.net.http;
import java.io.IOException;
import java.lang.System.Logger.Level;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.time.Instant;
@ -68,18 +67,21 @@ final class ConnectionPool {
/**
* Entries in connection pool are keyed by destination address and/or
* proxy address:
* case 1: plain TCP not via proxy (destination only)
* case 1: plain TCP not via proxy (destination IP only)
* case 2: plain TCP via proxy (proxy only)
* case 3: SSL not via proxy (destination only)
* case 4: SSL over tunnel (destination and proxy)
* case 3: SSL not via proxy (destination IP+hostname only)
* case 4: SSL over tunnel (destination IP+hostname and proxy)
*/
static class CacheKey {
final InetSocketAddress proxy;
final InetSocketAddress destination;
final boolean secure;
CacheKey(InetSocketAddress destination, InetSocketAddress proxy) {
private CacheKey(boolean secure, InetSocketAddress destination,
InetSocketAddress proxy) {
this.proxy = proxy;
this.destination = destination;
this.secure = secure;
}
@Override
@ -91,12 +93,26 @@ final class ConnectionPool {
return false;
}
final CacheKey other = (CacheKey) obj;
if (this.secure != other.secure) {
return false;
}
if (!Objects.equals(this.proxy, other.proxy)) {
return false;
}
if (!Objects.equals(this.destination, other.destination)) {
return false;
}
if (secure && destination != null) {
if (destination.getHostName() != null) {
if (!destination.getHostName().equalsIgnoreCase(
other.destination.getHostName())) {
return false;
}
} else {
if (other.destination.getHostName() != null)
return false;
}
}
return true;
}
@ -128,10 +144,10 @@ final class ConnectionPool {
assert !stopped : "Already stopped";
}
static CacheKey cacheKey(InetSocketAddress destination,
static CacheKey cacheKey(boolean secure, InetSocketAddress destination,
InetSocketAddress proxy)
{
return new CacheKey(destination, proxy);
return new CacheKey(secure, destination, proxy);
}
synchronized HttpConnection getConnection(boolean secure,
@ -140,7 +156,7 @@ final class ConnectionPool {
if (stopped) return null;
// for plain (unsecure) proxy connection the destination address is irrelevant.
addr = secure || proxy == null ? addr : null;
CacheKey key = new CacheKey(addr, proxy);
CacheKey key = new CacheKey(secure, addr, proxy);
HttpConnection c = secure ? findConnection(key, sslPool)
: findConnection(key, plainPool);
//System.out.println ("getConnection returning: " + c);

View File

@ -414,7 +414,7 @@ class PlainHttpConnection extends HttpConnection {
@Override
ConnectionPool.CacheKey cacheKey() {
return new ConnectionPool.CacheKey(address, null);
return ConnectionPool.cacheKey(false, address, null);
}
@Override

View File

@ -35,7 +35,7 @@ class PlainProxyConnection extends PlainHttpConnection {
@Override
ConnectionPool.CacheKey cacheKey() {
return new ConnectionPool.CacheKey(null, address);
return ConnectionPool.cacheKey(false, null, address);
}
@Override

View File

@ -30,11 +30,10 @@ import java.net.InetSocketAddress;
import java.net.http.HttpTimeoutException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.function.Function;
import java.net.http.HttpHeaders;
import jdk.internal.net.http.common.FlowTube;
import jdk.internal.net.http.common.MinimalFuture;
import static java.net.http.HttpResponse.BodyHandlers.discarding;
@ -153,7 +152,7 @@ final class PlainTunnelingConnection extends HttpConnection {
@Override
ConnectionPool.CacheKey cacheKey() {
return new ConnectionPool.CacheKey(null, proxyAddr);
return ConnectionPool.cacheKey(false, null, proxyAddr);
}
@Override

View File

@ -457,7 +457,7 @@ public class ConnectionPoolTest {
InetSocketAddress proxy,
boolean secured) {
super(address, impl);
this.key = ConnectionPool.cacheKey(address, proxy);
this.key = ConnectionPool.cacheKey(secured, address, proxy);
this.address = address;
this.proxy = proxy;
this.secured = secured;