author | chegar |
Thu, 07 Dec 2017 11:54:55 +0000 | |
branch | http-client-branch |
changeset 55973 | 4d9b002587db |
parent 55856 | 038e64c3d92a |
parent 48083 | b1c1b4ef4be2 |
child 55982 | b6ff245c0db6 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, 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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
package jdk.incubator.http; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import javax.net.ssl.SSLContext; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import javax.net.ssl.SSLParameters; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.io.IOException; |
48083 | 31 |
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
|
32 |
import java.lang.ref.WeakReference; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.net.Authenticator; |
48083 | 34 |
import java.net.CookieHandler; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.net.ProxySelector; |
48083 | 36 |
import java.nio.channels.CancelledKeyException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.nio.channels.ClosedChannelException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.nio.channels.SelectableChannel; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.nio.channels.SelectionKey; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.nio.channels.Selector; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
import java.nio.channels.SocketChannel; |
48083 | 42 |
import java.security.AccessControlContext; |
43 |
import java.security.AccessController; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
import java.security.NoSuchAlgorithmException; |
48083 | 45 |
import java.security.PrivilegedAction; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
import java.time.Instant; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
import java.time.temporal.ChronoUnit; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
import java.util.ArrayList; |
48083 | 49 |
import java.util.HashSet; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
import java.util.Iterator; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
import java.util.List; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
import java.util.Optional; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
import java.util.Set; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
import java.util.TreeSet; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
import java.util.concurrent.CompletableFuture; |
48083 | 56 |
import java.util.concurrent.ExecutionException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
import java.util.concurrent.Executor; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
import java.util.concurrent.Executors; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
import java.util.concurrent.ThreadFactory; |
48083 | 60 |
import java.util.concurrent.atomic.AtomicInteger; |
61 |
import java.util.concurrent.atomic.AtomicLong; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
import java.util.stream.Stream; |
48083 | 63 |
import jdk.incubator.http.HttpResponse.BodyHandler; |
64 |
import jdk.incubator.http.HttpResponse.MultiSubscriber; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
import jdk.incubator.http.internal.common.Log; |
48083 | 66 |
import jdk.incubator.http.internal.common.Pair; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
import jdk.incubator.http.internal.common.Utils; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
68 |
import jdk.incubator.http.internal.websocket.BuilderImpl; |
48083 | 69 |
import jdk.internal.misc.InnocuousThread; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
* Client implementation. Contains all configuration information and also |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
* the selector manager thread which allows async events to be registered |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
* and delivered when they occur. See AsyncEvent. |
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 |
class HttpClientImpl extends HttpClient { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
|
48083 | 78 |
static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag. |
79 |
static final boolean DEBUGELAPSED = Utils.TESTING || DEBUG; // Revisit: temporary dev flag. |
|
80 |
static final boolean DEBUGTIMEOUT = false; // Revisit: temporary dev flag. |
|
81 |
final System.Logger debug = Utils.getDebugLogger(this::dbgString, DEBUG); |
|
82 |
final System.Logger debugelapsed = Utils.getDebugLogger(this::dbgString, DEBUGELAPSED); |
|
83 |
final System.Logger debugtimeout = Utils.getDebugLogger(this::dbgString, DEBUGTIMEOUT); |
|
84 |
static final AtomicLong CLIENT_IDS = new AtomicLong(); |
|
85 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
86 |
// Define the default factory as a static inner class |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
// that embeds all the necessary logic to avoid |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
// the risk of using a lambda that might keep a reference on the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
// HttpClient instance from which it was created (helps with |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
// heapdump analysis). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
private static final class DefaultThreadFactory implements ThreadFactory { |
48083 | 92 |
private final String namePrefix; |
93 |
private final AtomicInteger nextId = new AtomicInteger(); |
|
94 |
||
95 |
DefaultThreadFactory(long clientID) { |
|
96 |
namePrefix = "HttpClient-" + clientID + "-Worker-"; |
|
97 |
} |
|
98 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
99 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
100 |
public Thread newThread(Runnable r) { |
48083 | 101 |
String name = namePrefix + nextId.getAndIncrement(); |
102 |
Thread t; |
|
103 |
if (System.getSecurityManager() == null) { |
|
104 |
t = new Thread(null, r, name, 0, false); |
|
105 |
} else { |
|
106 |
t = InnocuousThread.newThread(name, r); |
|
107 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
108 |
t.setDaemon(true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
return t; |
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 |
|
48083 | 113 |
private final CookieHandler cookieHandler; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
private final Redirect followRedirects; |
48083 | 115 |
private final Optional<ProxySelector> userProxySelector; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
116 |
private final ProxySelector proxySelector; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
117 |
private final Authenticator authenticator; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
118 |
private final Version version; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
private final ConnectionPool connections; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
private final Executor executor; |
48083 | 121 |
private final boolean isDefaultExecutor; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
// Security parameters |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
private final SSLContext sslContext; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
private final SSLParameters sslParams; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
private final SelectorManager selmgr; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
private final FilterFactory filters; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
private final Http2ClientImpl client2; |
48083 | 128 |
private final long id; |
129 |
private final String dbgTag; |
|
130 |
||
131 |
// This reference is used to keep track of the facade HttpClient |
|
132 |
// that was returned to the application code. |
|
133 |
// It makes it possible to know when the application no longer |
|
134 |
// holds any reference to the HttpClient. |
|
135 |
// Unfortunately, this information is not enough to know when |
|
136 |
// to exit the SelectorManager thread. Because of the asynchronous |
|
137 |
// nature of the API, we also need to wait until all pending operations |
|
138 |
// have completed. |
|
139 |
private final WeakReference<HttpClientFacade> facadeRef; |
|
140 |
||
141 |
// This counter keeps track of the number of operations pending |
|
142 |
// on the HttpClient. The SelectorManager thread will wait |
|
143 |
// until there are no longer any pending operations and the |
|
144 |
// facadeRef is cleared before exiting. |
|
145 |
// |
|
146 |
// The pendingOperationCount is incremented every time a send/sendAsync |
|
147 |
// operation is invoked on the HttpClient, and is decremented when |
|
148 |
// the HttpResponse<T> object is returned to the user. |
|
149 |
// However, at this point, the body may not have been fully read yet. |
|
150 |
// This is the case when the response T is implemented as a streaming |
|
151 |
// subscriber (such as an InputStream). |
|
152 |
// |
|
153 |
// To take care of this issue the pendingOperationCount will additionally |
|
154 |
// be incremented/decremented in the following cases: |
|
155 |
// |
|
156 |
// 1. For HTTP/2 it is incremented when a stream is added to the |
|
157 |
// Http2Connection streams map, and decreased when the stream is removed |
|
158 |
// from the map. This should also take care of push promises. |
|
159 |
// 2. For WebSocket the count is increased when creating a |
|
160 |
// DetachedConnectionChannel for the socket, and decreased |
|
161 |
// when the the channel is closed. |
|
162 |
// In addition, the HttpClient facade is passed to the WebSocket builder, |
|
163 |
// (instead of the client implementation delegate). |
|
164 |
// 3. For HTTP/1.1 the count is incremented before starting to parse the body |
|
165 |
// response, and decremented when the parser has reached the end of the |
|
166 |
// response body flow. |
|
167 |
// |
|
168 |
// This should ensure that the selector manager thread remains alive until |
|
169 |
// the response has been fully received or the web socket is closed. |
|
170 |
private final AtomicLong pendingOperationCount = new AtomicLong(); |
|
171 |
private final AtomicLong pendingWebSocketCount = new AtomicLong(); |
|
172 |
private final AtomicLong pendingHttpRequestCount = new AtomicLong(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
174 |
/** A Set of, deadline first, ordered timeout events. */ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
175 |
private final TreeSet<TimeoutEvent> timeouts; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
|
48083 | 177 |
/** |
178 |
* This is a bit tricky: |
|
179 |
* 1. an HttpClientFacade has a final HttpClientImpl field. |
|
180 |
* 2. an HttpClientImpl has a final WeakReference<HttpClientFacade> field, |
|
181 |
* where the referent is the facade created for that instance. |
|
182 |
* 3. We cannot just create the HttpClientFacade in the HttpClientImpl |
|
183 |
* constructor, because it would be only weakly referenced and could |
|
184 |
* be GC'ed before we can return it. |
|
185 |
* The solution is to use an instance of SingleFacadeFactory which will |
|
186 |
* allow the caller of new HttpClientImpl(...) to retrieve the facade |
|
187 |
* after the HttpClientImpl has been created. |
|
188 |
*/ |
|
189 |
private static final class SingleFacadeFactory { |
|
190 |
HttpClientFacade facade; |
|
191 |
HttpClientFacade createFacade(HttpClientImpl impl) { |
|
192 |
assert facade == null; |
|
193 |
return (facade = new HttpClientFacade(impl)); |
|
194 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
196 |
|
48083 | 197 |
static HttpClientFacade create(HttpClientBuilderImpl builder) { |
198 |
SingleFacadeFactory facadeFactory = new SingleFacadeFactory(); |
|
199 |
HttpClientImpl impl = new HttpClientImpl(builder, facadeFactory); |
|
200 |
impl.start(); |
|
201 |
assert facadeFactory.facade != null; |
|
202 |
assert impl.facadeRef.get() == facadeFactory.facade; |
|
203 |
return facadeFactory.facade; |
|
204 |
} |
|
205 |
||
206 |
private HttpClientImpl(HttpClientBuilderImpl builder, |
|
207 |
SingleFacadeFactory facadeFactory) { |
|
208 |
id = CLIENT_IDS.incrementAndGet(); |
|
209 |
dbgTag = "HttpClientImpl(" + id +")"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
210 |
if (builder.sslContext == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
211 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
212 |
sslContext = SSLContext.getDefault(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
213 |
} catch (NoSuchAlgorithmException ex) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
214 |
throw new InternalError(ex); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
215 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
sslContext = builder.sslContext; |
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 |
Executor ex = builder.executor; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
220 |
if (ex == null) { |
48083 | 221 |
ex = Executors.newCachedThreadPool(new DefaultThreadFactory(id)); |
222 |
isDefaultExecutor = true; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
ex = builder.executor; |
48083 | 225 |
isDefaultExecutor = false; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
} |
48083 | 227 |
facadeRef = new WeakReference<>(facadeFactory.createFacade(this)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
client2 = new Http2ClientImpl(this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
executor = ex; |
48083 | 230 |
cookieHandler = builder.cookieHandler; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
followRedirects = builder.followRedirects == null ? |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
Redirect.NEVER : builder.followRedirects; |
48083 | 233 |
this.userProxySelector = Optional.ofNullable(builder.proxy); |
234 |
this.proxySelector = userProxySelector |
|
235 |
.orElseGet(HttpClientImpl::getDefaultProxySelector); |
|
236 |
debug.log(Level.DEBUG, "proxySelector is %s (user-supplied=%s)", |
|
237 |
this.proxySelector, userProxySelector.isPresent()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
authenticator = builder.authenticator; |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
44639
diff
changeset
|
239 |
if (builder.version == null) { |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
44639
diff
changeset
|
240 |
version = HttpClient.Version.HTTP_2; |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
44639
diff
changeset
|
241 |
} else { |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
44639
diff
changeset
|
242 |
version = builder.version; |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
44639
diff
changeset
|
243 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
if (builder.sslParams == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
sslParams = getDefaultParams(sslContext); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
sslParams = builder.sslParams; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
} |
48083 | 249 |
connections = new ConnectionPool(id); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
250 |
connections.start(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
251 |
timeouts = new TreeSet<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
252 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
253 |
selmgr = new SelectorManager(this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
254 |
} catch (IOException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
255 |
// unlikely |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
256 |
throw new InternalError(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
257 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
258 |
selmgr.setDaemon(true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
259 |
filters = new FilterFactory(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
260 |
initFilters(); |
48083 | 261 |
assert facadeRef.get() != null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
262 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
263 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
private void start() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
selmgr.start(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
267 |
|
48083 | 268 |
// Called from the SelectorManager thread, just before exiting. |
269 |
// Clears the HTTP/1.1 and HTTP/2 cache, ensuring that the connections |
|
270 |
// that may be still lingering there are properly closed (and their |
|
271 |
// possibly still opened SocketChannel released). |
|
272 |
private void stop() { |
|
273 |
// Clears HTTP/1.1 cache and close its connections |
|
274 |
connections.stop(); |
|
275 |
// Clears HTTP/2 cache and close its connections. |
|
276 |
client2.stop(); |
|
277 |
} |
|
278 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
279 |
private static SSLParameters getDefaultParams(SSLContext ctx) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
280 |
SSLParameters params = ctx.getSupportedSSLParameters(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
281 |
params.setProtocols(new String[]{"TLSv1.2"}); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
282 |
return params; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
283 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
|
48083 | 285 |
private static ProxySelector getDefaultProxySelector() { |
286 |
PrivilegedAction<ProxySelector> action = ProxySelector::getDefault; |
|
287 |
return AccessController.doPrivileged(action); |
|
288 |
} |
|
289 |
||
290 |
// Returns the facade that was returned to the application code. |
|
291 |
// May be null if that facade is no longer referenced. |
|
292 |
final HttpClientFacade facade() { |
|
293 |
return facadeRef.get(); |
|
294 |
} |
|
295 |
||
296 |
// Increments the pendingOperationCount. |
|
297 |
final long reference() { |
|
298 |
pendingHttpRequestCount.incrementAndGet(); |
|
299 |
return pendingOperationCount.incrementAndGet(); |
|
300 |
} |
|
301 |
||
302 |
// Decrements the pendingOperationCount. |
|
303 |
final long unreference() { |
|
304 |
final long count = pendingOperationCount.decrementAndGet(); |
|
305 |
final long httpCount = pendingHttpRequestCount.decrementAndGet(); |
|
306 |
final long webSocketCount = pendingWebSocketCount.get(); |
|
307 |
if (count == 0 && facade() == null) { |
|
308 |
selmgr.wakeupSelector(); |
|
309 |
} |
|
310 |
assert httpCount >= 0 : "count of HTTP operations < 0"; |
|
311 |
assert webSocketCount >= 0 : "count of WS operations < 0"; |
|
312 |
assert count >= 0 : "count of pending operations < 0"; |
|
313 |
return count; |
|
314 |
} |
|
315 |
||
316 |
// Increments the pendingOperationCount. |
|
317 |
final long webSocketOpen() { |
|
318 |
pendingWebSocketCount.incrementAndGet(); |
|
319 |
return pendingOperationCount.incrementAndGet(); |
|
320 |
} |
|
321 |
||
322 |
// Decrements the pendingOperationCount. |
|
323 |
final long webSocketClose() { |
|
324 |
final long count = pendingOperationCount.decrementAndGet(); |
|
325 |
final long webSocketCount = pendingWebSocketCount.decrementAndGet(); |
|
326 |
final long httpCount = pendingHttpRequestCount.get(); |
|
327 |
if (count == 0 && facade() == null) { |
|
328 |
selmgr.wakeupSelector(); |
|
329 |
} |
|
330 |
assert httpCount >= 0 : "count of HTTP operations < 0"; |
|
331 |
assert webSocketCount >= 0 : "count of WS operations < 0"; |
|
332 |
assert count >= 0 : "count of pending operations < 0"; |
|
333 |
return count; |
|
334 |
} |
|
335 |
||
336 |
// Returns the pendingOperationCount. |
|
337 |
final long referenceCount() { |
|
338 |
return pendingOperationCount.get(); |
|
339 |
} |
|
340 |
||
341 |
// Called by the SelectorManager thread to figure out whether it's time |
|
342 |
// to terminate. |
|
343 |
final boolean isReferenced() { |
|
344 |
HttpClient facade = facade(); |
|
345 |
return facade != null || referenceCount() > 0; |
|
346 |
} |
|
347 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
/** |
48083 | 349 |
* Wait for activity on given exchange. |
350 |
* The following occurs in the SelectorManager thread. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
* |
48083 | 352 |
* 1) add to selector |
353 |
* 2) If selector fires for this exchange then |
|
354 |
* call AsyncEvent.handle() |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
355 |
* |
48083 | 356 |
* If exchange needs to change interest ops, then call registerEvent() again. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
357 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
void registerEvent(AsyncEvent exchange) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
selmgr.register(exchange); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
* Only used from RawChannel to disconnect the channel from |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
* the selector |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
void cancelRegistration(SocketChannel s) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
selmgr.cancel(s); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
|
48083 | 370 |
/** |
371 |
* Allows an AsyncEvent to modify its interestOps. |
|
372 |
* @param event The modified event. |
|
373 |
*/ |
|
374 |
void eventUpdated(AsyncEvent event) throws ClosedChannelException { |
|
375 |
assert !(event instanceof AsyncTriggerEvent); |
|
376 |
selmgr.eventUpdated(event); |
|
377 |
} |
|
378 |
||
379 |
boolean isSelectorThread() { |
|
380 |
return Thread.currentThread() == selmgr; |
|
381 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
382 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
383 |
Http2ClientImpl client2() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
384 |
return client2; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
385 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
386 |
|
48083 | 387 |
private void debugCompleted(String tag, long startNanos, HttpRequest req) { |
388 |
if (debugelapsed.isLoggable(Level.DEBUG)) { |
|
389 |
debugelapsed.log(Level.DEBUG, () -> tag + " elapsed " |
|
390 |
+ (System.nanoTime() - startNanos)/1000_000L |
|
391 |
+ " millis for " + req.method() |
|
392 |
+ " to " + req.uri()); |
|
393 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
394 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
395 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
397 |
public <T> HttpResponse<T> |
48083 | 398 |
send(HttpRequest req, BodyHandler<T> responseHandler) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
399 |
throws IOException, InterruptedException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
400 |
{ |
48083 | 401 |
try { |
402 |
return sendAsync(req, responseHandler).get(); |
|
403 |
} catch (ExecutionException e) { |
|
404 |
Throwable t = e.getCause(); |
|
405 |
if (t instanceof Error) |
|
406 |
throw (Error)t; |
|
407 |
if (t instanceof RuntimeException) |
|
408 |
throw (RuntimeException)t; |
|
409 |
else if (t instanceof IOException) |
|
410 |
throw Utils.getIOException(t); |
|
411 |
else |
|
412 |
throw new InternalError("Unexpected exception", t); |
|
413 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
414 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
415 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
416 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
417 |
public <T> CompletableFuture<HttpResponse<T>> |
48083 | 418 |
sendAsync(HttpRequest userRequest, BodyHandler<T> responseHandler) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
419 |
{ |
48083 | 420 |
AccessControlContext acc = null; |
421 |
if (System.getSecurityManager() != null) |
|
422 |
acc = AccessController.getContext(); |
|
423 |
||
424 |
// Clone the, possibly untrusted, HttpRequest |
|
425 |
HttpRequestImpl requestImpl = new HttpRequestImpl(userRequest, proxySelector, acc); |
|
426 |
if (requestImpl.method().equals("CONNECT")) |
|
427 |
throw new IllegalArgumentException("Unsupported method CONNECT"); |
|
428 |
||
429 |
long start = DEBUGELAPSED ? System.nanoTime() : 0; |
|
430 |
reference(); |
|
431 |
try { |
|
432 |
debugelapsed.log(Level.DEBUG, "ClientImpl (async) send %s", userRequest); |
|
433 |
||
434 |
MultiExchange<Void,T> mex = new MultiExchange<>(userRequest, |
|
435 |
requestImpl, |
|
436 |
this, |
|
437 |
responseHandler, |
|
438 |
acc); |
|
439 |
CompletableFuture<HttpResponse<T>> res = |
|
440 |
mex.responseAsync().whenComplete((b,t) -> unreference()); |
|
441 |
if (DEBUGELAPSED) { |
|
442 |
res = res.whenComplete( |
|
443 |
(b,t) -> debugCompleted("ClientImpl (async)", start, userRequest)); |
|
444 |
} |
|
445 |
// makes sure that any dependent actions happen in the executor |
|
446 |
if (acc != null) { |
|
447 |
res.whenCompleteAsync((r, t) -> { /* do nothing */}, |
|
448 |
new PrivilegedExecutor(executor, acc)); |
|
449 |
} |
|
450 |
||
451 |
return res; |
|
452 |
} catch(Throwable t) { |
|
453 |
unreference(); |
|
454 |
debugCompleted("ClientImpl (async)", start, userRequest); |
|
455 |
throw t; |
|
456 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
457 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
458 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
459 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
460 |
public <U, T> CompletableFuture<U> |
48083 | 461 |
sendAsync(HttpRequest userRequest, MultiSubscriber<U, T> responseHandler) { |
462 |
AccessControlContext acc = null; |
|
463 |
if (System.getSecurityManager() != null) |
|
464 |
acc = AccessController.getContext(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
465 |
|
48083 | 466 |
// Clone the, possibly untrusted, HttpRequest |
467 |
HttpRequestImpl requestImpl = new HttpRequestImpl(userRequest, proxySelector, acc); |
|
468 |
if (requestImpl.method().equals("CONNECT")) |
|
469 |
throw new IllegalArgumentException("Unsupported method CONNECT"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
470 |
|
48083 | 471 |
long start = DEBUGELAPSED ? System.nanoTime() : 0; |
472 |
reference(); |
|
473 |
try { |
|
474 |
debugelapsed.log(Level.DEBUG, "ClientImpl (async) send multi %s", userRequest); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
475 |
|
48083 | 476 |
MultiExchange<U,T> mex = new MultiExchange<>(userRequest, |
477 |
requestImpl, |
|
478 |
this, |
|
479 |
responseHandler, |
|
480 |
acc); |
|
481 |
CompletableFuture<U> res = mex.multiResponseAsync() |
|
482 |
.whenComplete((b,t) -> unreference()); |
|
483 |
if (DEBUGELAPSED) { |
|
484 |
res = res.whenComplete( |
|
485 |
(b,t) -> debugCompleted("ClientImpl (async)", start, userRequest)); |
|
486 |
} |
|
487 |
// makes sure that any dependent actions happen in the executor |
|
488 |
if (acc != null) { |
|
489 |
res.whenCompleteAsync((r, t) -> { /* do nothing */}, |
|
490 |
new PrivilegedExecutor(executor, acc)); |
|
491 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
492 |
|
48083 | 493 |
return res; |
494 |
} catch(Throwable t) { |
|
495 |
unreference(); |
|
496 |
debugCompleted("ClientImpl (async)", start, userRequest); |
|
497 |
throw t; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
498 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
499 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
500 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
501 |
// Main loop for this client's selector |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
502 |
private final static class SelectorManager extends Thread { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
503 |
|
48083 | 504 |
// For testing purposes we have an internal System property that |
505 |
// can control the frequency at which the selector manager will wake |
|
506 |
// up when there are no pending operations. |
|
507 |
// Increasing the frequency (shorter delays) might allow the selector |
|
508 |
// to observe that the facade is no longer referenced and might allow |
|
509 |
// the selector thread to terminate more timely - for when nothing is |
|
510 |
// ongoing it will only check for that condition every NODEADLINE ms. |
|
511 |
// To avoid misuse of the property, the delay that can be specified |
|
512 |
// is comprised between [MIN_NODEADLINE, MAX_NODEADLINE], and its default |
|
513 |
// value if unspecified (or <= 0) is DEF_NODEADLINE = 3000ms |
|
514 |
// The property is -Djdk.httpclient.internal.selector.timeout=<millis> |
|
515 |
private static final int MIN_NODEADLINE = 1000; // ms |
|
516 |
private static final int MAX_NODEADLINE = 1000 * 1200; // ms |
|
517 |
private static final int DEF_NODEADLINE = 3000; // ms |
|
518 |
private static final long NODEADLINE; // default is DEF_NODEADLINE ms |
|
519 |
static { |
|
520 |
// ensure NODEADLINE is initialized with some valid value. |
|
521 |
long deadline = Utils.getIntegerNetProperty( |
|
522 |
"jdk.httpclient.internal.selector.timeout", |
|
523 |
DEF_NODEADLINE); // millis |
|
524 |
if (deadline <= 0) deadline = DEF_NODEADLINE; |
|
525 |
deadline = Math.max(deadline, MIN_NODEADLINE); |
|
526 |
NODEADLINE = Math.min(deadline, MAX_NODEADLINE); |
|
527 |
} |
|
528 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
529 |
private final Selector selector; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
530 |
private volatile boolean closed; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
531 |
private final List<AsyncEvent> registrations; |
48083 | 532 |
private final System.Logger debug; |
533 |
private final System.Logger debugtimeout; |
|
534 |
HttpClientImpl owner; |
|
535 |
ConnectionPool pool; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
536 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
537 |
SelectorManager(HttpClientImpl ref) throws IOException { |
48083 | 538 |
super(null, null, "HttpClient-" + ref.id + "-SelectorManager", 0, false); |
539 |
owner = ref; |
|
540 |
debug = ref.debug; |
|
541 |
debugtimeout = ref.debugtimeout; |
|
542 |
pool = ref.connectionPool(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
543 |
registrations = new ArrayList<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
544 |
selector = Selector.open(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
545 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
546 |
|
48083 | 547 |
void eventUpdated(AsyncEvent e) throws ClosedChannelException { |
548 |
if (Thread.currentThread() == this) { |
|
549 |
SelectionKey key = e.channel().keyFor(selector); |
|
550 |
SelectorAttachment sa = (SelectorAttachment) key.attachment(); |
|
551 |
if (sa != null) sa.register(e); |
|
552 |
} else { |
|
553 |
register(e); |
|
554 |
} |
|
555 |
} |
|
556 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
557 |
// This returns immediately. So caller not allowed to send/receive |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
558 |
// on connection. |
48083 | 559 |
synchronized void register(AsyncEvent e) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
560 |
registrations.add(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
561 |
selector.wakeup(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
562 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
563 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
564 |
synchronized void cancel(SocketChannel e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
565 |
SelectionKey key = e.keyFor(selector); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
566 |
if (key != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
567 |
key.cancel(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
568 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
569 |
selector.wakeup(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
570 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
571 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
572 |
void wakeupSelector() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
573 |
selector.wakeup(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
574 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
575 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
576 |
synchronized void shutdown() { |
48083 | 577 |
debug.log(Level.DEBUG, "SelectorManager shutting down"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
578 |
closed = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
579 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
580 |
selector.close(); |
48083 | 581 |
} catch (IOException ignored) { |
582 |
} finally { |
|
583 |
owner.stop(); |
|
584 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
585 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
586 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
587 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
588 |
public void run() { |
48083 | 589 |
List<Pair<AsyncEvent,IOException>> errorList = new ArrayList<>(); |
590 |
List<AsyncEvent> readyList = new ArrayList<>(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
591 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
592 |
while (!Thread.currentThread().isInterrupted()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
593 |
synchronized (this) { |
48083 | 594 |
assert errorList.isEmpty(); |
595 |
assert readyList.isEmpty(); |
|
596 |
for (AsyncEvent event : registrations) { |
|
597 |
if (event instanceof AsyncTriggerEvent) { |
|
598 |
readyList.add(event); |
|
599 |
continue; |
|
600 |
} |
|
601 |
SelectableChannel chan = event.channel(); |
|
602 |
SelectionKey key = null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
603 |
try { |
48083 | 604 |
key = chan.keyFor(selector); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
605 |
SelectorAttachment sa; |
45531
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
606 |
if (key == null || !key.isValid()) { |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
607 |
if (key != null) { |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
608 |
// key is canceled. |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
609 |
// invoke selectNow() to purge it |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
610 |
// before registering the new event. |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
611 |
selector.selectNow(); |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
612 |
} |
48083 | 613 |
sa = new SelectorAttachment(chan, selector); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
614 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
615 |
sa = (SelectorAttachment) key.attachment(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
616 |
} |
48083 | 617 |
// may throw IOE if channel closed: that's OK |
618 |
sa.register(event); |
|
619 |
if (!chan.isOpen()) { |
|
620 |
throw new IOException("Channel closed"); |
|
621 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
622 |
} catch (IOException e) { |
48083 | 623 |
Log.logTrace("HttpClientImpl: " + e); |
624 |
debug.log(Level.DEBUG, () -> |
|
625 |
"Got " + e.getClass().getName() |
|
626 |
+ " while handling" |
|
627 |
+ " registration events"); |
|
628 |
chan.close(); |
|
629 |
// let the event abort deal with it |
|
630 |
errorList.add(new Pair<>(event, e)); |
|
631 |
if (key != null) { |
|
632 |
key.cancel(); |
|
633 |
selector.selectNow(); |
|
634 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
635 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
636 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
637 |
registrations.clear(); |
48083 | 638 |
selector.selectedKeys().clear(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
639 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
640 |
|
48083 | 641 |
for (AsyncEvent event : readyList) { |
642 |
assert event instanceof AsyncTriggerEvent; |
|
643 |
event.handle(); |
|
644 |
} |
|
645 |
readyList.clear(); |
|
646 |
||
647 |
for (Pair<AsyncEvent,IOException> error : errorList) { |
|
648 |
// an IOException was raised and the channel closed. |
|
649 |
handleEvent(error.first, error.second); |
|
650 |
} |
|
651 |
errorList.clear(); |
|
652 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
653 |
// Check whether client is still alive, and if not, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
654 |
// gracefully stop this thread |
48083 | 655 |
if (!owner.isReferenced()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
656 |
Log.logTrace("HttpClient no longer referenced. Exiting..."); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
657 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
658 |
} |
48083 | 659 |
|
660 |
// Timeouts will have milliseconds granularity. It is important |
|
661 |
// to handle them in a timely fashion. |
|
662 |
long nextTimeout = owner.purgeTimeoutsAndReturnNextDeadline(); |
|
663 |
debugtimeout.log(Level.DEBUG, "next timeout: %d", nextTimeout); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
664 |
|
48083 | 665 |
// Keep-alive have seconds granularity. It's not really an |
666 |
// issue if we keep connections linger a bit more in the keep |
|
667 |
// alive cache. |
|
668 |
long nextExpiry = pool.purgeExpiredConnectionsAndReturnNextDeadline(); |
|
669 |
debugtimeout.log(Level.DEBUG, "next expired: %d", nextExpiry); |
|
670 |
||
671 |
assert nextTimeout >= 0; |
|
672 |
assert nextExpiry >= 0; |
|
673 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
674 |
// Don't wait for ever as it might prevent the thread to |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
675 |
// stop gracefully. millis will be 0 if no deadline was found. |
48083 | 676 |
if (nextTimeout <= 0) nextTimeout = NODEADLINE; |
677 |
||
678 |
// Clip nextExpiry at NODEADLINE limit. The default |
|
679 |
// keep alive is 1200 seconds (half an hour) - we don't |
|
680 |
// want to wait that long. |
|
681 |
if (nextExpiry <= 0) nextExpiry = NODEADLINE; |
|
682 |
else nextExpiry = Math.min(NODEADLINE, nextExpiry); |
|
683 |
||
684 |
// takes the least of the two. |
|
685 |
long millis = Math.min(nextExpiry, nextTimeout); |
|
686 |
||
687 |
debugtimeout.log(Level.DEBUG, "Next deadline is %d", |
|
688 |
(millis == 0 ? NODEADLINE : millis)); |
|
689 |
//debugPrint(selector); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
690 |
int n = selector.select(millis == 0 ? NODEADLINE : millis); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
691 |
if (n == 0) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
692 |
// Check whether client is still alive, and if not, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
693 |
// gracefully stop this thread |
48083 | 694 |
if (!owner.isReferenced()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
695 |
Log.logTrace("HttpClient no longer referenced. Exiting..."); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
696 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
697 |
} |
48083 | 698 |
owner.purgeTimeoutsAndReturnNextDeadline(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
699 |
continue; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
700 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
701 |
Set<SelectionKey> keys = selector.selectedKeys(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
702 |
|
48083 | 703 |
assert errorList.isEmpty(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
704 |
for (SelectionKey key : keys) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
705 |
SelectorAttachment sa = (SelectorAttachment) key.attachment(); |
48083 | 706 |
if (!key.isValid()) { |
707 |
IOException ex = sa.chan.isOpen() |
|
708 |
? new IOException("Invalid key") |
|
709 |
: new ClosedChannelException(); |
|
710 |
sa.pending.forEach(e -> errorList.add(new Pair<>(e,ex))); |
|
711 |
sa.pending.clear(); |
|
712 |
continue; |
|
713 |
} |
|
714 |
||
715 |
int eventsOccurred; |
|
716 |
try { |
|
717 |
eventsOccurred = key.readyOps(); |
|
718 |
} catch (CancelledKeyException ex) { |
|
719 |
IOException io = Utils.getIOException(ex); |
|
720 |
sa.pending.forEach(e -> errorList.add(new Pair<>(e,io))); |
|
721 |
sa.pending.clear(); |
|
722 |
continue; |
|
723 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
724 |
sa.events(eventsOccurred).forEach(readyList::add); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
725 |
sa.resetInterestOps(eventsOccurred); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
726 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
727 |
selector.selectNow(); // complete cancellation |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
728 |
selector.selectedKeys().clear(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
729 |
|
48083 | 730 |
for (AsyncEvent event : readyList) { |
731 |
handleEvent(event, null); // will be delegated to executor |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
732 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
733 |
readyList.clear(); |
48083 | 734 |
errorList.forEach((p) -> handleEvent(p.first, p.second)); |
735 |
errorList.clear(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
736 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
737 |
} catch (Throwable e) { |
48083 | 738 |
//e.printStackTrace(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
739 |
if (!closed) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
740 |
// This terminates thread. So, better just print stack trace |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
741 |
String err = Utils.stackTrace(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
742 |
Log.logError("HttpClientImpl: fatal error: " + err); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
743 |
} |
48083 | 744 |
debug.log(Level.DEBUG, "shutting down", e); |
745 |
if (Utils.ASSERTIONSENABLED && !debug.isLoggable(Level.DEBUG)) { |
|
746 |
e.printStackTrace(System.err); // always print the stack |
|
747 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
748 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
749 |
shutdown(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
750 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
751 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
752 |
|
48083 | 753 |
// void debugPrint(Selector selector) { |
754 |
// System.err.println("Selector: debugprint start"); |
|
755 |
// Set<SelectionKey> keys = selector.keys(); |
|
756 |
// for (SelectionKey key : keys) { |
|
757 |
// SelectableChannel c = key.channel(); |
|
758 |
// int ops = key.interestOps(); |
|
759 |
// System.err.printf("selector chan:%s ops:%d\n", c, ops); |
|
760 |
// } |
|
761 |
// System.err.println("Selector: debugprint end"); |
|
762 |
// } |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
763 |
|
48083 | 764 |
/** Handles the given event. The given ioe may be null. */ |
765 |
void handleEvent(AsyncEvent event, IOException ioe) { |
|
766 |
if (closed || ioe != null) { |
|
767 |
event.abort(ioe); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
768 |
} else { |
48083 | 769 |
event.handle(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
770 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
771 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
772 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
773 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
774 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
775 |
* Tracks multiple user level registrations associated with one NIO |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
776 |
* registration (SelectionKey). In this implementation, registrations |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
777 |
* are one-off and when an event is posted the registration is cancelled |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
778 |
* until explicitly registered again. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
779 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
780 |
* <p> No external synchronization required as this class is only used |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
781 |
* by the SelectorManager thread. One of these objects required per |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
782 |
* connection. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
783 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
784 |
private static class SelectorAttachment { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
785 |
private final SelectableChannel chan; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
786 |
private final Selector selector; |
48083 | 787 |
private final Set<AsyncEvent> pending; |
788 |
private final static System.Logger debug = |
|
789 |
Utils.getDebugLogger("SelectorAttachment"::toString, DEBUG); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
790 |
private int interestOps; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
791 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
792 |
SelectorAttachment(SelectableChannel chan, Selector selector) { |
48083 | 793 |
this.pending = new HashSet<>(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
794 |
this.chan = chan; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
795 |
this.selector = selector; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
796 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
797 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
798 |
void register(AsyncEvent e) throws ClosedChannelException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
799 |
int newOps = e.interestOps(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
800 |
boolean reRegister = (interestOps & newOps) != newOps; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
801 |
interestOps |= newOps; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
802 |
pending.add(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
803 |
if (reRegister) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
804 |
// first time registration happens here also |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
805 |
chan.register(selector, interestOps, this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
806 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
807 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
808 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
809 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
810 |
* Returns a Stream<AsyncEvents> containing only events that are |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
811 |
* registered with the given {@code interestOps}. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
812 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
813 |
Stream<AsyncEvent> events(int interestOps) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
814 |
return pending.stream() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
815 |
.filter(ev -> (ev.interestOps() & interestOps) != 0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
816 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
817 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
818 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
819 |
* Removes any events with the given {@code interestOps}, and if no |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
820 |
* events remaining, cancels the associated SelectionKey. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
821 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
822 |
void resetInterestOps(int interestOps) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
823 |
int newOps = 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
824 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
825 |
Iterator<AsyncEvent> itr = pending.iterator(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
826 |
while (itr.hasNext()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
827 |
AsyncEvent event = itr.next(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
828 |
int evops = event.interestOps(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
829 |
if (event.repeating()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
830 |
newOps |= evops; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
831 |
continue; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
832 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
833 |
if ((evops & interestOps) != 0) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
834 |
itr.remove(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
835 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
836 |
newOps |= evops; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
837 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
838 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
839 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
840 |
this.interestOps = newOps; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
841 |
SelectionKey key = chan.keyFor(selector); |
48083 | 842 |
if (newOps == 0 && pending.isEmpty()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
843 |
key.cancel(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
844 |
} else { |
48083 | 845 |
try { |
846 |
key.interestOps(newOps); |
|
847 |
} catch (CancelledKeyException x) { |
|
848 |
// channel may have been closed |
|
849 |
debug.log(Level.DEBUG, "key cancelled for " + chan); |
|
850 |
abortPending(x); |
|
851 |
} |
|
852 |
} |
|
853 |
} |
|
854 |
||
855 |
void abortPending(Throwable x) { |
|
856 |
if (!pending.isEmpty()) { |
|
857 |
AsyncEvent[] evts = pending.toArray(new AsyncEvent[0]); |
|
858 |
pending.clear(); |
|
859 |
IOException io = Utils.getIOException(x); |
|
860 |
for (AsyncEvent event : evts) { |
|
861 |
event.abort(io); |
|
862 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
863 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
864 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
865 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
866 |
|
48083 | 867 |
/*package-private*/ SSLContext theSSLContext() { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
868 |
return sslContext; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
869 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
870 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
871 |
@Override |
48083 | 872 |
public SSLContext sslContext() { |
873 |
return sslContext; |
|
874 |
} |
|
875 |
||
876 |
@Override |
|
877 |
public SSLParameters sslParameters() { |
|
878 |
return Utils.copySSLParameters(sslParams); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
879 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
880 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
881 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
882 |
public Optional<Authenticator> authenticator() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
883 |
return Optional.ofNullable(authenticator); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
884 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
885 |
|
48083 | 886 |
/*package-private*/ final Executor theExecutor() { |
887 |
return executor; |
|
888 |
} |
|
889 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
890 |
@Override |
48083 | 891 |
public final Optional<Executor> executor() { |
892 |
return isDefaultExecutor ? Optional.empty() : Optional.of(executor); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
893 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
894 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
895 |
ConnectionPool connectionPool() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
896 |
return connections; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
897 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
898 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
899 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
900 |
public Redirect followRedirects() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
901 |
return followRedirects; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
902 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
903 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
904 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
905 |
@Override |
48083 | 906 |
public Optional<CookieHandler> cookieHandler() { |
907 |
return Optional.ofNullable(cookieHandler); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
908 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
909 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
910 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
911 |
public Optional<ProxySelector> proxy() { |
48083 | 912 |
return this.userProxySelector; |
913 |
} |
|
914 |
||
915 |
// Return the effective proxy that this client uses. |
|
916 |
ProxySelector proxySelector() { |
|
917 |
return proxySelector; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
918 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
919 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
920 |
@Override |
48083 | 921 |
public WebSocket.Builder newWebSocketBuilder() { |
922 |
// Make sure to pass the HttpClientFacade to the WebSocket builder. |
|
923 |
// This will ensure that the facade is not released before the |
|
924 |
// WebSocket has been created, at which point the pendingOperationCount |
|
925 |
// will have been incremented by the DetachedConnectionChannel |
|
926 |
// (see PlainHttpConnection.detachChannel()) |
|
927 |
return new BuilderImpl(this.facade(), proxySelector); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
928 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
929 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
930 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
931 |
public Version version() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
932 |
return version; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
933 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
934 |
|
48083 | 935 |
String dbgString() { |
936 |
return dbgTag; |
|
937 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
938 |
|
48083 | 939 |
@Override |
940 |
public String toString() { |
|
941 |
// Used by tests to get the client's id and compute the |
|
942 |
// name of the SelectorManager thread. |
|
943 |
return super.toString() + ("(" + id + ")"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
944 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
945 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
946 |
private void initFilters() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
947 |
addFilter(AuthenticationFilter.class); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
948 |
addFilter(RedirectFilter.class); |
48083 | 949 |
if (this.cookieHandler != null) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
950 |
addFilter(CookieFilter.class); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
951 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
952 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
953 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
954 |
private void addFilter(Class<? extends HeaderFilter> f) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
955 |
filters.addFilter(f); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
956 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
957 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
958 |
final List<HeaderFilter> filterChain() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
959 |
return filters.getFilterChain(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
960 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
961 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
962 |
// Timer controls. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
963 |
// Timers are implemented through timed Selector.select() calls. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
964 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
965 |
synchronized void registerTimer(TimeoutEvent event) { |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
966 |
Log.logTrace("Registering timer {0}", event); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
967 |
timeouts.add(event); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
968 |
selmgr.wakeupSelector(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
969 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
970 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
971 |
synchronized void cancelTimer(TimeoutEvent event) { |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
972 |
Log.logTrace("Canceling timer {0}", event); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
973 |
timeouts.remove(event); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
974 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
975 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
976 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
977 |
* Purges ( handles ) timer events that have passed their deadline, and |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
978 |
* returns the amount of time, in milliseconds, until the next earliest |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
979 |
* event. A return value of 0 means that there are no events. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
980 |
*/ |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
981 |
private long purgeTimeoutsAndReturnNextDeadline() { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
982 |
long diff = 0L; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
983 |
List<TimeoutEvent> toHandle = null; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
984 |
int remaining = 0; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
985 |
// enter critical section to retrieve the timeout event to handle |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
986 |
synchronized(this) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
987 |
if (timeouts.isEmpty()) return 0L; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
988 |
|
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
989 |
Instant now = Instant.now(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
990 |
Iterator<TimeoutEvent> itr = timeouts.iterator(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
991 |
while (itr.hasNext()) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
992 |
TimeoutEvent event = itr.next(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
993 |
diff = now.until(event.deadline(), ChronoUnit.MILLIS); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
994 |
if (diff <= 0) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
995 |
itr.remove(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
996 |
toHandle = (toHandle == null) ? new ArrayList<>() : toHandle; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
997 |
toHandle.add(event); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
998 |
} else { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
999 |
break; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1000 |
} |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1001 |
} |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1002 |
remaining = timeouts.size(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1003 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1004 |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1005 |
// can be useful for debugging |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1006 |
if (toHandle != null && Log.trace()) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1007 |
Log.logTrace("purgeTimeoutsAndReturnNextDeadline: handling " |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1008 |
+ (toHandle == null ? 0 : toHandle.size()) + " events, " |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1009 |
+ "remaining " + remaining |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1010 |
+ ", next deadline: " + (diff < 0 ? 0L : diff)); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1011 |
} |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1012 |
|
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1013 |
// handle timeout events out of critical section |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1014 |
if (toHandle != null) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1015 |
Throwable failed = null; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1016 |
for (TimeoutEvent event : toHandle) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1017 |
try { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1018 |
Log.logTrace("Firing timer {0}", event); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1019 |
event.handle(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1020 |
} catch (Error | RuntimeException e) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1021 |
// Not expected. Handle remaining events then throw... |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1022 |
// If e is an OOME or SOE it might simply trigger a new |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1023 |
// error from here - but in this case there's not much we |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1024 |
// could do anyway. Just let it flow... |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1025 |
if (failed == null) failed = e; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1026 |
else failed.addSuppressed(e); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1027 |
Log.logTrace("Failed to handle event {0}: {1}", event, e); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1028 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1029 |
} |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1030 |
if (failed instanceof Error) throw (Error) failed; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1031 |
if (failed instanceof RuntimeException) throw (RuntimeException) failed; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1032 |
} |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1033 |
|
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1034 |
// return time to wait until next event. 0L if there's no more events. |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1035 |
return diff < 0 ? 0L : diff; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1036 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1037 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1038 |
// used for the connection window |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1039 |
int getReceiveBufferSize() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1040 |
return Utils.getIntegerNetProperty( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1041 |
"jdk.httpclient.connectionWindowSize", 256 * 1024 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1042 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1043 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1044 |
} |