author | dfuchs |
Fri, 30 Aug 2019 15:42:27 +0100 | |
branch | JDK-8229867-branch |
changeset 57968 | 8595871a5446 |
parent 57956 | e0b8b019d2f5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
47361
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.net.www.http; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.NotSerializableException; |
|
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
30 |
import java.io.ObjectInputStream; |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
31 |
import java.io.ObjectOutputStream; |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
32 |
import java.net.URL; |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
33 |
import java.security.AccessController; |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
34 |
import java.security.PrivilegedAction; |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
35 |
import java.util.ArrayDeque; |
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
36 |
import java.util.ArrayList; |
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
37 |
import java.util.HashMap; |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
38 |
import java.util.List; |
57968 | 39 |
import java.util.concurrent.locks.Lock; |
40 |
import java.util.concurrent.locks.ReentrantLock; |
|
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
41 |
|
35786
9d26db88aa14
8149653: Move sun.misc.InnocuousThread to jdk.internal.misc
chegar
parents:
29986
diff
changeset
|
42 |
import jdk.internal.misc.InnocuousThread; |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
43 |
import sun.security.action.GetIntegerAction; |
2 | 44 |
|
45 |
/** |
|
46 |
* A class that implements a cache of idle Http connections for keep-alive |
|
47 |
* |
|
48 |
* @author Stephen R. Pietrowicz (NCSA) |
|
49 |
* @author Dave Brown |
|
50 |
*/ |
|
51 | 51 |
public class KeepAliveCache |
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
52 |
extends HashMap<KeepAliveKey, ClientVector> |
51 | 53 |
implements Runnable { |
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
47361
diff
changeset
|
54 |
@java.io.Serial |
2 | 55 |
private static final long serialVersionUID = -2937172892064557949L; |
56 |
||
57 |
/* maximum # keep-alive connections to maintain at once |
|
58 |
* This should be 2 by the HTTP spec, but because we don't support pipe-lining |
|
59 |
* a larger value is more appropriate. So we now set a default of 5, and the value |
|
60 |
* refers to the number of idle connections per destination (in the cache) only. |
|
61 |
* It can be reset by setting system property "http.maxConnections". |
|
62 |
*/ |
|
63 |
static final int MAX_CONNECTIONS = 5; |
|
64 |
static int result = -1; |
|
65 |
static int getMaxConnections() { |
|
66 |
if (result == -1) { |
|
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
67 |
result = AccessController.doPrivileged( |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
68 |
new GetIntegerAction("http.maxConnections", MAX_CONNECTIONS)) |
2 | 69 |
.intValue(); |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
70 |
if (result <= 0) { |
2 | 71 |
result = MAX_CONNECTIONS; |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
72 |
} |
2 | 73 |
} |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
74 |
return result; |
2 | 75 |
} |
76 |
||
77 |
static final int LIFETIME = 5000; |
|
78 |
||
57968 | 79 |
private final ReentrantLock cacheLock = new ReentrantLock(); |
2 | 80 |
private Thread keepAliveTimer = null; |
81 |
||
82 |
/** |
|
83 |
* Constructor |
|
84 |
*/ |
|
85 |
public KeepAliveCache() {} |
|
86 |
||
87 |
/** |
|
88 |
* Register this URL and HttpClient (that supports keep-alive) with the cache |
|
89 |
* @param url The URL contains info about the host and port |
|
90 |
* @param http The HttpClient to be cached |
|
91 |
*/ |
|
57968 | 92 |
public void put(final URL url, Object obj, HttpClient http) { |
93 |
cacheLock.lock(); |
|
94 |
try { |
|
95 |
boolean startThread = (keepAliveTimer == null); |
|
96 |
if (!startThread) { |
|
97 |
if (!keepAliveTimer.isAlive()) { |
|
98 |
startThread = true; |
|
99 |
} |
|
2 | 100 |
} |
57968 | 101 |
if (startThread) { |
102 |
clear(); |
|
103 |
/* Unfortunately, we can't always believe the keep-alive timeout we got |
|
104 |
* back from the server. If I'm connected through a Netscape proxy |
|
105 |
* to a server that sent me a keep-alive |
|
106 |
* time of 15 sec, the proxy unilaterally terminates my connection |
|
107 |
* The robustness to get around this is in HttpClient.parseHTTP() |
|
108 |
*/ |
|
109 |
final KeepAliveCache cache = this; |
|
110 |
AccessController.doPrivileged(new PrivilegedAction<>() { |
|
111 |
public Void run() { |
|
112 |
keepAliveTimer = InnocuousThread.newSystemThread("Keep-Alive-Timer", cache); |
|
113 |
keepAliveTimer.setDaemon(true); |
|
114 |
keepAliveTimer.setPriority(Thread.MAX_PRIORITY - 2); |
|
115 |
keepAliveTimer.start(); |
|
116 |
return null; |
|
117 |
} |
|
118 |
}); |
|
119 |
} |
|
2 | 120 |
|
57968 | 121 |
KeepAliveKey key = new KeepAliveKey(url, obj); |
122 |
ClientVector v = super.get(key); |
|
2 | 123 |
|
57968 | 124 |
if (v == null) { |
125 |
int keepAliveTimeout = http.getKeepAliveTimeout(); |
|
126 |
v = new ClientVector(keepAliveTimeout > 0 ? |
|
127 |
keepAliveTimeout * 1000 : LIFETIME); |
|
128 |
v.put(http); |
|
129 |
super.put(key, v); |
|
130 |
} else { |
|
131 |
v.put(http); |
|
132 |
} |
|
133 |
} finally { |
|
134 |
cacheLock.unlock(); |
|
2 | 135 |
} |
136 |
} |
|
137 |
||
51 | 138 |
/* remove an obsolete HttpClient from its VectorCache */ |
57968 | 139 |
public void remove(HttpClient h, Object obj) { |
140 |
cacheLock.lock(); |
|
141 |
try { |
|
142 |
KeepAliveKey key = new KeepAliveKey(h.url, obj); |
|
143 |
ClientVector v = super.get(key); |
|
144 |
if (v != null) { |
|
145 |
v.remove(h); |
|
146 |
if (v.isEmpty()) { |
|
147 |
super.remove(key); |
|
148 |
} |
|
2 | 149 |
} |
57968 | 150 |
} finally { |
151 |
cacheLock.unlock(); |
|
2 | 152 |
} |
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Check to see if this URL has a cached HttpClient |
|
157 |
*/ |
|
57968 | 158 |
public HttpClient get(URL url, Object obj) { |
159 |
cacheLock.lock(); |
|
160 |
try { |
|
161 |
KeepAliveKey key = new KeepAliveKey(url, obj); |
|
162 |
ClientVector v = super.get(key); |
|
163 |
if (v == null) { // nothing in cache yet |
|
164 |
return null; |
|
165 |
} |
|
166 |
return v.get(); |
|
167 |
} finally { |
|
168 |
cacheLock.unlock(); |
|
2 | 169 |
} |
170 |
} |
|
171 |
||
172 |
/* Sleeps for an alloted timeout, then checks for timed out connections. |
|
173 |
* Errs on the side of caution (leave connections idle for a relatively |
|
174 |
* short time). |
|
175 |
*/ |
|
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
176 |
@Override |
2 | 177 |
public void run() { |
178 |
do { |
|
179 |
try { |
|
180 |
Thread.sleep(LIFETIME); |
|
181 |
} catch (InterruptedException e) {} |
|
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
182 |
|
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
183 |
// Remove all outdated HttpClients. |
57968 | 184 |
cacheLock.lock(); |
185 |
try { |
|
2 | 186 |
long currentTime = System.currentTimeMillis(); |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
187 |
List<KeepAliveKey> keysToRemove = new ArrayList<>(); |
2 | 188 |
|
51 | 189 |
for (KeepAliveKey key : keySet()) { |
190 |
ClientVector v = get(key); |
|
57968 | 191 |
final Lock lock = v.lock; |
192 |
lock.lock(); |
|
193 |
try { |
|
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
194 |
KeepAliveEntry e = v.peek(); |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
195 |
while (e != null) { |
2 | 196 |
if ((currentTime - e.idleStartTime) > v.nap) { |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
197 |
v.poll(); |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
198 |
e.hc.closeServer(); |
2 | 199 |
} else { |
200 |
break; |
|
201 |
} |
|
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
202 |
e = v.peek(); |
2 | 203 |
} |
204 |
||
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
205 |
if (v.isEmpty()) { |
2 | 206 |
keysToRemove.add(key); |
207 |
} |
|
57968 | 208 |
} finally { |
209 |
lock.unlock(); |
|
2 | 210 |
} |
211 |
} |
|
51 | 212 |
|
213 |
for (KeepAliveKey key : keysToRemove) { |
|
57968 | 214 |
super.remove(key); |
2 | 215 |
} |
57968 | 216 |
} finally { |
217 |
cacheLock.unlock(); |
|
2 | 218 |
} |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
219 |
} while (!isEmpty()); |
2 | 220 |
} |
221 |
||
222 |
/* |
|
223 |
* Do not serialize this class! |
|
224 |
*/ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
47361
diff
changeset
|
225 |
@java.io.Serial |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
226 |
private void writeObject(ObjectOutputStream stream) throws IOException { |
2 | 227 |
throw new NotSerializableException(); |
228 |
} |
|
229 |
||
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
47361
diff
changeset
|
230 |
@java.io.Serial |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
231 |
private void readObject(ObjectInputStream stream) |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
232 |
throws IOException, ClassNotFoundException |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
233 |
{ |
2 | 234 |
throw new NotSerializableException(); |
235 |
} |
|
236 |
} |
|
237 |
||
238 |
/* FILO order for recycling HttpClients, should run in a thread |
|
239 |
* to time them out. If > maxConns are in use, block. |
|
240 |
*/ |
|
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
241 |
class ClientVector extends ArrayDeque<KeepAliveEntry> { |
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
47361
diff
changeset
|
242 |
@java.io.Serial |
2 | 243 |
private static final long serialVersionUID = -8680532108106489459L; |
57968 | 244 |
final ReentrantLock lock = new ReentrantLock(); |
2 | 245 |
|
246 |
// sleep time in milliseconds, before cache clear |
|
247 |
int nap; |
|
248 |
||
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
249 |
ClientVector(int nap) { |
2 | 250 |
this.nap = nap; |
251 |
} |
|
252 |
||
57968 | 253 |
HttpClient get() { |
254 |
lock.lock(); |
|
255 |
try { |
|
256 |
if (isEmpty()) { |
|
257 |
return null; |
|
258 |
} |
|
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
259 |
|
57968 | 260 |
// Loop until we find a connection that has not timed out |
261 |
HttpClient hc = null; |
|
262 |
long currentTime = System.currentTimeMillis(); |
|
263 |
do { |
|
264 |
KeepAliveEntry e = pop(); |
|
265 |
if ((currentTime - e.idleStartTime) > nap) { |
|
266 |
e.hc.closeServer(); |
|
267 |
} else { |
|
268 |
hc = e.hc; |
|
269 |
} |
|
270 |
} while ((hc == null) && (!isEmpty())); |
|
271 |
return hc; |
|
272 |
} finally { |
|
273 |
lock.unlock(); |
|
274 |
} |
|
2 | 275 |
} |
276 |
||
277 |
/* return a still valid, unused HttpClient */ |
|
57968 | 278 |
void put(HttpClient h) { |
279 |
lock.lock(); |
|
280 |
try { |
|
281 |
if (size() >= KeepAliveCache.getMaxConnections()) { |
|
282 |
h.closeServer(); // otherwise the connection remains in limbo |
|
283 |
} else { |
|
284 |
push(new KeepAliveEntry(h, System.currentTimeMillis())); |
|
285 |
} |
|
286 |
} finally { |
|
287 |
lock.unlock(); |
|
2 | 288 |
} |
289 |
} |
|
290 |
||
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
291 |
/* remove an HttpClient */ |
57968 | 292 |
boolean remove(HttpClient h) { |
293 |
lock.lock(); |
|
294 |
try { |
|
295 |
for (KeepAliveEntry curr : this) { |
|
296 |
if (curr.hc == h) { |
|
297 |
return super.remove(curr); |
|
298 |
} |
|
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
299 |
} |
57968 | 300 |
return false; |
301 |
} finally { |
|
302 |
lock.unlock(); |
|
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
303 |
} |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
304 |
} |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
305 |
|
2 | 306 |
/* |
307 |
* Do not serialize this class! |
|
308 |
*/ |
|
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
47361
diff
changeset
|
309 |
@java.io.Serial |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
310 |
private void writeObject(ObjectOutputStream stream) throws IOException { |
2 | 311 |
throw new NotSerializableException(); |
312 |
} |
|
313 |
||
57956
e0b8b019d2f5
8229997: Apply java.io.Serial annotations in java.base
darcy
parents:
47361
diff
changeset
|
314 |
@java.io.Serial |
47361
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
315 |
private void readObject(ObjectInputStream stream) |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
316 |
throws IOException, ClassNotFoundException |
74e1913a98c0
8155590: Dubious collection management in sun.net.www.http.KeepAliveCache
clanger
parents:
47216
diff
changeset
|
317 |
{ |
2 | 318 |
throw new NotSerializableException(); |
319 |
} |
|
320 |
} |
|
321 |
||
322 |
class KeepAliveKey { |
|
323 |
private String protocol = null; |
|
324 |
private String host = null; |
|
325 |
private int port = 0; |
|
326 |
private Object obj = null; // additional key, such as socketfactory |
|
327 |
||
328 |
/** |
|
329 |
* Constructor |
|
330 |
* |
|
331 |
* @param url the URL containing the protocol, host and port information |
|
332 |
*/ |
|
333 |
public KeepAliveKey(URL url, Object obj) { |
|
334 |
this.protocol = url.getProtocol(); |
|
335 |
this.host = url.getHost(); |
|
336 |
this.port = url.getPort(); |
|
337 |
this.obj = obj; |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Determine whether or not two objects of this type are equal |
|
342 |
*/ |
|
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
343 |
@Override |
2 | 344 |
public boolean equals(Object obj) { |
345 |
if ((obj instanceof KeepAliveKey) == false) |
|
346 |
return false; |
|
347 |
KeepAliveKey kae = (KeepAliveKey)obj; |
|
348 |
return host.equals(kae.host) |
|
349 |
&& (port == kae.port) |
|
350 |
&& protocol.equals(kae.protocol) |
|
351 |
&& this.obj == kae.obj; |
|
352 |
} |
|
353 |
||
354 |
/** |
|
355 |
* The hashCode() for this object is the string hashCode() of |
|
356 |
* concatenation of the protocol, host name and port. |
|
357 |
*/ |
|
3865
cc2ca3d07bbb
6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion
chegar
parents:
715
diff
changeset
|
358 |
@Override |
2 | 359 |
public int hashCode() { |
360 |
String str = protocol+host+port; |
|
361 |
return this.obj == null? str.hashCode() : |
|
362 |
str.hashCode() + this.obj.hashCode(); |
|
363 |
} |
|
364 |
} |
|
365 |
||
366 |
class KeepAliveEntry { |
|
367 |
HttpClient hc; |
|
368 |
long idleStartTime; |
|
369 |
||
370 |
KeepAliveEntry(HttpClient hc, long idleStartTime) { |
|
371 |
this.hc = hc; |
|
372 |
this.idleStartTime = idleStartTime; |
|
373 |
} |
|
374 |
} |