Skip to content

Commit e3ad4e5

Browse files
Yuri NesterenkoRealCLanger
authored andcommitted
8289366: Improve HTTP/2 client usage
Reviewed-by: mbalao Backport-of: 48e33fcf7723688f3ec76e7506853c731cf8dad6
1 parent ce65380 commit e3ad4e5

File tree

7 files changed

+31
-17
lines changed

7 files changed

+31
-17
lines changed

src/java.net.http/share/classes/jdk/internal/net/http/AsyncSSLConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ SocketChannel channel() {
103103

104104
@Override
105105
ConnectionPool.CacheKey cacheKey() {
106-
return ConnectionPool.cacheKey(address, null);
106+
return ConnectionPool.cacheKey(true, address, null);
107107
}
108108

109109
@Override

src/java.net.http/share/classes/jdk/internal/net/http/AsyncSSLTunnelConnection.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.net.InetSocketAddress;
2929
import java.nio.channels.SocketChannel;
3030
import java.util.concurrent.CompletableFuture;
31-
import java.net.http.HttpHeaders;
3231
import java.util.function.Function;
3332
import jdk.internal.net.http.common.MinimalFuture;
3433
import jdk.internal.net.http.common.SSLTube;
@@ -106,7 +105,7 @@ public String toString() {
106105

107106
@Override
108107
ConnectionPool.CacheKey cacheKey() {
109-
return ConnectionPool.cacheKey(address, plainConnection.proxyAddr);
108+
return ConnectionPool.cacheKey(true, address, plainConnection.proxyAddr);
110109
}
111110

112111
@Override

src/java.net.http/share/classes/jdk/internal/net/http/ConnectionPool.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
package jdk.internal.net.http;
2727

2828
import java.io.IOException;
29-
import java.lang.System.Logger.Level;
3029
import java.net.InetSocketAddress;
3130
import java.nio.ByteBuffer;
3231
import java.time.Instant;
@@ -68,18 +67,21 @@ final class ConnectionPool {
6867
/**
6968
* Entries in connection pool are keyed by destination address and/or
7069
* proxy address:
71-
* case 1: plain TCP not via proxy (destination only)
70+
* case 1: plain TCP not via proxy (destination IP only)
7271
* case 2: plain TCP via proxy (proxy only)
73-
* case 3: SSL not via proxy (destination only)
74-
* case 4: SSL over tunnel (destination and proxy)
72+
* case 3: SSL not via proxy (destination IP+hostname only)
73+
* case 4: SSL over tunnel (destination IP+hostname and proxy)
7574
*/
7675
static class CacheKey {
7776
final InetSocketAddress proxy;
7877
final InetSocketAddress destination;
78+
final boolean secure;
7979

80-
CacheKey(InetSocketAddress destination, InetSocketAddress proxy) {
80+
private CacheKey(boolean secure, InetSocketAddress destination,
81+
InetSocketAddress proxy) {
8182
this.proxy = proxy;
8283
this.destination = destination;
84+
this.secure = secure;
8385
}
8486

8587
@Override
@@ -91,12 +93,26 @@ public boolean equals(Object obj) {
9193
return false;
9294
}
9395
final CacheKey other = (CacheKey) obj;
96+
if (this.secure != other.secure) {
97+
return false;
98+
}
9499
if (!Objects.equals(this.proxy, other.proxy)) {
95100
return false;
96101
}
97102
if (!Objects.equals(this.destination, other.destination)) {
98103
return false;
99104
}
105+
if (secure && destination != null) {
106+
if (destination.getHostName() != null) {
107+
if (!destination.getHostName().equalsIgnoreCase(
108+
other.destination.getHostName())) {
109+
return false;
110+
}
111+
} else {
112+
if (other.destination.getHostName() != null)
113+
return false;
114+
}
115+
}
100116
return true;
101117
}
102118

@@ -128,10 +144,10 @@ synchronized void start() {
128144
assert !stopped : "Already stopped";
129145
}
130146

131-
static CacheKey cacheKey(InetSocketAddress destination,
147+
static CacheKey cacheKey(boolean secure, InetSocketAddress destination,
132148
InetSocketAddress proxy)
133149
{
134-
return new CacheKey(destination, proxy);
150+
return new CacheKey(secure, destination, proxy);
135151
}
136152

137153
synchronized HttpConnection getConnection(boolean secure,
@@ -140,7 +156,7 @@ synchronized HttpConnection getConnection(boolean secure,
140156
if (stopped) return null;
141157
// for plain (unsecure) proxy connection the destination address is irrelevant.
142158
addr = secure || proxy == null ? addr : null;
143-
CacheKey key = new CacheKey(addr, proxy);
159+
CacheKey key = new CacheKey(secure, addr, proxy);
144160
HttpConnection c = secure ? findConnection(key, sslPool)
145161
: findConnection(key, plainPool);
146162
//System.out.println ("getConnection returning: " + c);

src/java.net.http/share/classes/jdk/internal/net/http/PlainHttpConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public void close() {
378378

379379
@Override
380380
ConnectionPool.CacheKey cacheKey() {
381-
return new ConnectionPool.CacheKey(address, null);
381+
return ConnectionPool.cacheKey(false, address, null);
382382
}
383383

384384
@Override

src/java.net.http/share/classes/jdk/internal/net/http/PlainProxyConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class PlainProxyConnection extends PlainHttpConnection {
3535

3636
@Override
3737
ConnectionPool.CacheKey cacheKey() {
38-
return new ConnectionPool.CacheKey(null, address);
38+
return ConnectionPool.cacheKey(false, null, address);
3939
}
4040

4141
@Override

src/java.net.http/share/classes/jdk/internal/net/http/PlainTunnelingConnection.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@
3030
import java.net.http.HttpTimeoutException;
3131
import java.nio.ByteBuffer;
3232
import java.nio.channels.SocketChannel;
33-
import java.time.Duration;
3433
import java.util.concurrent.CompletableFuture;
3534
import java.util.concurrent.CompletionException;
3635
import java.util.function.Function;
37-
import java.net.http.HttpHeaders;
36+
3837
import jdk.internal.net.http.common.FlowTube;
3938
import jdk.internal.net.http.common.MinimalFuture;
4039
import static java.net.http.HttpResponse.BodyHandlers.discarding;
@@ -153,7 +152,7 @@ FlowTube getConnectionFlow() {
153152

154153
@Override
155154
ConnectionPool.CacheKey cacheKey() {
156-
return new ConnectionPool.CacheKey(null, proxyAddr);
155+
return ConnectionPool.cacheKey(false, null, proxyAddr);
157156
}
158157

159158
@Override

test/jdk/java/net/httpclient/whitebox/java.net.http/jdk/internal/net/http/ConnectionPoolTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ public HttpConnectionStub(
455455
InetSocketAddress proxy,
456456
boolean secured) {
457457
super(address, impl);
458-
this.key = ConnectionPool.cacheKey(address, proxy);
458+
this.key = ConnectionPool.cacheKey(secured, address, proxy);
459459
this.address = address;
460460
this.proxy = proxy;
461461
this.secured = secured;

0 commit comments

Comments
 (0)