author | dfuchs |
Wed, 13 Jun 2018 19:11:47 +0100 | |
branch | http-client-branch |
changeset 56756 | ba60eaef37d7 |
parent 56736 | 705824c1e596 |
child 56763 | 25821dd1d917 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.net.InetSocketAddress; |
49765 | 30 |
import java.net.http.HttpResponse.BodyHandler; |
31 |
import java.net.http.HttpResponse.BodySubscriber; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.nio.ByteBuffer; |
48083 | 33 |
import java.util.Objects; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.util.LinkedList; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.util.List; |
48083 | 37 |
import java.util.concurrent.ConcurrentLinkedDeque; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.util.concurrent.Executor; |
48083 | 39 |
import java.util.concurrent.Flow; |
49765 | 40 |
import jdk.internal.net.http.common.Demand; |
41 |
import jdk.internal.net.http.common.Log; |
|
42 |
import jdk.internal.net.http.common.FlowTube; |
|
43 |
import jdk.internal.net.http.common.Logger; |
|
44 |
import jdk.internal.net.http.common.SequentialScheduler; |
|
45 |
import jdk.internal.net.http.common.MinimalFuture; |
|
46 |
import jdk.internal.net.http.common.Utils; |
|
47 |
import static java.net.http.HttpClient.Version.HTTP_1_1; |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56621
diff
changeset
|
48 |
import static jdk.internal.net.http.common.Utils.wrapWithExtraDetail; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
/** |
48083 | 51 |
* Encapsulates one HTTP/1.1 request/response exchange. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
class Http1Exchange<T> extends ExchangeImpl<T> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
|
49765 | 55 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
48083 | 56 |
final HttpRequestImpl request; // main request |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
final Http1Request requestAction; |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
58 |
private volatile Http1Response<T> response; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
final HttpConnection connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
final HttpClientImpl client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
final Executor executor; |
48083 | 62 |
private final Http1AsyncReceiver asyncReceiver; |
63 |
||
64 |
/** Records a possible cancellation raised before any operation |
|
65 |
* has been initiated, or an error received while sending the request. */ |
|
66 |
private Throwable failed; |
|
67 |
private final List<CompletableFuture<?>> operations; // used for cancel |
|
68 |
||
69 |
/** Must be held when operating on any internal state or data. */ |
|
70 |
private final Object lock = new Object(); |
|
71 |
||
72 |
/** Holds the outgoing data, either the headers or a request body part. Or |
|
73 |
* an error from the request body publisher. At most there can be ~2 pieces |
|
74 |
* of outgoing data ( onComplete|onError can be invoked without demand ).*/ |
|
75 |
final ConcurrentLinkedDeque<DataPair> outgoing = new ConcurrentLinkedDeque<>(); |
|
76 |
||
77 |
/** The write publisher, responsible for writing the complete request ( both |
|
78 |
* headers and body ( if any ). */ |
|
79 |
private final Http1Publisher writePublisher = new Http1Publisher(); |
|
80 |
||
81 |
/** Completed when the header have been published, or there is an error */ |
|
49765 | 82 |
private final CompletableFuture<ExchangeImpl<T>> headersSentCF = new MinimalFuture<>(); |
48083 | 83 |
/** Completed when the body has been published, or there is an error */ |
49765 | 84 |
private final CompletableFuture<ExchangeImpl<T>> bodySentCF = new MinimalFuture<>(); |
48083 | 85 |
|
86 |
/** The subscriber to the request's body published. Maybe null. */ |
|
87 |
private volatile Http1BodySubscriber bodySubscriber; |
|
88 |
||
89 |
enum State { INITIAL, |
|
90 |
HEADERS, |
|
91 |
BODY, |
|
92 |
ERROR, // terminal state |
|
93 |
COMPLETING, |
|
94 |
COMPLETED } // terminal state |
|
95 |
||
96 |
private State state = State.INITIAL; |
|
97 |
||
98 |
/** A carrier for either data or an error. Used to carry data, and communicate |
|
99 |
* errors from the request ( both headers and body ) to the exchange. */ |
|
100 |
static class DataPair { |
|
101 |
Throwable throwable; |
|
102 |
List<ByteBuffer> data; |
|
103 |
DataPair(List<ByteBuffer> data, Throwable throwable){ |
|
104 |
this.data = data; |
|
105 |
this.throwable = throwable; |
|
106 |
} |
|
107 |
@Override |
|
108 |
public String toString() { |
|
109 |
return "DataPair [data=" + data + ", throwable=" + throwable + "]"; |
|
110 |
} |
|
111 |
} |
|
112 |
||
113 |
/** An abstract supertype for HTTP/1.1 body subscribers. There are two |
|
114 |
* concrete implementations: {@link Http1Request.StreamSubscriber}, and |
|
115 |
* {@link Http1Request.FixedContentSubscriber}, for receiving chunked and |
|
116 |
* fixed length bodies, respectively. */ |
|
117 |
static abstract class Http1BodySubscriber implements Flow.Subscriber<ByteBuffer> { |
|
49765 | 118 |
final MinimalFuture<Flow.Subscription> whenSubscribed = new MinimalFuture<>(); |
119 |
private volatile Flow.Subscription subscription; |
|
120 |
volatile boolean complete; |
|
121 |
private final Logger debug; |
|
122 |
Http1BodySubscriber(Logger debug) { |
|
123 |
assert debug != null; |
|
124 |
this.debug = debug; |
|
125 |
} |
|
48083 | 126 |
|
127 |
/** Final sentinel in the stream of request body. */ |
|
128 |
static final List<ByteBuffer> COMPLETED = List.of(ByteBuffer.allocate(0)); |
|
129 |
||
49765 | 130 |
final void request(long n) { |
131 |
if (debug.on()) |
|
132 |
debug.log("Http1BodySubscriber requesting %d, from %s", |
|
133 |
n, subscription); |
|
48083 | 134 |
subscription.request(n); |
135 |
} |
|
136 |
||
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56621
diff
changeset
|
137 |
/** A current-state message suitable for inclusion in an exception detail message. */ |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56621
diff
changeset
|
138 |
abstract String currentStateMessage(); |
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56621
diff
changeset
|
139 |
|
49765 | 140 |
final boolean isSubscribed() { |
141 |
return subscription != null; |
|
142 |
} |
|
143 |
||
144 |
final void setSubscription(Flow.Subscription subscription) { |
|
145 |
this.subscription = subscription; |
|
146 |
whenSubscribed.complete(subscription); |
|
147 |
} |
|
148 |
||
149 |
final void cancelSubscription() { |
|
150 |
try { |
|
151 |
subscription.cancel(); |
|
152 |
} catch(Throwable t) { |
|
153 |
String msg = "Ignoring exception raised when canceling BodyPublisher subscription"; |
|
154 |
if (debug.on()) debug.log("%s: %s", msg, t); |
|
155 |
Log.logError("{0}: {1}", msg, (Object)t); |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
static Http1BodySubscriber completeSubscriber(Logger debug) { |
|
160 |
return new Http1BodySubscriber(debug) { |
|
48083 | 161 |
@Override public void onSubscribe(Flow.Subscription subscription) { error(); } |
162 |
@Override public void onNext(ByteBuffer item) { error(); } |
|
163 |
@Override public void onError(Throwable throwable) { error(); } |
|
164 |
@Override public void onComplete() { error(); } |
|
56682
9822bbe48b9b
http-client-branch: more descriptive HTTP/1.1 exception detail messages
chegar
parents:
56621
diff
changeset
|
165 |
@Override String currentStateMessage() { return null; } |
48083 | 166 |
private void error() { |
167 |
throw new InternalError("should not reach here"); |
|
168 |
} |
|
169 |
}; |
|
170 |
} |
|
171 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
172 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
174 |
public String toString() { |
48083 | 175 |
return "HTTP/1.1 " + request.toString(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
177 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
178 |
HttpRequestImpl request() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
return request; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
Http1Exchange(Exchange<T> exchange, HttpConnection connection) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
throws IOException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
184 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
super(exchange); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
this.request = exchange.request(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
this.client = exchange.client(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
this.executor = exchange.executor(); |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
189 |
this.operations = new LinkedList<>(); |
48083 | 190 |
operations.add(headersSentCF); |
191 |
operations.add(bodySentCF); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
if (connection != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
this.connection = connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
} else { |
48083 | 195 |
InetSocketAddress addr = request.getAddress(); |
196 |
this.connection = HttpConnection.getConnection(addr, client, request, HTTP_1_1); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
197 |
} |
48083 | 198 |
this.requestAction = new Http1Request(request, this); |
199 |
this.asyncReceiver = new Http1AsyncReceiver(executor, this); |
|
200 |
} |
|
201 |
||
202 |
@Override |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
203 |
HttpConnection connection() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
204 |
return connection; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
205 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
206 |
|
48083 | 207 |
private void connectFlows(HttpConnection connection) { |
208 |
FlowTube tube = connection.getConnectionFlow(); |
|
49765 | 209 |
if (debug.on()) debug.log("%s connecting flows", tube); |
48083 | 210 |
|
211 |
// Connect the flow to our Http1TubeSubscriber: |
|
212 |
// asyncReceiver.subscriber(). |
|
213 |
tube.connectFlows(writePublisher, |
|
214 |
asyncReceiver.subscriber()); |
|
215 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
@Override |
48083 | 218 |
CompletableFuture<ExchangeImpl<T>> sendHeadersAsync() { |
219 |
// create the response before sending the request headers, so that |
|
220 |
// the response can set the appropriate receivers. |
|
49765 | 221 |
if (debug.on()) debug.log("Sending headers only"); |
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
222 |
// If the first attempt to read something triggers EOF, or |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
223 |
// IOException("channel reset by peer"), we're going to retry. |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
224 |
// Instruct the asyncReceiver to throw ConnectionExpiredException |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
225 |
// to force a retry. |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
226 |
asyncReceiver.setRetryOnError(true); |
48083 | 227 |
if (response == null) { |
228 |
response = new Http1Response<>(connection, this, asyncReceiver); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
} |
48083 | 230 |
|
49765 | 231 |
if (debug.on()) debug.log("response created in advance"); |
48083 | 232 |
|
233 |
CompletableFuture<Void> connectCF; |
|
234 |
if (!connection.connected()) { |
|
49765 | 235 |
if (debug.on()) debug.log("initiating connect async"); |
48083 | 236 |
connectCF = connection.connectAsync(); |
49944 | 237 |
Throwable cancelled; |
48083 | 238 |
synchronized (lock) { |
49944 | 239 |
if ((cancelled = failed) == null) { |
240 |
operations.add(connectCF); |
|
241 |
} |
|
242 |
} |
|
243 |
if (cancelled != null) { |
|
244 |
if (client.isSelectorThread()) { |
|
245 |
executor.execute(() -> |
|
246 |
connectCF.completeExceptionally(cancelled)); |
|
247 |
} else { |
|
248 |
connectCF.completeExceptionally(cancelled); |
|
249 |
} |
|
48083 | 250 |
} |
251 |
} else { |
|
252 |
connectCF = new MinimalFuture<>(); |
|
253 |
connectCF.complete(null); |
|
254 |
} |
|
255 |
||
256 |
return connectCF |
|
257 |
.thenCompose(unused -> { |
|
258 |
CompletableFuture<Void> cf = new MinimalFuture<>(); |
|
259 |
try { |
|
260 |
connectFlows(connection); |
|
261 |
||
49765 | 262 |
if (debug.on()) debug.log("requestAction.headers"); |
48083 | 263 |
List<ByteBuffer> data = requestAction.headers(); |
264 |
synchronized (lock) { |
|
265 |
state = State.HEADERS; |
|
266 |
} |
|
49765 | 267 |
if (debug.on()) debug.log("setting outgoing with headers"); |
48083 | 268 |
assert outgoing.isEmpty() : "Unexpected outgoing:" + outgoing; |
269 |
appendToOutgoing(data); |
|
270 |
cf.complete(null); |
|
271 |
return cf; |
|
272 |
} catch (Throwable t) { |
|
49765 | 273 |
if (debug.on()) debug.log("Failed to send headers: %s", t); |
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
274 |
headersSentCF.completeExceptionally(t); |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
275 |
bodySentCF.completeExceptionally(t); |
48083 | 276 |
connection.close(); |
277 |
cf.completeExceptionally(t); |
|
278 |
return cf; |
|
279 |
} }) |
|
280 |
.thenCompose(unused -> headersSentCF); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
281 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
282 |
|
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
283 |
private void cancelIfFailed(Flow.Subscription s) { |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
284 |
asyncReceiver.whenFinished.whenCompleteAsync((r,t) -> { |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
285 |
if (debug.on()) debug.log("asyncReceiver finished (failed=%s)", t); |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
286 |
if (t != null) { |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
287 |
s.cancel(); |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
288 |
bodySentCF.complete(this); |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
289 |
} |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
290 |
}, executor); |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
291 |
} |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
292 |
|
48083 | 293 |
@Override |
294 |
CompletableFuture<ExchangeImpl<T>> sendBodyAsync() { |
|
295 |
assert headersSentCF.isDone(); |
|
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
296 |
if (debug.on()) debug.log("sendBodyAsync"); |
48083 | 297 |
try { |
298 |
bodySubscriber = requestAction.continueRequest(); |
|
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
299 |
if (debug.on()) debug.log("bodySubscriber is %s", |
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
300 |
bodySubscriber == null ? null : bodySubscriber.getClass()); |
48083 | 301 |
if (bodySubscriber == null) { |
49765 | 302 |
bodySubscriber = Http1BodySubscriber.completeSubscriber(debug); |
48083 | 303 |
appendToOutgoing(Http1BodySubscriber.COMPLETED); |
304 |
} else { |
|
49765 | 305 |
// start |
306 |
bodySubscriber.whenSubscribed |
|
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
307 |
.thenAccept((s) -> cancelIfFailed(s)) |
49765 | 308 |
.thenAccept((s) -> requestMoreBody()); |
48083 | 309 |
} |
310 |
} catch (Throwable t) { |
|
49765 | 311 |
cancelImpl(t); |
48083 | 312 |
bodySentCF.completeExceptionally(t); |
313 |
} |
|
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
314 |
return Utils.wrapForDebug(debug, "sendBodyAsync", bodySentCF); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
316 |
|
48083 | 317 |
@Override |
318 |
CompletableFuture<Response> getResponseAsync(Executor executor) { |
|
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
319 |
if (debug.on()) debug.log("reading headers"); |
48083 | 320 |
CompletableFuture<Response> cf = response.readHeadersAsync(executor); |
321 |
Throwable cause; |
|
322 |
synchronized (lock) { |
|
323 |
operations.add(cf); |
|
324 |
cause = failed; |
|
325 |
failed = null; |
|
326 |
} |
|
327 |
||
328 |
if (cause != null) { |
|
329 |
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms]" |
|
330 |
+ "\n\tCompleting exceptionally with {2}\n", |
|
331 |
request.uri(), |
|
332 |
request.timeout().isPresent() ? |
|
333 |
// calling duration.toMillis() can throw an exception. |
|
334 |
// this is just debugging, we don't care if it overflows. |
|
335 |
(request.timeout().get().getSeconds() * 1000 |
|
336 |
+ request.timeout().get().getNano() / 1000000) : -1, |
|
337 |
cause); |
|
338 |
boolean acknowledged = cf.completeExceptionally(cause); |
|
49765 | 339 |
if (debug.on()) |
340 |
debug.log(acknowledged ? ("completed response with " + cause) |
|
341 |
: ("response already completed, ignoring " + cause)); |
|
48083 | 342 |
} |
56756
ba60eaef37d7
http-client-branch: fixed ShortBodyResponse issues
dfuchs
parents:
56736
diff
changeset
|
343 |
return Utils.wrapForDebug(debug, "getResponseAsync", cf); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
CompletableFuture<T> readBodyAsync(BodyHandler<T> handler, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
43984
diff
changeset
|
348 |
boolean returnConnectionToPool, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
Executor executor) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
{ |
49765 | 351 |
BodySubscriber<T> bs = handler.apply(new ResponseInfoImpl(response.responseCode(), |
352 |
response.responseHeaders(), |
|
353 |
HTTP_1_1)); |
|
48083 | 354 |
CompletableFuture<T> bodyCF = response.readBody(bs, |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
43984
diff
changeset
|
355 |
returnConnectionToPool, |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
executor); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
357 |
return bodyCF; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
@Override |
48083 | 361 |
CompletableFuture<Void> ignoreBody() { |
362 |
return response.ignoreBody(executor); |
|
363 |
} |
|
364 |
||
365 |
ByteBuffer drainLeftOverBytes() { |
|
366 |
synchronized (lock) { |
|
367 |
asyncReceiver.stop(); |
|
368 |
return asyncReceiver.drain(Utils.EMPTY_BYTEBUFFER); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
370 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
371 |
|
48083 | 372 |
void released() { |
373 |
Http1Response<T> resp = this.response; |
|
374 |
if (resp != null) resp.completed(); |
|
375 |
asyncReceiver.clear(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
377 |
|
48083 | 378 |
void completed() { |
379 |
Http1Response<T> resp = this.response; |
|
380 |
if (resp != null) resp.completed(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
381 |
} |
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 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
384 |
* Cancel checks to see if request and responseAsync finished already. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
385 |
* If not it closes the connection and completes all pending operations |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
386 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
387 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
388 |
void cancel() { |
48083 | 389 |
cancelImpl(new IOException("Request cancelled")); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
390 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
391 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
392 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
393 |
* Cancel checks to see if request and responseAsync finished already. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
394 |
* If not it closes the connection and completes all pending operations |
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 |
48083 | 397 |
void cancel(IOException cause) { |
398 |
cancelImpl(cause); |
|
399 |
} |
|
400 |
||
401 |
private void cancelImpl(Throwable cause) { |
|
402 |
LinkedList<CompletableFuture<?>> toComplete = null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
int count = 0; |
49765 | 404 |
Throwable error; |
48083 | 405 |
synchronized (lock) { |
49765 | 406 |
if ((error = failed) == null) { |
407 |
failed = error = cause; |
|
408 |
} |
|
49944 | 409 |
if (debug.on()) { |
410 |
debug.log(request.uri() + ": " + error); |
|
411 |
} |
|
48083 | 412 |
if (requestAction != null && requestAction.finished() |
413 |
&& response != null && response.finished()) { |
|
414 |
return; |
|
415 |
} |
|
416 |
writePublisher.writeScheduler.stop(); |
|
417 |
if (operations.isEmpty()) { |
|
418 |
Log.logTrace("Http1Exchange: request [{0}/timeout={1}ms] no pending operation." |
|
419 |
+ "\n\tCan''t cancel yet with {2}", |
|
420 |
request.uri(), |
|
421 |
request.timeout().isPresent() ? |
|
422 |
// calling duration.toMillis() can throw an exception. |
|
423 |
// this is just debugging, we don't care if it overflows. |
|
424 |
(request.timeout().get().getSeconds() * 1000 |
|
425 |
+ request.timeout().get().getNano() / 1000000) : -1, |
|
426 |
cause); |
|
427 |
} else { |
|
428 |
for (CompletableFuture<?> cf : operations) { |
|
429 |
if (!cf.isDone()) { |
|
430 |
if (toComplete == null) toComplete = new LinkedList<>(); |
|
431 |
toComplete.add(cf); |
|
432 |
count++; |
|
433 |
} |
|
434 |
} |
|
435 |
operations.clear(); |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
436 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
437 |
} |
56736
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
438 |
try { |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
439 |
Log.logError("Http1Exchange.cancel: count=" + count); |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
440 |
if (toComplete != null) { |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
441 |
// We might be in the selector thread in case of timeout, when |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
442 |
// the SelectorManager calls purgeTimeoutsAndReturnNextDeadline() |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
443 |
// There may or may not be other places that reach here |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
444 |
// from the SelectorManager thread, so just make sure we |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
445 |
// don't complete any CF from within the selector manager |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
446 |
// thread. |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
447 |
Executor exec = client.isSelectorThread() |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
448 |
? executor |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
449 |
: this::runInline; |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
450 |
Throwable x = error; |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
451 |
while (!toComplete.isEmpty()) { |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
452 |
CompletableFuture<?> cf = toComplete.poll(); |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
453 |
exec.execute(() -> { |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
454 |
if (cf.completeExceptionally(x)) { |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
455 |
if (debug.on()) |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
456 |
debug.log("%s: completed cf with %s", request.uri(), x); |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
457 |
} |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
458 |
}); |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
459 |
} |
48083 | 460 |
} |
56736
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
461 |
} finally { |
705824c1e596
http-client-branch: fix regression introcuced by last changeset
dfuchs
parents:
56730
diff
changeset
|
462 |
connection.close(); |
48083 | 463 |
} |
464 |
} |
|
465 |
||
466 |
private void runInline(Runnable run) { |
|
467 |
assert !client.isSelectorThread(); |
|
468 |
run.run(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
469 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
470 |
|
48083 | 471 |
/** Returns true if this exchange was canceled. */ |
472 |
boolean isCanceled() { |
|
473 |
synchronized (lock) { |
|
474 |
return failed != null; |
|
475 |
} |
|
476 |
} |
|
477 |
||
478 |
/** Returns the cause for which this exchange was canceled, if available. */ |
|
479 |
Throwable getCancelCause() { |
|
480 |
synchronized (lock) { |
|
481 |
return failed; |
|
482 |
} |
|
483 |
} |
|
484 |
||
485 |
/** Convenience for {@link #appendToOutgoing(DataPair)}, with just a Throwable. */ |
|
486 |
void appendToOutgoing(Throwable throwable) { |
|
487 |
appendToOutgoing(new DataPair(null, throwable)); |
|
488 |
} |
|
489 |
||
490 |
/** Convenience for {@link #appendToOutgoing(DataPair)}, with just data. */ |
|
491 |
void appendToOutgoing(List<ByteBuffer> item) { |
|
492 |
appendToOutgoing(new DataPair(item, null)); |
|
493 |
} |
|
494 |
||
495 |
private void appendToOutgoing(DataPair dp) { |
|
49765 | 496 |
if (debug.on()) debug.log("appending to outgoing " + dp); |
48083 | 497 |
outgoing.add(dp); |
498 |
writePublisher.writeScheduler.runOrSchedule(); |
|
499 |
} |
|
500 |
||
501 |
/** Tells whether, or not, there is any outgoing data that can be published, |
|
502 |
* or if there is an error. */ |
|
503 |
private boolean hasOutgoing() { |
|
504 |
return !outgoing.isEmpty(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
505 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
506 |
|
49765 | 507 |
private void requestMoreBody() { |
508 |
try { |
|
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
509 |
if (debug.on()) debug.log("requesting more request body from the subscriber"); |
49765 | 510 |
bodySubscriber.request(1); |
511 |
} catch (Throwable t) { |
|
512 |
if (debug.on()) debug.log("Subscription::request failed", t); |
|
513 |
cancelImpl(t); |
|
514 |
bodySentCF.completeExceptionally(t); |
|
515 |
} |
|
516 |
} |
|
517 |
||
48083 | 518 |
// Invoked only by the publisher |
519 |
// ALL tasks should execute off the Selector-Manager thread |
|
520 |
/** Returns the next portion of the HTTP request, or the error. */ |
|
521 |
private DataPair getOutgoing() { |
|
522 |
final Executor exec = client.theExecutor(); |
|
523 |
final DataPair dp = outgoing.pollFirst(); |
|
524 |
||
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
525 |
if (writePublisher.cancelled) { |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
526 |
if (debug.on()) debug.log("cancelling upstream publisher"); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
527 |
if (bodySubscriber != null) { |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
528 |
exec.execute(bodySubscriber::cancelSubscription); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
529 |
} else if (debug.on()) { |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
530 |
debug.log("bodySubscriber is null"); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
531 |
} |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
532 |
headersSentCF.completeAsync(() -> this, exec); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
533 |
bodySentCF.completeAsync(() -> this, exec); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
534 |
return null; |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
535 |
} |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
536 |
|
48083 | 537 |
if (dp == null) // publisher has not published anything yet |
538 |
return null; |
|
539 |
||
56621
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
540 |
if (dp.throwable != null) { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
541 |
synchronized (lock) { |
48083 | 542 |
state = State.ERROR; |
543 |
} |
|
56621
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
544 |
exec.execute(() -> { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
545 |
headersSentCF.completeExceptionally(dp.throwable); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
546 |
bodySentCF.completeExceptionally(dp.throwable); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
547 |
connection.close(); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
548 |
}); |
48083 | 549 |
return dp; |
550 |
} |
|
56621
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
551 |
|
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
552 |
switch (state) { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
553 |
case HEADERS: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
554 |
synchronized (lock) { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
555 |
state = State.BODY; |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
556 |
} |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
557 |
// completeAsync, since dependent tasks should run in another thread |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
558 |
if (debug.on()) debug.log("initiating completion of headersSentCF"); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
559 |
headersSentCF.completeAsync(() -> this, exec); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
560 |
break; |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
561 |
case BODY: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
562 |
if (dp.data == Http1BodySubscriber.COMPLETED) { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
563 |
synchronized (lock) { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
564 |
state = State.COMPLETING; |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
565 |
} |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
566 |
if (debug.on()) debug.log("initiating completion of bodySentCF"); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
567 |
bodySentCF.completeAsync(() -> this, exec); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
568 |
} else { |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
569 |
exec.execute(this::requestMoreBody); |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
570 |
} |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
571 |
break; |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
572 |
case INITIAL: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
573 |
case ERROR: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
574 |
case COMPLETING: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
575 |
case COMPLETED: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
576 |
default: |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
577 |
assert false : "Unexpected state:" + state; |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
578 |
} |
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
579 |
|
a85c163fc41c
http-client-branch: performance - reduce context switching
dfuchs
parents:
56507
diff
changeset
|
580 |
return dp; |
48083 | 581 |
} |
582 |
||
583 |
/** A Publisher of HTTP/1.1 headers and request body. */ |
|
584 |
final class Http1Publisher implements FlowTube.TubePublisher { |
|
585 |
||
49765 | 586 |
final Logger debug = Utils.getDebugLogger(this::dbgString); |
48083 | 587 |
volatile Flow.Subscriber<? super List<ByteBuffer>> subscriber; |
588 |
volatile boolean cancelled; |
|
589 |
final Http1WriteSubscription subscription = new Http1WriteSubscription(); |
|
590 |
final Demand demand = new Demand(); |
|
591 |
final SequentialScheduler writeScheduler = |
|
592 |
SequentialScheduler.synchronizedScheduler(new WriteTask()); |
|
593 |
||
594 |
@Override |
|
595 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
596 |
assert state == State.INITIAL; |
|
597 |
Objects.requireNonNull(s); |
|
598 |
assert subscriber == null; |
|
599 |
||
600 |
subscriber = s; |
|
49765 | 601 |
if (debug.on()) debug.log("got subscriber: %s", s); |
48083 | 602 |
s.onSubscribe(subscription); |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
603 |
} |
48083 | 604 |
|
605 |
volatile String dbgTag; |
|
606 |
String dbgString() { |
|
607 |
String tag = dbgTag; |
|
608 |
Object flow = connection.getConnectionFlow(); |
|
609 |
if (tag == null && flow != null) { |
|
610 |
dbgTag = tag = "Http1Publisher(" + flow + ")"; |
|
611 |
} else if (tag == null) { |
|
612 |
tag = "Http1Publisher(?)"; |
|
613 |
} |
|
614 |
return tag; |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43999
diff
changeset
|
615 |
} |
48083 | 616 |
|
617 |
final class WriteTask implements Runnable { |
|
618 |
@Override |
|
619 |
public void run() { |
|
620 |
assert state != State.COMPLETED : "Unexpected state:" + state; |
|
49765 | 621 |
if (debug.on()) debug.log("WriteTask"); |
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
622 |
|
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
623 |
if (cancelled) { |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
624 |
if (debug.on()) debug.log("handling cancellation"); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
625 |
writeScheduler.stop(); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
626 |
getOutgoing(); |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
627 |
return; |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
628 |
} |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
629 |
|
48083 | 630 |
if (subscriber == null) { |
49765 | 631 |
if (debug.on()) debug.log("no subscriber yet"); |
48083 | 632 |
return; |
633 |
} |
|
49765 | 634 |
if (debug.on()) debug.log(() -> "hasOutgoing = " + hasOutgoing()); |
48083 | 635 |
while (hasOutgoing() && demand.tryDecrement()) { |
636 |
DataPair dp = getOutgoing(); |
|
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
637 |
if (dp == null) |
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
638 |
break; |
48083 | 639 |
|
640 |
if (dp.throwable != null) { |
|
49765 | 641 |
if (debug.on()) debug.log("onError"); |
48083 | 642 |
// Do not call the subscriber's onError, it is not required. |
643 |
writeScheduler.stop(); |
|
644 |
} else { |
|
645 |
List<ByteBuffer> data = dp.data; |
|
646 |
if (data == Http1BodySubscriber.COMPLETED) { |
|
647 |
synchronized (lock) { |
|
648 |
assert state == State.COMPLETING : "Unexpected state:" + state; |
|
649 |
state = State.COMPLETED; |
|
650 |
} |
|
49765 | 651 |
if (debug.on()) |
652 |
debug.log("completed, stopping %s", writeScheduler); |
|
48083 | 653 |
writeScheduler.stop(); |
654 |
// Do nothing more. Just do not publish anything further. |
|
655 |
// The next Subscriber will eventually take over. |
|
656 |
||
657 |
} else { |
|
49765 | 658 |
if (debug.on()) |
659 |
debug.log("onNext with " + Utils.remaining(data) + " bytes"); |
|
48083 | 660 |
subscriber.onNext(data); |
661 |
} |
|
662 |
} |
|
663 |
} |
|
664 |
} |
|
665 |
} |
|
666 |
||
667 |
final class Http1WriteSubscription implements Flow.Subscription { |
|
668 |
||
669 |
@Override |
|
670 |
public void request(long n) { |
|
671 |
if (cancelled) |
|
672 |
return; //no-op |
|
673 |
demand.increase(n); |
|
49765 | 674 |
if (debug.on()) |
675 |
debug.log("subscription request(%d), demand=%s", n, demand); |
|
676 |
writeScheduler.runOrSchedule(client.theExecutor()); |
|
48083 | 677 |
} |
678 |
||
679 |
@Override |
|
680 |
public void cancel() { |
|
49765 | 681 |
if (debug.on()) debug.log("subscription cancelled"); |
48083 | 682 |
if (cancelled) |
683 |
return; //no-op |
|
684 |
cancelled = true; |
|
56730
b08918259eed
http-client-branch: review comment: better handling for error conditions and tests to verify
chegar
parents:
56682
diff
changeset
|
685 |
writeScheduler.runOrSchedule(client.theExecutor()); |
48083 | 686 |
} |
687 |
} |
|
688 |
} |
|
689 |
||
690 |
String dbgString() { |
|
691 |
return "Http1Exchange"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
692 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
693 |
} |