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