author | mbaesken |
Thu, 28 Nov 2019 13:02:39 +0100 | |
changeset 59323 | ae2eb76c486d |
parent 58055 | 734f7711f87c |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
48083 | 28 |
import java.io.IOException; |
29 |
import java.lang.System.Logger.Level; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.net.InetSocketAddress; |
48083 | 31 |
import java.nio.ByteBuffer; |
32 |
import java.time.Instant; |
|
33 |
import java.time.temporal.ChronoUnit; |
|
34 |
import java.util.ArrayList; |
|
35 |
import java.util.Collections; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.util.HashMap; |
48083 | 37 |
import java.util.Iterator; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.util.LinkedList; |
48083 | 39 |
import java.util.List; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.util.ListIterator; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
import java.util.Objects; |
48083 | 42 |
import java.util.Optional; |
43 |
import java.util.concurrent.Flow; |
|
44 |
import java.util.stream.Collectors; |
|
49765 | 45 |
import jdk.internal.net.http.common.FlowTube; |
46 |
import jdk.internal.net.http.common.Logger; |
|
47 |
import jdk.internal.net.http.common.Utils; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
* Http 1.1 connection pool. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
final class ConnectionPool { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
static final long KEEP_ALIVE = Utils.getIntegerNetProperty( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
"jdk.httpclient.keepalive.timeout", 1200); // seconds |
50681 | 56 |
static final long MAX_POOL_SIZE = Utils.getIntegerNetProperty( |
57 |
"jdk.httpclient.connectionPoolSize", 0); // unbounded |
|
49765 | 58 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
// Pools of idle connections |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
|
48083 | 62 |
private final HashMap<CacheKey,LinkedList<HttpConnection>> plainPool; |
63 |
private final HashMap<CacheKey,LinkedList<HttpConnection>> sslPool; |
|
64 |
private final ExpiryList expiryList; |
|
65 |
private final String dbgTag; // used for debug |
|
66 |
boolean stopped; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
68 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
69 |
* Entries in connection pool are keyed by destination address and/or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
* proxy address: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
* case 1: plain TCP not via proxy (destination only) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
* case 2: plain TCP via proxy (proxy only) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
* case 3: SSL not via proxy (destination only) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
* case 4: SSL over tunnel (destination and proxy) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
static class CacheKey { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
final InetSocketAddress proxy; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
final InetSocketAddress destination; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
CacheKey(InetSocketAddress destination, InetSocketAddress proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
81 |
this.proxy = proxy; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
82 |
this.destination = destination; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
83 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
85 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
86 |
public boolean equals(Object obj) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
if (obj == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
if (getClass() != obj.getClass()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
final CacheKey other = (CacheKey) obj; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
if (!Objects.equals(this.proxy, other.proxy)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
if (!Objects.equals(this.destination, other.destination)) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
99 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
100 |
return true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
101 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
104 |
public int hashCode() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
105 |
return Objects.hash(proxy, destination); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
106 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
107 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
108 |
|
48083 | 109 |
ConnectionPool(long clientId) { |
110 |
this("ConnectionPool("+clientId+")"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
* There should be one of these per HttpClient. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
*/ |
48083 | 116 |
private ConnectionPool(String tag) { |
117 |
dbgTag = tag; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
118 |
plainPool = new HashMap<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
sslPool = new HashMap<>(); |
48083 | 120 |
expiryList = new ExpiryList(); |
121 |
} |
|
122 |
||
123 |
final String dbgString() { |
|
124 |
return dbgTag; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
|
49765 | 127 |
synchronized void start() { |
48083 | 128 |
assert !stopped : "Already stopped"; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
static CacheKey cacheKey(InetSocketAddress destination, |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
132 |
InetSocketAddress proxy) |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
133 |
{ |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
return new CacheKey(destination, proxy); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
synchronized HttpConnection getConnection(boolean secure, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
InetSocketAddress addr, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
InetSocketAddress proxy) { |
48083 | 140 |
if (stopped) return null; |
58055
734f7711f87c
8230526: jdk.internal.net.http.PlainProxyConnection is never reused by HttpClient
dfuchs
parents:
50681
diff
changeset
|
141 |
// for plain (unsecure) proxy connection the destination address is irrelevant. |
734f7711f87c
8230526: jdk.internal.net.http.PlainProxyConnection is never reused by HttpClient
dfuchs
parents:
50681
diff
changeset
|
142 |
addr = secure || proxy == null ? addr : null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
CacheKey key = new CacheKey(addr, proxy); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
HttpConnection c = secure ? findConnection(key, sslPool) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
145 |
: findConnection(key, plainPool); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
//System.out.println ("getConnection returning: " + c); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
147 |
return c; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
148 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
150 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
151 |
* Returns the connection to the pool. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
*/ |
48083 | 153 |
void returnToPool(HttpConnection conn) { |
154 |
returnToPool(conn, Instant.now(), KEEP_ALIVE); |
|
155 |
} |
|
156 |
||
157 |
// Called also by whitebox tests |
|
158 |
void returnToPool(HttpConnection conn, Instant now, long keepAlive) { |
|
159 |
||
160 |
// Don't call registerCleanupTrigger while holding a lock, |
|
161 |
// but register it before the connection is added to the pool, |
|
162 |
// since we don't want to trigger the cleanup if the connection |
|
163 |
// is not in the pool. |
|
164 |
CleanupTrigger cleanup = registerCleanupTrigger(conn); |
|
165 |
||
166 |
// it's possible that cleanup may have been called. |
|
50681 | 167 |
HttpConnection toClose = null; |
48083 | 168 |
synchronized(this) { |
169 |
if (cleanup.isDone()) { |
|
170 |
return; |
|
171 |
} else if (stopped) { |
|
172 |
conn.close(); |
|
173 |
return; |
|
174 |
} |
|
50681 | 175 |
if (MAX_POOL_SIZE > 0 && expiryList.size() >= MAX_POOL_SIZE) { |
176 |
toClose = expiryList.removeOldest(); |
|
177 |
if (toClose != null) removeFromPool(toClose); |
|
178 |
} |
|
48083 | 179 |
if (conn instanceof PlainHttpConnection) { |
180 |
putConnection(conn, plainPool); |
|
181 |
} else { |
|
182 |
assert conn.isSecure(); |
|
183 |
putConnection(conn, sslPool); |
|
184 |
} |
|
185 |
expiryList.add(conn, now, keepAlive); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
} |
50681 | 187 |
if (toClose != null) { |
188 |
if (debug.on()) { |
|
189 |
debug.log("Maximum pool size reached: removing oldest connection %s", |
|
190 |
toClose.dbgString()); |
|
191 |
} |
|
192 |
close(toClose); |
|
193 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
//System.out.println("Return to pool: " + conn); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
196 |
|
48083 | 197 |
private CleanupTrigger registerCleanupTrigger(HttpConnection conn) { |
198 |
// Connect the connection flow to a pub/sub pair that will take the |
|
199 |
// connection out of the pool and close it if anything happens |
|
200 |
// while the connection is sitting in the pool. |
|
201 |
CleanupTrigger cleanup = new CleanupTrigger(conn); |
|
202 |
FlowTube flow = conn.getConnectionFlow(); |
|
49765 | 203 |
if (debug.on()) debug.log("registering %s", cleanup); |
48083 | 204 |
flow.connectFlows(cleanup, cleanup); |
205 |
return cleanup; |
|
206 |
} |
|
207 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
208 |
private HttpConnection |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
209 |
findConnection(CacheKey key, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
210 |
HashMap<CacheKey,LinkedList<HttpConnection>> pool) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
211 |
LinkedList<HttpConnection> l = pool.get(key); |
47116
6160b308ed24
8187044: HttpClient ConnectionPool may spawn several concurrent CacheCleaner and prevent early GC of HttpClient.
dfuchs
parents:
44854
diff
changeset
|
212 |
if (l == null || l.isEmpty()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
213 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
214 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
215 |
HttpConnection c = l.removeFirst(); |
48083 | 216 |
expiryList.remove(c); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
return c; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
218 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
220 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
/* called from cache cleaner only */ |
48083 | 222 |
private boolean |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
removeFromPool(HttpConnection c, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
HashMap<CacheKey,LinkedList<HttpConnection>> pool) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
//System.out.println("cacheCleaner removing: " + c); |
48083 | 226 |
assert Thread.holdsLock(this); |
227 |
CacheKey k = c.cacheKey(); |
|
228 |
List<HttpConnection> l = pool.get(k); |
|
229 |
if (l == null || l.isEmpty()) { |
|
230 |
pool.remove(k); |
|
231 |
return false; |
|
232 |
} |
|
233 |
return l.remove(c); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
234 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
private void |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
putConnection(HttpConnection c, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
HashMap<CacheKey,LinkedList<HttpConnection>> pool) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
239 |
CacheKey key = c.cacheKey(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
240 |
LinkedList<HttpConnection> l = pool.get(key); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
if (l == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
l = new LinkedList<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
pool.put(key, l); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
l.add(c); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
|
48083 | 248 |
/** |
249 |
* Purge expired connection and return the number of milliseconds |
|
250 |
* in which the next connection is scheduled to expire. |
|
251 |
* If no connections are scheduled to be purged return 0. |
|
252 |
* @return the delay in milliseconds in which the next connection will |
|
253 |
* expire. |
|
254 |
*/ |
|
255 |
long purgeExpiredConnectionsAndReturnNextDeadline() { |
|
256 |
if (!expiryList.purgeMaybeRequired()) return 0; |
|
257 |
return purgeExpiredConnectionsAndReturnNextDeadline(Instant.now()); |
|
47116
6160b308ed24
8187044: HttpClient ConnectionPool may spawn several concurrent CacheCleaner and prevent early GC of HttpClient.
dfuchs
parents:
44854
diff
changeset
|
258 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
259 |
|
48083 | 260 |
// Used for whitebox testing |
261 |
long purgeExpiredConnectionsAndReturnNextDeadline(Instant now) { |
|
262 |
long nextPurge = 0; |
|
263 |
||
264 |
// We may be in the process of adding new elements |
|
265 |
// to the expiry list - but those elements will not |
|
266 |
// have outlast their keep alive timer yet since we're |
|
267 |
// just adding them. |
|
268 |
if (!expiryList.purgeMaybeRequired()) return nextPurge; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
269 |
|
48083 | 270 |
List<HttpConnection> closelist; |
271 |
synchronized (this) { |
|
272 |
closelist = expiryList.purgeUntil(now); |
|
273 |
for (HttpConnection c : closelist) { |
|
274 |
if (c instanceof PlainHttpConnection) { |
|
275 |
boolean wasPresent = removeFromPool(c, plainPool); |
|
276 |
assert wasPresent; |
|
277 |
} else { |
|
278 |
boolean wasPresent = removeFromPool(c, sslPool); |
|
279 |
assert wasPresent; |
|
280 |
} |
|
281 |
} |
|
282 |
nextPurge = now.until( |
|
283 |
expiryList.nextExpiryDeadline().orElse(now), |
|
284 |
ChronoUnit.MILLIS); |
|
47116
6160b308ed24
8187044: HttpClient ConnectionPool may spawn several concurrent CacheCleaner and prevent early GC of HttpClient.
dfuchs
parents:
44854
diff
changeset
|
285 |
} |
48083 | 286 |
closelist.forEach(this::close); |
287 |
return nextPurge; |
|
288 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
289 |
|
48083 | 290 |
private void close(HttpConnection c) { |
291 |
try { |
|
292 |
c.close(); |
|
293 |
} catch (Throwable e) {} // ignore |
|
294 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
|
48083 | 296 |
void stop() { |
297 |
List<HttpConnection> closelist = Collections.emptyList(); |
|
298 |
try { |
|
299 |
synchronized (this) { |
|
300 |
stopped = true; |
|
301 |
closelist = expiryList.stream() |
|
302 |
.map(e -> e.connection) |
|
303 |
.collect(Collectors.toList()); |
|
304 |
expiryList.clear(); |
|
305 |
plainPool.clear(); |
|
306 |
sslPool.clear(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
307 |
} |
48083 | 308 |
} finally { |
309 |
closelist.forEach(this::close); |
|
310 |
} |
|
311 |
} |
|
312 |
||
313 |
static final class ExpiryEntry { |
|
314 |
final HttpConnection connection; |
|
315 |
final Instant expiry; // absolute time in seconds of expiry time |
|
316 |
ExpiryEntry(HttpConnection connection, Instant expiry) { |
|
317 |
this.connection = connection; |
|
318 |
this.expiry = expiry; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
319 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
320 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
321 |
|
48083 | 322 |
/** |
323 |
* Manages a LinkedList of sorted ExpiryEntry. The entry with the closer |
|
324 |
* deadline is at the tail of the list, and the entry with the farther |
|
325 |
* deadline is at the head. In the most common situation, new elements |
|
326 |
* will need to be added at the head (or close to it), and expired elements |
|
327 |
* will need to be purged from the tail. |
|
328 |
*/ |
|
329 |
private static final class ExpiryList { |
|
330 |
private final LinkedList<ExpiryEntry> list = new LinkedList<>(); |
|
331 |
private volatile boolean mayContainEntries; |
|
332 |
||
50681 | 333 |
int size() { return list.size(); } |
334 |
||
48083 | 335 |
// A loosely accurate boolean whose value is computed |
336 |
// at the end of each operation performed on ExpiryList; |
|
337 |
// Does not require synchronizing on the ConnectionPool. |
|
338 |
boolean purgeMaybeRequired() { |
|
339 |
return mayContainEntries; |
|
340 |
} |
|
341 |
||
342 |
// Returns the next expiry deadline |
|
343 |
// should only be called while holding a synchronization |
|
344 |
// lock on the ConnectionPool |
|
345 |
Optional<Instant> nextExpiryDeadline() { |
|
346 |
if (list.isEmpty()) return Optional.empty(); |
|
347 |
else return Optional.of(list.getLast().expiry); |
|
348 |
} |
|
349 |
||
350 |
// should only be called while holding a synchronization |
|
351 |
// lock on the ConnectionPool |
|
50681 | 352 |
HttpConnection removeOldest() { |
353 |
ExpiryEntry entry = list.pollLast(); |
|
354 |
return entry == null ? null : entry.connection; |
|
355 |
} |
|
356 |
||
357 |
// should only be called while holding a synchronization |
|
358 |
// lock on the ConnectionPool |
|
48083 | 359 |
void add(HttpConnection conn) { |
360 |
add(conn, Instant.now(), KEEP_ALIVE); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
} |
48083 | 362 |
|
363 |
// Used by whitebox test. |
|
364 |
void add(HttpConnection conn, Instant now, long keepAlive) { |
|
365 |
Instant then = now.truncatedTo(ChronoUnit.SECONDS) |
|
366 |
.plus(keepAlive, ChronoUnit.SECONDS); |
|
367 |
||
368 |
// Elements with the farther deadline are at the head of |
|
369 |
// the list. It's more likely that the new element will |
|
370 |
// have the farthest deadline, and will need to be inserted |
|
371 |
// at the head of the list, so we're using an ascending |
|
372 |
// list iterator to find the right insertion point. |
|
373 |
ListIterator<ExpiryEntry> li = list.listIterator(); |
|
374 |
while (li.hasNext()) { |
|
375 |
ExpiryEntry entry = li.next(); |
|
376 |
||
377 |
if (then.isAfter(entry.expiry)) { |
|
378 |
li.previous(); |
|
379 |
// insert here |
|
380 |
li.add(new ExpiryEntry(conn, then)); |
|
381 |
mayContainEntries = true; |
|
382 |
return; |
|
383 |
} |
|
384 |
} |
|
385 |
// last (or first) element of list (the last element is |
|
386 |
// the first when the list is empty) |
|
387 |
list.add(new ExpiryEntry(conn, then)); |
|
388 |
mayContainEntries = true; |
|
389 |
} |
|
390 |
||
391 |
// should only be called while holding a synchronization |
|
392 |
// lock on the ConnectionPool |
|
393 |
void remove(HttpConnection c) { |
|
394 |
if (c == null || list.isEmpty()) return; |
|
395 |
ListIterator<ExpiryEntry> li = list.listIterator(); |
|
396 |
while (li.hasNext()) { |
|
397 |
ExpiryEntry e = li.next(); |
|
398 |
if (e.connection.equals(c)) { |
|
399 |
li.remove(); |
|
400 |
mayContainEntries = !list.isEmpty(); |
|
401 |
return; |
|
402 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
404 |
} |
48083 | 405 |
|
406 |
// should only be called while holding a synchronization |
|
407 |
// lock on the ConnectionPool. |
|
408 |
// Purge all elements whose deadline is before now (now included). |
|
409 |
List<HttpConnection> purgeUntil(Instant now) { |
|
410 |
if (list.isEmpty()) return Collections.emptyList(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
411 |
|
48083 | 412 |
List<HttpConnection> closelist = new ArrayList<>(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
413 |
|
48083 | 414 |
// elements with the closest deadlines are at the tail |
415 |
// of the queue, so we're going to use a descending iterator |
|
416 |
// to remove them, and stop when we find the first element |
|
417 |
// that has not expired yet. |
|
418 |
Iterator<ExpiryEntry> li = list.descendingIterator(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
419 |
while (li.hasNext()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
420 |
ExpiryEntry entry = li.next(); |
48083 | 421 |
// use !isAfter instead of isBefore in order to |
422 |
// remove the entry if its expiry == now |
|
423 |
if (!entry.expiry.isAfter(now)) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
424 |
li.remove(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
425 |
HttpConnection c = entry.connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
426 |
closelist.add(c); |
48083 | 427 |
} else break; // the list is sorted |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
428 |
} |
48083 | 429 |
mayContainEntries = !list.isEmpty(); |
430 |
return closelist; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
431 |
} |
48083 | 432 |
|
433 |
// should only be called while holding a synchronization |
|
434 |
// lock on the ConnectionPool |
|
435 |
java.util.stream.Stream<ExpiryEntry> stream() { |
|
436 |
return list.stream(); |
|
437 |
} |
|
438 |
||
439 |
// should only be called while holding a synchronization |
|
440 |
// lock on the ConnectionPool |
|
441 |
void clear() { |
|
442 |
list.clear(); |
|
443 |
mayContainEntries = false; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
444 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
445 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
446 |
|
50681 | 447 |
// Remove a connection from the pool. |
448 |
// should only be called while holding a synchronization |
|
449 |
// lock on the ConnectionPool |
|
450 |
private void removeFromPool(HttpConnection c) { |
|
451 |
assert Thread.holdsLock(this); |
|
452 |
if (c instanceof PlainHttpConnection) { |
|
453 |
removeFromPool(c, plainPool); |
|
454 |
} else { |
|
455 |
assert c.isSecure(); |
|
456 |
removeFromPool(c, sslPool); |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
// Used by tests |
|
461 |
synchronized boolean contains(HttpConnection c) { |
|
462 |
final CacheKey key = c.cacheKey(); |
|
463 |
List<HttpConnection> list; |
|
464 |
if ((list = plainPool.get(key)) != null) { |
|
465 |
if (list.contains(c)) return true; |
|
466 |
} |
|
467 |
if ((list = sslPool.get(key)) != null) { |
|
468 |
if (list.contains(c)) return true; |
|
469 |
} |
|
470 |
return false; |
|
471 |
} |
|
472 |
||
48083 | 473 |
void cleanup(HttpConnection c, Throwable error) { |
49765 | 474 |
if (debug.on()) |
475 |
debug.log("%s : ConnectionPool.cleanup(%s)", |
|
476 |
String.valueOf(c.getConnectionFlow()), error); |
|
48083 | 477 |
synchronized(this) { |
50681 | 478 |
removeFromPool(c); |
48083 | 479 |
expiryList.remove(c); |
480 |
} |
|
481 |
c.close(); |
|
482 |
} |
|
483 |
||
484 |
/** |
|
485 |
* An object that subscribes to the flow while the connection is in |
|
486 |
* the pool. Anything that comes in will cause the connection to be closed |
|
487 |
* and removed from the pool. |
|
488 |
*/ |
|
489 |
private final class CleanupTrigger implements |
|
490 |
FlowTube.TubeSubscriber, FlowTube.TubePublisher, |
|
491 |
Flow.Subscription { |
|
492 |
||
493 |
private final HttpConnection connection; |
|
494 |
private volatile boolean done; |
|
495 |
||
496 |
public CleanupTrigger(HttpConnection connection) { |
|
497 |
this.connection = connection; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
498 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
499 |
|
48083 | 500 |
public boolean isDone() { return done;} |
501 |
||
502 |
private void triggerCleanup(Throwable error) { |
|
503 |
done = true; |
|
504 |
cleanup(connection, error); |
|
505 |
} |
|
506 |
||
507 |
@Override public void request(long n) {} |
|
508 |
@Override public void cancel() {} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
509 |
|
48083 | 510 |
@Override |
511 |
public void onSubscribe(Flow.Subscription subscription) { |
|
512 |
subscription.request(1); |
|
513 |
} |
|
514 |
@Override |
|
515 |
public void onError(Throwable error) { triggerCleanup(error); } |
|
516 |
@Override |
|
517 |
public void onComplete() { triggerCleanup(null); } |
|
518 |
@Override |
|
519 |
public void onNext(List<ByteBuffer> item) { |
|
520 |
triggerCleanup(new IOException("Data received while in pool")); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
521 |
} |
48083 | 522 |
|
523 |
@Override |
|
524 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) { |
|
525 |
subscriber.onSubscribe(this); |
|
526 |
} |
|
527 |
||
528 |
@Override |
|
529 |
public String toString() { |
|
530 |
return "CleanupTrigger(" + connection.getConnectionFlow() + ")"; |
|
531 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
532 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
533 |
} |