author | chegar |
Wed, 07 Feb 2018 14:17:24 +0000 | |
branch | http-client-branch |
changeset 56089 | 42208b2f224e |
parent 56079 | src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/ConnectionPool.java@d23b02f37fce |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56063
diff
changeset
|
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 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
26 |
package java.net.http.internal; |
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; |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
45 |
import java.net.http.internal.common.FlowTube; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
46 |
import java.net.http.internal.common.Utils; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
|
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 |
* Http 1.1 connection pool. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
final class ConnectionPool { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
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
|
54 |
"jdk.httpclient.keepalive.timeout", 1200); // seconds |
48083 | 55 |
static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag. |
56 |
final System.Logger debug = Utils.getDebugLogger(this::dbgString, DEBUG); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
// Pools of idle connections |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
|
48083 | 60 |
private final HashMap<CacheKey,LinkedList<HttpConnection>> plainPool; |
61 |
private final HashMap<CacheKey,LinkedList<HttpConnection>> sslPool; |
|
62 |
private final ExpiryList expiryList; |
|
63 |
private final String dbgTag; // used for debug |
|
64 |
boolean stopped; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
* 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
|
68 |
* proxy address: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
69 |
* 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
|
70 |
* 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
|
71 |
* 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
|
72 |
* 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
|
73 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
static class CacheKey { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
final InetSocketAddress proxy; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
final InetSocketAddress destination; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
CacheKey(InetSocketAddress destination, InetSocketAddress proxy) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
this.proxy = proxy; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
this.destination = destination; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
81 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
82 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
83 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
public boolean equals(Object obj) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
85 |
if (obj == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
86 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
if (getClass() != obj.getClass()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
final CacheKey other = (CacheKey) obj; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
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
|
93 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
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
|
96 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
return true; |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
101 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
public int hashCode() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
return Objects.hash(proxy, destination); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
104 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
105 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
106 |
|
48083 | 107 |
ConnectionPool(long clientId) { |
108 |
this("ConnectionPool("+clientId+")"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
|
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 |
* 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
|
113 |
*/ |
48083 | 114 |
private ConnectionPool(String tag) { |
115 |
dbgTag = tag; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
116 |
plainPool = new HashMap<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
117 |
sslPool = new HashMap<>(); |
48083 | 118 |
expiryList = new ExpiryList(); |
119 |
} |
|
120 |
||
121 |
final String dbgString() { |
|
122 |
return dbgTag; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
|
56063
fb2dd3c0dd60
http-client-branch: inconsistent synchronization in ConnectionPool
chegar
parents:
55973
diff
changeset
|
125 |
synchronized void start() { |
48083 | 126 |
assert !stopped : "Already stopped"; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
128 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
static CacheKey cacheKey(InetSocketAddress destination, |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
130 |
InetSocketAddress proxy) |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
131 |
{ |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
return new CacheKey(destination, proxy); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
synchronized HttpConnection getConnection(boolean secure, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
InetSocketAddress addr, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
InetSocketAddress proxy) { |
48083 | 138 |
if (stopped) return null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
CacheKey key = new CacheKey(addr, proxy); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
HttpConnection c = secure ? findConnection(key, sslPool) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
: findConnection(key, plainPool); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
//System.out.println ("getConnection returning: " + c); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
return c; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
145 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
147 |
* Returns the connection to the pool. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
148 |
*/ |
48083 | 149 |
void returnToPool(HttpConnection conn) { |
150 |
returnToPool(conn, Instant.now(), KEEP_ALIVE); |
|
151 |
} |
|
152 |
||
153 |
// Called also by whitebox tests |
|
154 |
void returnToPool(HttpConnection conn, Instant now, long keepAlive) { |
|
155 |
||
156 |
// Don't call registerCleanupTrigger while holding a lock, |
|
157 |
// but register it before the connection is added to the pool, |
|
158 |
// since we don't want to trigger the cleanup if the connection |
|
159 |
// is not in the pool. |
|
160 |
CleanupTrigger cleanup = registerCleanupTrigger(conn); |
|
161 |
||
162 |
// it's possible that cleanup may have been called. |
|
163 |
synchronized(this) { |
|
164 |
if (cleanup.isDone()) { |
|
165 |
return; |
|
166 |
} else if (stopped) { |
|
167 |
conn.close(); |
|
168 |
return; |
|
169 |
} |
|
170 |
if (conn instanceof PlainHttpConnection) { |
|
171 |
putConnection(conn, plainPool); |
|
172 |
} else { |
|
173 |
assert conn.isSecure(); |
|
174 |
putConnection(conn, sslPool); |
|
175 |
} |
|
176 |
expiryList.add(conn, now, keepAlive); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
177 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
178 |
//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
|
179 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
|
48083 | 181 |
private CleanupTrigger registerCleanupTrigger(HttpConnection conn) { |
182 |
// Connect the connection flow to a pub/sub pair that will take the |
|
183 |
// connection out of the pool and close it if anything happens |
|
184 |
// while the connection is sitting in the pool. |
|
185 |
CleanupTrigger cleanup = new CleanupTrigger(conn); |
|
186 |
FlowTube flow = conn.getConnectionFlow(); |
|
187 |
debug.log(Level.DEBUG, "registering %s", cleanup); |
|
188 |
flow.connectFlows(cleanup, cleanup); |
|
189 |
return cleanup; |
|
190 |
} |
|
191 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
private HttpConnection |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
findConnection(CacheKey key, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
HashMap<CacheKey,LinkedList<HttpConnection>> pool) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
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
|
196 |
if (l == null || l.isEmpty()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
197 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
198 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
199 |
HttpConnection c = l.removeFirst(); |
48083 | 200 |
expiryList.remove(c); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
201 |
return c; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
202 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
203 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
204 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
205 |
/* called from cache cleaner only */ |
48083 | 206 |
private boolean |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
207 |
removeFromPool(HttpConnection c, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
208 |
HashMap<CacheKey,LinkedList<HttpConnection>> pool) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
209 |
//System.out.println("cacheCleaner removing: " + c); |
48083 | 210 |
assert Thread.holdsLock(this); |
211 |
CacheKey k = c.cacheKey(); |
|
212 |
List<HttpConnection> l = pool.get(k); |
|
213 |
if (l == null || l.isEmpty()) { |
|
214 |
pool.remove(k); |
|
215 |
return false; |
|
216 |
} |
|
217 |
return l.remove(c); |
|
42460
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 |
private void |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
putConnection(HttpConnection c, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
HashMap<CacheKey,LinkedList<HttpConnection>> pool) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
CacheKey key = c.cacheKey(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
LinkedList<HttpConnection> l = pool.get(key); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
if (l == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
l = new LinkedList<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
pool.put(key, l); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
l.add(c); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
|
48083 | 232 |
/** |
233 |
* Purge expired connection and return the number of milliseconds |
|
234 |
* in which the next connection is scheduled to expire. |
|
235 |
* If no connections are scheduled to be purged return 0. |
|
236 |
* @return the delay in milliseconds in which the next connection will |
|
237 |
* expire. |
|
238 |
*/ |
|
239 |
long purgeExpiredConnectionsAndReturnNextDeadline() { |
|
240 |
if (!expiryList.purgeMaybeRequired()) return 0; |
|
241 |
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
|
242 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
|
48083 | 244 |
// Used for whitebox testing |
245 |
long purgeExpiredConnectionsAndReturnNextDeadline(Instant now) { |
|
246 |
long nextPurge = 0; |
|
247 |
||
248 |
// We may be in the process of adding new elements |
|
249 |
// to the expiry list - but those elements will not |
|
250 |
// have outlast their keep alive timer yet since we're |
|
251 |
// just adding them. |
|
252 |
if (!expiryList.purgeMaybeRequired()) return nextPurge; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
253 |
|
48083 | 254 |
List<HttpConnection> closelist; |
255 |
synchronized (this) { |
|
256 |
closelist = expiryList.purgeUntil(now); |
|
257 |
for (HttpConnection c : closelist) { |
|
258 |
if (c instanceof PlainHttpConnection) { |
|
259 |
boolean wasPresent = removeFromPool(c, plainPool); |
|
260 |
assert wasPresent; |
|
261 |
} else { |
|
262 |
boolean wasPresent = removeFromPool(c, sslPool); |
|
263 |
assert wasPresent; |
|
264 |
} |
|
265 |
} |
|
266 |
nextPurge = now.until( |
|
267 |
expiryList.nextExpiryDeadline().orElse(now), |
|
268 |
ChronoUnit.MILLIS); |
|
47116
6160b308ed24
8187044: HttpClient ConnectionPool may spawn several concurrent CacheCleaner and prevent early GC of HttpClient.
dfuchs
parents:
44854
diff
changeset
|
269 |
} |
48083 | 270 |
closelist.forEach(this::close); |
271 |
return nextPurge; |
|
272 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
273 |
|
48083 | 274 |
private void close(HttpConnection c) { |
275 |
try { |
|
276 |
c.close(); |
|
277 |
} catch (Throwable e) {} // ignore |
|
278 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
279 |
|
48083 | 280 |
void stop() { |
281 |
List<HttpConnection> closelist = Collections.emptyList(); |
|
282 |
try { |
|
283 |
synchronized (this) { |
|
284 |
stopped = true; |
|
285 |
closelist = expiryList.stream() |
|
286 |
.map(e -> e.connection) |
|
287 |
.collect(Collectors.toList()); |
|
288 |
expiryList.clear(); |
|
289 |
plainPool.clear(); |
|
290 |
sslPool.clear(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
291 |
} |
48083 | 292 |
} finally { |
293 |
closelist.forEach(this::close); |
|
294 |
} |
|
295 |
} |
|
296 |
||
297 |
static final class ExpiryEntry { |
|
298 |
final HttpConnection connection; |
|
299 |
final Instant expiry; // absolute time in seconds of expiry time |
|
300 |
ExpiryEntry(HttpConnection connection, Instant expiry) { |
|
301 |
this.connection = connection; |
|
302 |
this.expiry = expiry; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
|
48083 | 306 |
/** |
307 |
* Manages a LinkedList of sorted ExpiryEntry. The entry with the closer |
|
308 |
* deadline is at the tail of the list, and the entry with the farther |
|
309 |
* deadline is at the head. In the most common situation, new elements |
|
310 |
* will need to be added at the head (or close to it), and expired elements |
|
311 |
* will need to be purged from the tail. |
|
312 |
*/ |
|
313 |
private static final class ExpiryList { |
|
314 |
private final LinkedList<ExpiryEntry> list = new LinkedList<>(); |
|
315 |
private volatile boolean mayContainEntries; |
|
316 |
||
317 |
// A loosely accurate boolean whose value is computed |
|
318 |
// at the end of each operation performed on ExpiryList; |
|
319 |
// Does not require synchronizing on the ConnectionPool. |
|
320 |
boolean purgeMaybeRequired() { |
|
321 |
return mayContainEntries; |
|
322 |
} |
|
323 |
||
324 |
// Returns the next expiry deadline |
|
325 |
// should only be called while holding a synchronization |
|
326 |
// lock on the ConnectionPool |
|
327 |
Optional<Instant> nextExpiryDeadline() { |
|
328 |
if (list.isEmpty()) return Optional.empty(); |
|
329 |
else return Optional.of(list.getLast().expiry); |
|
330 |
} |
|
331 |
||
332 |
// should only be called while holding a synchronization |
|
333 |
// lock on the ConnectionPool |
|
334 |
void add(HttpConnection conn) { |
|
335 |
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
|
336 |
} |
48083 | 337 |
|
338 |
// Used by whitebox test. |
|
339 |
void add(HttpConnection conn, Instant now, long keepAlive) { |
|
340 |
Instant then = now.truncatedTo(ChronoUnit.SECONDS) |
|
341 |
.plus(keepAlive, ChronoUnit.SECONDS); |
|
342 |
||
343 |
// Elements with the farther deadline are at the head of |
|
344 |
// the list. It's more likely that the new element will |
|
345 |
// have the farthest deadline, and will need to be inserted |
|
346 |
// at the head of the list, so we're using an ascending |
|
347 |
// list iterator to find the right insertion point. |
|
348 |
ListIterator<ExpiryEntry> li = list.listIterator(); |
|
349 |
while (li.hasNext()) { |
|
350 |
ExpiryEntry entry = li.next(); |
|
351 |
||
352 |
if (then.isAfter(entry.expiry)) { |
|
353 |
li.previous(); |
|
354 |
// insert here |
|
355 |
li.add(new ExpiryEntry(conn, then)); |
|
356 |
mayContainEntries = true; |
|
357 |
return; |
|
358 |
} |
|
359 |
} |
|
360 |
// last (or first) element of list (the last element is |
|
361 |
// the first when the list is empty) |
|
362 |
list.add(new ExpiryEntry(conn, then)); |
|
363 |
mayContainEntries = true; |
|
364 |
} |
|
365 |
||
366 |
// should only be called while holding a synchronization |
|
367 |
// lock on the ConnectionPool |
|
368 |
void remove(HttpConnection c) { |
|
369 |
if (c == null || list.isEmpty()) return; |
|
370 |
ListIterator<ExpiryEntry> li = list.listIterator(); |
|
371 |
while (li.hasNext()) { |
|
372 |
ExpiryEntry e = li.next(); |
|
373 |
if (e.connection.equals(c)) { |
|
374 |
li.remove(); |
|
375 |
mayContainEntries = !list.isEmpty(); |
|
376 |
return; |
|
377 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
} |
48083 | 380 |
|
381 |
// should only be called while holding a synchronization |
|
382 |
// lock on the ConnectionPool. |
|
383 |
// Purge all elements whose deadline is before now (now included). |
|
384 |
List<HttpConnection> purgeUntil(Instant now) { |
|
385 |
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
|
386 |
|
48083 | 387 |
List<HttpConnection> closelist = new ArrayList<>(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
388 |
|
48083 | 389 |
// elements with the closest deadlines are at the tail |
390 |
// of the queue, so we're going to use a descending iterator |
|
391 |
// to remove them, and stop when we find the first element |
|
392 |
// that has not expired yet. |
|
393 |
Iterator<ExpiryEntry> li = list.descendingIterator(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
394 |
while (li.hasNext()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
395 |
ExpiryEntry entry = li.next(); |
48083 | 396 |
// use !isAfter instead of isBefore in order to |
397 |
// remove the entry if its expiry == now |
|
398 |
if (!entry.expiry.isAfter(now)) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
399 |
li.remove(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
400 |
HttpConnection c = entry.connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
401 |
closelist.add(c); |
48083 | 402 |
} 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
|
403 |
} |
48083 | 404 |
mayContainEntries = !list.isEmpty(); |
405 |
return closelist; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
406 |
} |
48083 | 407 |
|
408 |
// should only be called while holding a synchronization |
|
409 |
// lock on the ConnectionPool |
|
410 |
java.util.stream.Stream<ExpiryEntry> stream() { |
|
411 |
return list.stream(); |
|
412 |
} |
|
413 |
||
414 |
// should only be called while holding a synchronization |
|
415 |
// lock on the ConnectionPool |
|
416 |
void clear() { |
|
417 |
list.clear(); |
|
418 |
mayContainEntries = false; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
419 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
420 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
421 |
|
48083 | 422 |
void cleanup(HttpConnection c, Throwable error) { |
423 |
debug.log(Level.DEBUG, |
|
424 |
"%s : ConnectionPool.cleanup(%s)", |
|
425 |
String.valueOf(c.getConnectionFlow()), |
|
426 |
error); |
|
427 |
synchronized(this) { |
|
428 |
if (c instanceof PlainHttpConnection) { |
|
429 |
removeFromPool(c, plainPool); |
|
430 |
} else { |
|
431 |
assert c.isSecure(); |
|
432 |
removeFromPool(c, sslPool); |
|
47116
6160b308ed24
8187044: HttpClient ConnectionPool may spawn several concurrent CacheCleaner and prevent early GC of HttpClient.
dfuchs
parents:
44854
diff
changeset
|
433 |
} |
48083 | 434 |
expiryList.remove(c); |
435 |
} |
|
436 |
c.close(); |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* An object that subscribes to the flow while the connection is in |
|
441 |
* the pool. Anything that comes in will cause the connection to be closed |
|
442 |
* and removed from the pool. |
|
443 |
*/ |
|
444 |
private final class CleanupTrigger implements |
|
445 |
FlowTube.TubeSubscriber, FlowTube.TubePublisher, |
|
446 |
Flow.Subscription { |
|
447 |
||
448 |
private final HttpConnection connection; |
|
449 |
private volatile boolean done; |
|
450 |
||
451 |
public CleanupTrigger(HttpConnection connection) { |
|
452 |
this.connection = connection; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
453 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
454 |
|
48083 | 455 |
public boolean isDone() { return done;} |
456 |
||
457 |
private void triggerCleanup(Throwable error) { |
|
458 |
done = true; |
|
459 |
cleanup(connection, error); |
|
460 |
} |
|
461 |
||
462 |
@Override public void request(long n) {} |
|
463 |
@Override public void cancel() {} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
464 |
|
48083 | 465 |
@Override |
466 |
public void onSubscribe(Flow.Subscription subscription) { |
|
467 |
subscription.request(1); |
|
468 |
} |
|
469 |
@Override |
|
470 |
public void onError(Throwable error) { triggerCleanup(error); } |
|
471 |
@Override |
|
472 |
public void onComplete() { triggerCleanup(null); } |
|
473 |
@Override |
|
474 |
public void onNext(List<ByteBuffer> item) { |
|
475 |
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
|
476 |
} |
48083 | 477 |
|
478 |
@Override |
|
479 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) { |
|
480 |
subscriber.onSubscribe(this); |
|
481 |
} |
|
482 |
||
483 |
@Override |
|
484 |
public String toString() { |
|
485 |
return "CleanupTrigger(" + connection.getConnectionFlow() + ")"; |
|
486 |
} |
|
487 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
488 |
} |
48083 | 489 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
490 |
} |