author | dfuchs |
Mon, 23 Apr 2018 15:45:40 +0100 | |
branch | http-client-branch |
changeset 56474 | fe2bf7b369b8 |
parent 56451 | 9585061fdb04 |
child 56538 | 9bdcfc7d2b9c |
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 |
49765 | 56 |
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
|
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 |
|
49765 | 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(); |
|
49765 | 187 |
if (debug.on()) debug.log("registering %s", cleanup); |
48083 | 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) { |
49765 | 423 |
if (debug.on()) |
424 |
debug.log("%s : ConnectionPool.cleanup(%s)", |
|
425 |
String.valueOf(c.getConnectionFlow()), error); |
|
48083 | 426 |
synchronized(this) { |
427 |
if (c instanceof PlainHttpConnection) { |
|
428 |
removeFromPool(c, plainPool); |
|
429 |
} else { |
|
430 |
assert c.isSecure(); |
|
431 |
removeFromPool(c, sslPool); |
|
47116
6160b308ed24
8187044: HttpClient ConnectionPool may spawn several concurrent CacheCleaner and prevent early GC of HttpClient.
dfuchs
parents:
44854
diff
changeset
|
432 |
} |
48083 | 433 |
expiryList.remove(c); |
434 |
} |
|
435 |
c.close(); |
|
436 |
} |
|
437 |
||
438 |
/** |
|
439 |
* An object that subscribes to the flow while the connection is in |
|
440 |
* the pool. Anything that comes in will cause the connection to be closed |
|
441 |
* and removed from the pool. |
|
442 |
*/ |
|
443 |
private final class CleanupTrigger implements |
|
444 |
FlowTube.TubeSubscriber, FlowTube.TubePublisher, |
|
445 |
Flow.Subscription { |
|
446 |
||
447 |
private final HttpConnection connection; |
|
448 |
private volatile boolean done; |
|
449 |
||
450 |
public CleanupTrigger(HttpConnection connection) { |
|
451 |
this.connection = connection; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
452 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
453 |
|
48083 | 454 |
public boolean isDone() { return done;} |
455 |
||
456 |
private void triggerCleanup(Throwable error) { |
|
457 |
done = true; |
|
458 |
cleanup(connection, error); |
|
459 |
} |
|
460 |
||
461 |
@Override public void request(long n) {} |
|
462 |
@Override public void cancel() {} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
463 |
|
48083 | 464 |
@Override |
465 |
public void onSubscribe(Flow.Subscription subscription) { |
|
466 |
subscription.request(1); |
|
467 |
} |
|
468 |
@Override |
|
469 |
public void onError(Throwable error) { triggerCleanup(error); } |
|
470 |
@Override |
|
471 |
public void onComplete() { triggerCleanup(null); } |
|
472 |
@Override |
|
473 |
public void onNext(List<ByteBuffer> item) { |
|
474 |
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
|
475 |
} |
48083 | 476 |
|
477 |
@Override |
|
478 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) { |
|
479 |
subscriber.onSubscribe(this); |
|
480 |
} |
|
481 |
||
482 |
@Override |
|
483 |
public String toString() { |
|
484 |
return "CleanupTrigger(" + connection.getConnectionFlow() + ")"; |
|
485 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
486 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
487 |
} |