author | dfuchs |
Fri, 23 Mar 2018 18:01:52 +0000 | |
branch | http-client-branch |
changeset 56345 | eb72d194235c |
parent 56335 | 7e56c39fa1fa |
child 56355 | bf89fba643d9 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
55973
diff
changeset
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
48083 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
26 |
package jdk.internal.net.http; |
48083 | 27 |
|
28 |
import java.io.EOFException; |
|
29 |
import java.io.IOException; |
|
30 |
import java.lang.System.Logger.Level; |
|
31 |
import java.nio.ByteBuffer; |
|
32 |
import java.util.List; |
|
33 |
import java.util.Objects; |
|
34 |
import java.util.concurrent.Flow; |
|
56299
903ff8ec239d
http-client-branch: fix race condition between WebSocket and PlainHttpConnection::detachChannel
dfuchs
parents:
56252
diff
changeset
|
35 |
import java.util.concurrent.atomic.AtomicBoolean; |
48083 | 36 |
import java.util.concurrent.atomic.AtomicLong; |
37 |
import java.util.concurrent.atomic.AtomicReference; |
|
38 |
import java.nio.channels.SelectableChannel; |
|
39 |
import java.nio.channels.SelectionKey; |
|
40 |
import java.nio.channels.SocketChannel; |
|
41 |
import java.util.ArrayList; |
|
42 |
import java.util.function.Consumer; |
|
43 |
import java.util.function.Supplier; |
|
44 |
||
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
45 |
import jdk.internal.net.http.common.Demand; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
46 |
import jdk.internal.net.http.common.FlowTube; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
47 |
import jdk.internal.net.http.common.SequentialScheduler; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
48 |
import jdk.internal.net.http.common.SequentialScheduler.DeferredCompleter; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
49 |
import jdk.internal.net.http.common.SequentialScheduler.RestartableTask; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
50 |
import jdk.internal.net.http.common.Utils; |
48083 | 51 |
|
52 |
/** |
|
53 |
* A SocketTube is a terminal tube plugged directly into the socket. |
|
54 |
* The read subscriber should call {@code subscribe} on the SocketTube before |
|
55 |
* the SocketTube can be subscribed to the write publisher. |
|
56 |
*/ |
|
57 |
final class SocketTube implements FlowTube { |
|
58 |
||
59 |
static final boolean DEBUG = Utils.DEBUG; // revisit: temporary developer's flag |
|
60 |
final System.Logger debug = Utils.getDebugLogger(this::dbgString, DEBUG); |
|
61 |
static final AtomicLong IDS = new AtomicLong(); |
|
62 |
||
63 |
private final HttpClientImpl client; |
|
64 |
private final SocketChannel channel; |
|
65 |
private final Supplier<ByteBuffer> buffersSource; |
|
66 |
private final Object lock = new Object(); |
|
67 |
private final AtomicReference<Throwable> errorRef = new AtomicReference<>(); |
|
68 |
private final InternalReadPublisher readPublisher; |
|
69 |
private final InternalWriteSubscriber writeSubscriber; |
|
70 |
private final long id = IDS.incrementAndGet(); |
|
71 |
||
72 |
public SocketTube(HttpClientImpl client, SocketChannel channel, |
|
73 |
Supplier<ByteBuffer> buffersSource) { |
|
74 |
this.client = client; |
|
75 |
this.channel = channel; |
|
76 |
this.buffersSource = buffersSource; |
|
77 |
this.readPublisher = new InternalReadPublisher(); |
|
78 |
this.writeSubscriber = new InternalWriteSubscriber(); |
|
79 |
} |
|
80 |
||
81 |
// private static Flow.Subscription nopSubscription() { |
|
82 |
// return new Flow.Subscription() { |
|
83 |
// @Override public void request(long n) { } |
|
84 |
// @Override public void cancel() { } |
|
85 |
// }; |
|
86 |
// } |
|
87 |
||
88 |
/** |
|
89 |
* Returns {@code true} if this flow is finished. |
|
90 |
* This happens when this flow internal read subscription is completed, |
|
91 |
* either normally (EOF reading) or exceptionally (EOF writing, or |
|
92 |
* underlying socket closed, or some exception occurred while reading or |
|
93 |
* writing to the socket). |
|
94 |
* |
|
95 |
* @return {@code true} if this flow is finished. |
|
96 |
*/ |
|
97 |
public boolean isFinished() { |
|
98 |
InternalReadPublisher.InternalReadSubscription subscription = |
|
99 |
readPublisher.subscriptionImpl; |
|
100 |
return subscription != null && subscription.completed |
|
101 |
|| subscription == null && errorRef.get() != null; |
|
102 |
} |
|
103 |
||
104 |
// ===================================================================== // |
|
105 |
// Flow.Publisher // |
|
106 |
// ======================================================================// |
|
107 |
||
108 |
/** |
|
109 |
* {@inheritDoc } |
|
110 |
* @apiNote This method should be called first. In particular, the caller |
|
111 |
* must ensure that this method must be called by the read |
|
112 |
* subscriber before the write publisher can call {@code onSubscribe}. |
|
113 |
* Failure to adhere to this contract may result in assertion errors. |
|
114 |
*/ |
|
115 |
@Override |
|
116 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
117 |
Objects.requireNonNull(s); |
|
118 |
assert s instanceof TubeSubscriber : "Expected TubeSubscriber, got:" + s; |
|
119 |
readPublisher.subscribe(s); |
|
120 |
} |
|
121 |
||
122 |
||
123 |
// ===================================================================== // |
|
124 |
// Flow.Subscriber // |
|
125 |
// ======================================================================// |
|
126 |
||
127 |
/** |
|
128 |
* {@inheritDoc } |
|
129 |
* @apiNote The caller must ensure that {@code subscribe} is called by |
|
130 |
* the read subscriber before {@code onSubscribe} is called by |
|
131 |
* the write publisher. |
|
132 |
* Failure to adhere to this contract may result in assertion errors. |
|
133 |
*/ |
|
134 |
@Override |
|
135 |
public void onSubscribe(Flow.Subscription subscription) { |
|
136 |
writeSubscriber.onSubscribe(subscription); |
|
137 |
} |
|
138 |
||
139 |
@Override |
|
140 |
public void onNext(List<ByteBuffer> item) { |
|
141 |
writeSubscriber.onNext(item); |
|
142 |
} |
|
143 |
||
144 |
@Override |
|
145 |
public void onError(Throwable throwable) { |
|
146 |
writeSubscriber.onError(throwable); |
|
147 |
} |
|
148 |
||
149 |
@Override |
|
150 |
public void onComplete() { |
|
151 |
writeSubscriber.onComplete(); |
|
152 |
} |
|
153 |
||
154 |
// ===================================================================== // |
|
155 |
// Events // |
|
156 |
// ======================================================================// |
|
157 |
||
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
158 |
void signalClosed() { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
159 |
// Ensure that the subscriber will be terminated |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
160 |
// and that future subscribers will be notified |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
161 |
// when the connection is closed. |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
162 |
readPublisher.subscriptionImpl.signalError( |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
163 |
new IOException("connection closed locally")); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
164 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
165 |
|
48083 | 166 |
/** |
167 |
* A restartable task used to process tasks in sequence. |
|
168 |
*/ |
|
169 |
private static class SocketFlowTask implements RestartableTask { |
|
170 |
final Runnable task; |
|
171 |
private final Object monitor = new Object(); |
|
172 |
SocketFlowTask(Runnable task) { |
|
173 |
this.task = task; |
|
174 |
} |
|
175 |
@Override |
|
176 |
public final void run(DeferredCompleter taskCompleter) { |
|
177 |
try { |
|
178 |
// non contentious synchronized for visibility. |
|
179 |
synchronized(monitor) { |
|
180 |
task.run(); |
|
181 |
} |
|
182 |
} finally { |
|
183 |
taskCompleter.complete(); |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
// This is best effort - there's no guarantee that the printed set |
|
189 |
// of values is consistent. It should only be considered as |
|
190 |
// weakly accurate - in particular in what concerns the events states, |
|
191 |
// especially when displaying a read event state from a write event |
|
192 |
// callback and conversely. |
|
193 |
void debugState(String when) { |
|
194 |
if (debug.isLoggable(Level.DEBUG)) { |
|
195 |
StringBuilder state = new StringBuilder(); |
|
196 |
||
197 |
InternalReadPublisher.InternalReadSubscription sub = |
|
198 |
readPublisher.subscriptionImpl; |
|
199 |
InternalReadPublisher.ReadEvent readEvent = |
|
200 |
sub == null ? null : sub.readEvent; |
|
201 |
Demand rdemand = sub == null ? null : sub.demand; |
|
202 |
InternalWriteSubscriber.WriteEvent writeEvent = |
|
203 |
writeSubscriber.writeEvent; |
|
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
204 |
Demand wdemand = writeSubscriber.writeDemand; |
48083 | 205 |
int rops = readEvent == null ? 0 : readEvent.interestOps(); |
206 |
long rd = rdemand == null ? 0 : rdemand.get(); |
|
207 |
int wops = writeEvent == null ? 0 : writeEvent.interestOps(); |
|
208 |
long wd = wdemand == null ? 0 : wdemand.get(); |
|
209 |
||
210 |
state.append(when).append(" Reading: [ops=") |
|
211 |
.append(rops).append(", demand=").append(rd) |
|
212 |
.append(", stopped=") |
|
213 |
.append((sub == null ? false : sub.readScheduler.isStopped())) |
|
214 |
.append("], Writing: [ops=").append(wops) |
|
215 |
.append(", demand=").append(wd) |
|
216 |
.append("]"); |
|
217 |
debug.log(Level.DEBUG, state.toString()); |
|
218 |
} |
|
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* A repeatable event that can be paused or resumed by changing |
|
223 |
* its interestOps. |
|
224 |
* When the event is fired, it is first paused before being signaled. |
|
225 |
* It is the responsibility of the code triggered by {@code signalEvent} |
|
226 |
* to resume the event if required. |
|
227 |
*/ |
|
228 |
private static abstract class SocketFlowEvent extends AsyncEvent { |
|
229 |
final SocketChannel channel; |
|
230 |
final int defaultInterest; |
|
231 |
volatile int interestOps; |
|
232 |
volatile boolean registered; |
|
233 |
SocketFlowEvent(int defaultInterest, SocketChannel channel) { |
|
234 |
super(AsyncEvent.REPEATING); |
|
235 |
this.defaultInterest = defaultInterest; |
|
236 |
this.channel = channel; |
|
237 |
} |
|
238 |
final boolean registered() {return registered;} |
|
239 |
final void resume() { |
|
240 |
interestOps = defaultInterest; |
|
241 |
registered = true; |
|
242 |
} |
|
243 |
final void pause() {interestOps = 0;} |
|
244 |
@Override |
|
245 |
public final SelectableChannel channel() {return channel;} |
|
246 |
@Override |
|
247 |
public final int interestOps() {return interestOps;} |
|
248 |
||
249 |
@Override |
|
250 |
public final void handle() { |
|
251 |
pause(); // pause, then signal |
|
252 |
signalEvent(); // won't be fired again until resumed. |
|
253 |
} |
|
254 |
@Override |
|
255 |
public final void abort(IOException error) { |
|
256 |
debug().log(Level.DEBUG, () -> "abort: " + error); |
|
257 |
pause(); // pause, then signal |
|
258 |
signalError(error); // should not be resumed after abort (not checked) |
|
259 |
} |
|
260 |
||
261 |
protected abstract void signalEvent(); |
|
262 |
protected abstract void signalError(Throwable error); |
|
263 |
abstract System.Logger debug(); |
|
264 |
} |
|
265 |
||
266 |
// ===================================================================== // |
|
267 |
// Writing // |
|
268 |
// ======================================================================// |
|
269 |
||
270 |
// This class makes the assumption that the publisher will call |
|
271 |
// onNext sequentially, and that onNext won't be called if the demand |
|
272 |
// has not been incremented by request(1). |
|
273 |
// It has a 'queue of 1' meaning that it will call request(1) in |
|
274 |
// onSubscribe, and then only after its 'current' buffer list has been |
|
275 |
// fully written and current set to null; |
|
276 |
private final class InternalWriteSubscriber |
|
277 |
implements Flow.Subscriber<List<ByteBuffer>> { |
|
278 |
||
279 |
volatile Flow.Subscription subscription; |
|
280 |
volatile List<ByteBuffer> current; |
|
281 |
volatile boolean completed; |
|
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
282 |
final AsyncTriggerEvent startSubscription = |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
283 |
new AsyncTriggerEvent(this::signalError, this::startSubscription); |
48083 | 284 |
final WriteEvent writeEvent = new WriteEvent(channel, this); |
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
285 |
final Demand writeDemand = new Demand(); |
48083 | 286 |
|
287 |
@Override |
|
288 |
public void onSubscribe(Flow.Subscription subscription) { |
|
289 |
Flow.Subscription previous = this.subscription; |
|
290 |
this.subscription = subscription; |
|
291 |
debug.log(Level.DEBUG, "subscribed for writing"); |
|
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
292 |
try { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
293 |
if (current == null) { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
294 |
if (previous != subscription && previous != null) { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
295 |
debug.log(Level.DEBUG, "write: resetting demand to 0"); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
296 |
writeDemand.reset(); |
48083 | 297 |
} |
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
298 |
debug.log(Level.DEBUG, "write: registering startSubscription event"); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
299 |
client.registerEvent(startSubscription); |
48083 | 300 |
} |
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
301 |
} catch (Throwable t) { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
302 |
signalError(t); |
48083 | 303 |
} |
304 |
} |
|
305 |
||
306 |
@Override |
|
307 |
public void onNext(List<ByteBuffer> bufs) { |
|
56345
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56335
diff
changeset
|
308 |
assert current == null : dbgString() // this is a queue of 1. |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56335
diff
changeset
|
309 |
+ "w.onNext current: " + current; |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56335
diff
changeset
|
310 |
assert subscription != null : dbgString() |
eb72d194235c
http-client-branch: fix HTTP/1.1 retry to avoid duplication of header values
dfuchs
parents:
56335
diff
changeset
|
311 |
+ "w.onNext: subscription is null"; |
48083 | 312 |
current = bufs; |
313 |
tryFlushCurrent(client.isSelectorThread()); // may be in selector thread |
|
314 |
// For instance in HTTP/2, a received SETTINGS frame might trigger |
|
315 |
// the sending of a SETTINGS frame in turn which might cause |
|
316 |
// onNext to be called from within the same selector thread that the |
|
317 |
// original SETTINGS frames arrived on. If rs is the read-subscriber |
|
318 |
// and ws is the write-subscriber then the following can occur: |
|
319 |
// ReadEvent -> rs.onNext(bytes) -> process server SETTINGS -> write |
|
320 |
// client SETTINGS -> ws.onNext(bytes) -> tryFlushCurrent |
|
321 |
debugState("leaving w.onNext"); |
|
322 |
} |
|
323 |
||
324 |
// we don't use a SequentialScheduler here: we rely on |
|
325 |
// onNext() being called sequentially, and not being called |
|
326 |
// if we haven't call request(1) |
|
327 |
// onNext is usually called from within a user/executor thread. |
|
328 |
// we will perform the initial writing in that thread. |
|
329 |
// if for some reason, not all data can be written, the writeEvent |
|
330 |
// will be resumed, and the rest of the data will be written from |
|
331 |
// the selector manager thread when the writeEvent is fired. |
|
332 |
// If we are in the selector manager thread, then we will use the executor |
|
333 |
// to call request(1), ensuring that onNext() won't be called from |
|
334 |
// within the selector thread. |
|
335 |
// If we are not in the selector manager thread, then we don't care. |
|
336 |
void tryFlushCurrent(boolean inSelectorThread) { |
|
337 |
List<ByteBuffer> bufs = current; |
|
338 |
if (bufs == null) return; |
|
339 |
try { |
|
340 |
assert inSelectorThread == client.isSelectorThread() : |
|
341 |
"should " + (inSelectorThread ? "" : "not ") |
|
342 |
+ " be in the selector thread"; |
|
343 |
long remaining = Utils.remaining(bufs); |
|
344 |
debug.log(Level.DEBUG, "trying to write: %d", remaining); |
|
345 |
long written = writeAvailable(bufs); |
|
56326
63422db47911
http-client-branch: RawChannel implementation based on FlowTube
dfuchs
parents:
56315
diff
changeset
|
346 |
debug.log(Level.DEBUG, "wrote: %d", written); |
48083 | 347 |
if (written == -1) { |
348 |
signalError(new EOFException("EOF reached while writing")); |
|
349 |
return; |
|
350 |
} |
|
351 |
assert written <= remaining; |
|
352 |
if (remaining - written == 0) { |
|
353 |
current = null; |
|
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
354 |
if (writeDemand.tryDecrement()) { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
355 |
Runnable requestMore = this::requestMore; |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
356 |
if (inSelectorThread) { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
357 |
assert client.isSelectorThread(); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
358 |
client.theExecutor().execute(requestMore); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
359 |
} else { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
360 |
assert !client.isSelectorThread(); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
361 |
requestMore.run(); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
362 |
} |
48083 | 363 |
} |
364 |
} else { |
|
365 |
resumeWriteEvent(inSelectorThread); |
|
366 |
} |
|
367 |
} catch (Throwable t) { |
|
368 |
signalError(t); |
|
369 |
subscription.cancel(); |
|
370 |
} |
|
371 |
} |
|
372 |
||
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
373 |
// Kick off the initial request:1 that will start |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
374 |
// the writing side. Called from the selector manager |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
375 |
// thread. |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
376 |
void startSubscription() { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
377 |
try { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
378 |
debug.log(Level.DEBUG, "write: starting subscription"); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
379 |
assert client.isSelectorThread(); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
380 |
// make sure read registrations are handled before; |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
381 |
readPublisher.subscriptionImpl.handlePending(); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
382 |
debug.log(Level.DEBUG, "write: offloading requestMore"); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
383 |
// start writing; |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
384 |
client.theExecutor().execute(this::requestMore); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
385 |
} catch(Throwable t) { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
386 |
signalError(t); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
387 |
} |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
388 |
} |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
389 |
|
48083 | 390 |
void requestMore() { |
391 |
try { |
|
392 |
if (completed) return; |
|
393 |
long d = writeDemand.get(); |
|
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
394 |
if (writeDemand.increaseIfFulfilled()) { |
48083 | 395 |
debug.log(Level.DEBUG, "write: requesting more..."); |
396 |
subscription.request(1); |
|
397 |
} else { |
|
398 |
debug.log(Level.DEBUG, "write: no need to request more: %d", d); |
|
399 |
} |
|
400 |
} catch (Throwable t) { |
|
401 |
debug.log(Level.DEBUG, () -> |
|
402 |
"write: error while requesting more: " + t); |
|
403 |
signalError(t); |
|
404 |
subscription.cancel(); |
|
405 |
} finally { |
|
406 |
debugState("leaving requestMore: "); |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
@Override |
|
411 |
public void onError(Throwable throwable) { |
|
412 |
signalError(throwable); |
|
413 |
} |
|
414 |
||
415 |
@Override |
|
416 |
public void onComplete() { |
|
417 |
completed = true; |
|
418 |
// no need to pause the write event here: the write event will |
|
419 |
// be paused if there is nothing more to write. |
|
420 |
List<ByteBuffer> bufs = current; |
|
421 |
long remaining = bufs == null ? 0 : Utils.remaining(bufs); |
|
422 |
debug.log(Level.DEBUG, "write completed, %d yet to send", remaining); |
|
423 |
debugState("InternalWriteSubscriber::onComplete"); |
|
424 |
} |
|
425 |
||
426 |
void resumeWriteEvent(boolean inSelectorThread) { |
|
427 |
debug.log(Level.DEBUG, "scheduling write event"); |
|
428 |
resumeEvent(writeEvent, this::signalError); |
|
429 |
} |
|
430 |
||
431 |
void signalWritable() { |
|
432 |
debug.log(Level.DEBUG, "channel is writable"); |
|
433 |
tryFlushCurrent(true); |
|
434 |
} |
|
435 |
||
436 |
void signalError(Throwable error) { |
|
437 |
debug.log(Level.DEBUG, () -> "write error: " + error); |
|
438 |
completed = true; |
|
439 |
readPublisher.signalError(error); |
|
440 |
} |
|
441 |
||
442 |
// A repeatable WriteEvent which is paused after firing and can |
|
443 |
// be resumed if required - see SocketFlowEvent; |
|
444 |
final class WriteEvent extends SocketFlowEvent { |
|
445 |
final InternalWriteSubscriber sub; |
|
446 |
WriteEvent(SocketChannel channel, InternalWriteSubscriber sub) { |
|
447 |
super(SelectionKey.OP_WRITE, channel); |
|
448 |
this.sub = sub; |
|
449 |
} |
|
450 |
@Override |
|
451 |
protected final void signalEvent() { |
|
452 |
try { |
|
453 |
client.eventUpdated(this); |
|
454 |
sub.signalWritable(); |
|
455 |
} catch(Throwable t) { |
|
456 |
sub.signalError(t); |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
@Override |
|
461 |
protected void signalError(Throwable error) { |
|
462 |
sub.signalError(error); |
|
463 |
} |
|
464 |
||
465 |
@Override |
|
466 |
System.Logger debug() { |
|
467 |
return debug; |
|
468 |
} |
|
469 |
||
470 |
} |
|
471 |
||
472 |
} |
|
473 |
||
474 |
// ===================================================================== // |
|
475 |
// Reading // |
|
476 |
// ===================================================================== // |
|
477 |
||
478 |
// The InternalReadPublisher uses a SequentialScheduler to ensure that |
|
479 |
// onNext/onError/onComplete are called sequentially on the caller's |
|
480 |
// subscriber. |
|
481 |
// However, it relies on the fact that the only time where |
|
482 |
// runOrSchedule() is called from a user/executor thread is in signalError, |
|
483 |
// right after the errorRef has been set. |
|
484 |
// Because the sequential scheduler's task always checks for errors first, |
|
485 |
// and always terminate the scheduler on error, then it is safe to assume |
|
486 |
// that if it reaches the point where it reads from the channel, then |
|
487 |
// it is running in the SelectorManager thread. This is because all |
|
488 |
// other invocation of runOrSchedule() are triggered from within a |
|
489 |
// ReadEvent. |
|
490 |
// |
|
491 |
// When pausing/resuming the event, some shortcuts can then be taken |
|
492 |
// when we know we're running in the selector manager thread |
|
493 |
// (in that case there's no need to call client.eventUpdated(readEvent); |
|
494 |
// |
|
495 |
private final class InternalReadPublisher |
|
496 |
implements Flow.Publisher<List<ByteBuffer>> { |
|
497 |
private final InternalReadSubscription subscriptionImpl |
|
498 |
= new InternalReadSubscription(); |
|
499 |
AtomicReference<ReadSubscription> pendingSubscription = new AtomicReference<>(); |
|
500 |
private volatile ReadSubscription subscription; |
|
501 |
||
502 |
@Override |
|
503 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
504 |
Objects.requireNonNull(s); |
|
505 |
||
506 |
TubeSubscriber sub = FlowTube.asTubeSubscriber(s); |
|
507 |
ReadSubscription target = new ReadSubscription(subscriptionImpl, sub); |
|
508 |
ReadSubscription previous = pendingSubscription.getAndSet(target); |
|
509 |
||
510 |
if (previous != null && previous != target) { |
|
511 |
debug.log(Level.DEBUG, |
|
512 |
() -> "read publisher: dropping pending subscriber: " |
|
513 |
+ previous.subscriber); |
|
514 |
previous.errorRef.compareAndSet(null, errorRef.get()); |
|
515 |
previous.signalOnSubscribe(); |
|
516 |
if (subscriptionImpl.completed) { |
|
517 |
previous.signalCompletion(); |
|
518 |
} else { |
|
519 |
previous.subscriber.dropSubscription(); |
|
520 |
} |
|
521 |
} |
|
522 |
||
523 |
debug.log(Level.DEBUG, "read publisher got subscriber"); |
|
524 |
subscriptionImpl.signalSubscribe(); |
|
525 |
debugState("leaving read.subscribe: "); |
|
526 |
} |
|
527 |
||
528 |
void signalError(Throwable error) { |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
529 |
debug.log(Level.DEBUG, () -> "error signalled " + error); |
48083 | 530 |
if (!errorRef.compareAndSet(null, error)) { |
531 |
return; |
|
532 |
} |
|
533 |
subscriptionImpl.handleError(); |
|
534 |
} |
|
535 |
||
536 |
final class ReadSubscription implements Flow.Subscription { |
|
537 |
final InternalReadSubscription impl; |
|
538 |
final TubeSubscriber subscriber; |
|
539 |
final AtomicReference<Throwable> errorRef = new AtomicReference<>(); |
|
540 |
volatile boolean subscribed; |
|
541 |
volatile boolean cancelled; |
|
542 |
volatile boolean completed; |
|
543 |
||
544 |
public ReadSubscription(InternalReadSubscription impl, |
|
545 |
TubeSubscriber subscriber) { |
|
546 |
this.impl = impl; |
|
547 |
this.subscriber = subscriber; |
|
548 |
} |
|
549 |
||
550 |
@Override |
|
551 |
public void cancel() { |
|
552 |
cancelled = true; |
|
553 |
} |
|
554 |
||
555 |
@Override |
|
556 |
public void request(long n) { |
|
557 |
if (!cancelled) { |
|
558 |
impl.request(n); |
|
559 |
} else { |
|
560 |
debug.log(Level.DEBUG, |
|
561 |
"subscription cancelled, ignoring request %d", n); |
|
562 |
} |
|
563 |
} |
|
564 |
||
565 |
void signalCompletion() { |
|
566 |
assert subscribed || cancelled; |
|
567 |
if (completed || cancelled) return; |
|
568 |
synchronized (this) { |
|
569 |
if (completed) return; |
|
570 |
completed = true; |
|
571 |
} |
|
572 |
Throwable error = errorRef.get(); |
|
573 |
if (error != null) { |
|
574 |
debug.log(Level.DEBUG, () -> |
|
575 |
"forwarding error to subscriber: " |
|
576 |
+ error); |
|
577 |
subscriber.onError(error); |
|
578 |
} else { |
|
579 |
debug.log(Level.DEBUG, "completing subscriber"); |
|
580 |
subscriber.onComplete(); |
|
581 |
} |
|
582 |
} |
|
583 |
||
584 |
void signalOnSubscribe() { |
|
585 |
if (subscribed || cancelled) return; |
|
586 |
synchronized (this) { |
|
587 |
if (subscribed || cancelled) return; |
|
588 |
subscribed = true; |
|
589 |
} |
|
590 |
subscriber.onSubscribe(this); |
|
591 |
debug.log(Level.DEBUG, "onSubscribe called"); |
|
592 |
if (errorRef.get() != null) { |
|
593 |
signalCompletion(); |
|
594 |
} |
|
595 |
} |
|
596 |
} |
|
597 |
||
598 |
final class InternalReadSubscription implements Flow.Subscription { |
|
599 |
||
600 |
private final Demand demand = new Demand(); |
|
601 |
final SequentialScheduler readScheduler; |
|
602 |
private volatile boolean completed; |
|
603 |
private final ReadEvent readEvent; |
|
604 |
private final AsyncEvent subscribeEvent; |
|
605 |
||
606 |
InternalReadSubscription() { |
|
607 |
readScheduler = new SequentialScheduler(new SocketFlowTask(this::read)); |
|
608 |
subscribeEvent = new AsyncTriggerEvent(this::signalError, |
|
609 |
this::handleSubscribeEvent); |
|
610 |
readEvent = new ReadEvent(channel, this); |
|
611 |
} |
|
612 |
||
613 |
/* |
|
614 |
* This method must be invoked before any other method of this class. |
|
615 |
*/ |
|
616 |
final void signalSubscribe() { |
|
617 |
if (readScheduler.isStopped() || completed) { |
|
618 |
// if already completed or stopped we can handle any |
|
619 |
// pending connection directly from here. |
|
620 |
debug.log(Level.DEBUG, |
|
621 |
"handling pending subscription while completed"); |
|
622 |
handlePending(); |
|
623 |
} else { |
|
624 |
try { |
|
625 |
debug.log(Level.DEBUG, |
|
626 |
"registering subscribe event"); |
|
627 |
client.registerEvent(subscribeEvent); |
|
628 |
} catch (Throwable t) { |
|
629 |
signalError(t); |
|
630 |
handlePending(); |
|
631 |
} |
|
632 |
} |
|
633 |
} |
|
634 |
||
635 |
final void handleSubscribeEvent() { |
|
636 |
assert client.isSelectorThread(); |
|
637 |
debug.log(Level.DEBUG, "subscribe event raised"); |
|
638 |
readScheduler.runOrSchedule(); |
|
639 |
if (readScheduler.isStopped() || completed) { |
|
640 |
// if already completed or stopped we can handle any |
|
641 |
// pending connection directly from here. |
|
642 |
debug.log(Level.DEBUG, |
|
643 |
"handling pending subscription when completed"); |
|
644 |
handlePending(); |
|
645 |
} |
|
646 |
} |
|
647 |
||
648 |
||
649 |
/* |
|
650 |
* Although this method is thread-safe, the Reactive-Streams spec seems |
|
651 |
* to not require it to be as such. It's a responsibility of the |
|
652 |
* subscriber to signal demand in a thread-safe manner. |
|
653 |
* |
|
654 |
* https://github.com/reactive-streams/reactive-streams-jvm/blob/dd24d2ab164d7de6c316f6d15546f957bec29eaa/README.md |
|
655 |
* (rules 2.7 and 3.4) |
|
656 |
*/ |
|
657 |
@Override |
|
658 |
public final void request(long n) { |
|
659 |
if (n > 0L) { |
|
660 |
boolean wasFulfilled = demand.increase(n); |
|
661 |
if (wasFulfilled) { |
|
662 |
debug.log(Level.DEBUG, "got some demand for reading"); |
|
663 |
resumeReadEvent(); |
|
664 |
// if demand has been changed from fulfilled |
|
665 |
// to unfulfilled register read event; |
|
666 |
} |
|
667 |
} else { |
|
668 |
signalError(new IllegalArgumentException("non-positive request")); |
|
669 |
} |
|
670 |
debugState("leaving request("+n+"): "); |
|
671 |
} |
|
672 |
||
673 |
@Override |
|
674 |
public final void cancel() { |
|
675 |
pauseReadEvent(); |
|
676 |
readScheduler.stop(); |
|
677 |
} |
|
678 |
||
679 |
private void resumeReadEvent() { |
|
680 |
debug.log(Level.DEBUG, "resuming read event"); |
|
681 |
resumeEvent(readEvent, this::signalError); |
|
682 |
} |
|
683 |
||
684 |
private void pauseReadEvent() { |
|
685 |
debug.log(Level.DEBUG, "pausing read event"); |
|
686 |
pauseEvent(readEvent, this::signalError); |
|
687 |
} |
|
688 |
||
689 |
||
690 |
final void handleError() { |
|
691 |
assert errorRef.get() != null; |
|
692 |
readScheduler.runOrSchedule(); |
|
693 |
} |
|
694 |
||
695 |
final void signalError(Throwable error) { |
|
696 |
if (!errorRef.compareAndSet(null, error)) { |
|
697 |
return; |
|
698 |
} |
|
699 |
debug.log(Level.DEBUG, () -> "got read error: " + error); |
|
700 |
readScheduler.runOrSchedule(); |
|
701 |
} |
|
702 |
||
703 |
final void signalReadable() { |
|
704 |
readScheduler.runOrSchedule(); |
|
705 |
} |
|
706 |
||
707 |
/** The body of the task that runs in SequentialScheduler. */ |
|
708 |
final void read() { |
|
709 |
// It is important to only call pauseReadEvent() when stopping |
|
710 |
// the scheduler. The event is automatically paused before |
|
711 |
// firing, and trying to pause it again could cause a race |
|
712 |
// condition between this loop, which calls tryDecrementDemand(), |
|
713 |
// and the thread that calls request(n), which will try to resume |
|
714 |
// reading. |
|
715 |
try { |
|
716 |
while(!readScheduler.isStopped()) { |
|
717 |
if (completed) return; |
|
718 |
||
719 |
// make sure we have a subscriber |
|
720 |
if (handlePending()) { |
|
721 |
debug.log(Level.DEBUG, "pending subscriber subscribed"); |
|
722 |
return; |
|
723 |
} |
|
724 |
||
725 |
// If an error was signaled, we might not be in the |
|
726 |
// the selector thread, and that is OK, because we |
|
727 |
// will just call onError and return. |
|
728 |
ReadSubscription current = subscription; |
|
729 |
TubeSubscriber subscriber = current.subscriber; |
|
730 |
Throwable error = errorRef.get(); |
|
731 |
if (error != null) { |
|
732 |
completed = true; |
|
733 |
// safe to pause here because we're finished anyway. |
|
734 |
pauseReadEvent(); |
|
735 |
debug.log(Level.DEBUG, () -> "Sending error " + error |
|
736 |
+ " to subscriber " + subscriber); |
|
737 |
current.errorRef.compareAndSet(null, error); |
|
738 |
current.signalCompletion(); |
|
739 |
readScheduler.stop(); |
|
740 |
debugState("leaving read() loop with error: "); |
|
741 |
return; |
|
742 |
} |
|
743 |
||
744 |
// If we reach here then we must be in the selector thread. |
|
745 |
assert client.isSelectorThread(); |
|
746 |
if (demand.tryDecrement()) { |
|
747 |
// we have demand. |
|
748 |
try { |
|
749 |
List<ByteBuffer> bytes = readAvailable(); |
|
750 |
if (bytes == EOF) { |
|
751 |
if (!completed) { |
|
752 |
debug.log(Level.DEBUG, "got read EOF"); |
|
753 |
completed = true; |
|
754 |
// safe to pause here because we're finished |
|
755 |
// anyway. |
|
756 |
pauseReadEvent(); |
|
757 |
current.signalCompletion(); |
|
758 |
readScheduler.stop(); |
|
759 |
} |
|
760 |
debugState("leaving read() loop after EOF: "); |
|
761 |
return; |
|
762 |
} else if (Utils.remaining(bytes) > 0) { |
|
763 |
// the subscriber is responsible for offloading |
|
764 |
// to another thread if needed. |
|
765 |
debug.log(Level.DEBUG, () -> "read bytes: " |
|
766 |
+ Utils.remaining(bytes)); |
|
767 |
assert !current.completed; |
|
768 |
subscriber.onNext(bytes); |
|
769 |
// we could continue looping until the demand |
|
770 |
// reaches 0. However, that would risk starving |
|
771 |
// other connections (bound to other socket |
|
772 |
// channels) - as other selected keys activated |
|
773 |
// by the selector manager thread might be |
|
774 |
// waiting for this event to terminate. |
|
775 |
// So resume the read event and return now... |
|
776 |
resumeReadEvent(); |
|
777 |
debugState("leaving read() loop after onNext: "); |
|
778 |
return; |
|
779 |
} else { |
|
780 |
// nothing available! |
|
781 |
debug.log(Level.DEBUG, "no more bytes available"); |
|
782 |
// re-increment the demand and resume the read |
|
783 |
// event. This ensures that this loop is |
|
784 |
// executed again when the socket becomes |
|
785 |
// readable again. |
|
786 |
demand.increase(1); |
|
787 |
resumeReadEvent(); |
|
788 |
debugState("leaving read() loop with no bytes"); |
|
789 |
return; |
|
790 |
} |
|
791 |
} catch (Throwable x) { |
|
792 |
signalError(x); |
|
793 |
continue; |
|
794 |
} |
|
795 |
} else { |
|
796 |
debug.log(Level.DEBUG, "no more demand for reading"); |
|
797 |
// the event is paused just after firing, so it should |
|
798 |
// still be paused here, unless the demand was just |
|
799 |
// incremented from 0 to n, in which case, the |
|
800 |
// event will be resumed, causing this loop to be |
|
801 |
// invoked again when the socket becomes readable: |
|
802 |
// This is what we want. |
|
803 |
// Trying to pause the event here would actually |
|
804 |
// introduce a race condition between this loop and |
|
805 |
// request(n). |
|
806 |
debugState("leaving read() loop with no demand"); |
|
807 |
break; |
|
808 |
} |
|
809 |
} |
|
810 |
} catch (Throwable t) { |
|
811 |
debug.log(Level.DEBUG, "Unexpected exception in read loop", t); |
|
812 |
signalError(t); |
|
813 |
} finally { |
|
814 |
handlePending(); |
|
815 |
} |
|
816 |
} |
|
817 |
||
818 |
boolean handlePending() { |
|
819 |
ReadSubscription pending = pendingSubscription.getAndSet(null); |
|
820 |
if (pending == null) return false; |
|
821 |
debug.log(Level.DEBUG, "handling pending subscription for %s", |
|
822 |
pending.subscriber); |
|
823 |
ReadSubscription current = subscription; |
|
824 |
if (current != null && current != pending && !completed) { |
|
825 |
current.subscriber.dropSubscription(); |
|
826 |
} |
|
827 |
debug.log(Level.DEBUG, "read demand reset to 0"); |
|
828 |
subscriptionImpl.demand.reset(); // subscriber will increase demand if it needs to. |
|
829 |
pending.errorRef.compareAndSet(null, errorRef.get()); |
|
830 |
if (!readScheduler.isStopped()) { |
|
831 |
subscription = pending; |
|
832 |
} else { |
|
833 |
debug.log(Level.DEBUG, "socket tube is already stopped"); |
|
834 |
} |
|
835 |
debug.log(Level.DEBUG, "calling onSubscribe"); |
|
836 |
pending.signalOnSubscribe(); |
|
837 |
if (completed) { |
|
838 |
pending.errorRef.compareAndSet(null, errorRef.get()); |
|
839 |
pending.signalCompletion(); |
|
840 |
} |
|
841 |
return true; |
|
842 |
} |
|
843 |
} |
|
844 |
||
845 |
||
846 |
// A repeatable ReadEvent which is paused after firing and can |
|
847 |
// be resumed if required - see SocketFlowEvent; |
|
848 |
final class ReadEvent extends SocketFlowEvent { |
|
849 |
final InternalReadSubscription sub; |
|
850 |
ReadEvent(SocketChannel channel, InternalReadSubscription sub) { |
|
851 |
super(SelectionKey.OP_READ, channel); |
|
852 |
this.sub = sub; |
|
853 |
} |
|
854 |
@Override |
|
855 |
protected final void signalEvent() { |
|
856 |
try { |
|
857 |
client.eventUpdated(this); |
|
858 |
sub.signalReadable(); |
|
859 |
} catch(Throwable t) { |
|
860 |
sub.signalError(t); |
|
861 |
} |
|
862 |
} |
|
863 |
||
864 |
@Override |
|
865 |
protected final void signalError(Throwable error) { |
|
866 |
sub.signalError(error); |
|
867 |
} |
|
868 |
||
869 |
@Override |
|
870 |
System.Logger debug() { |
|
871 |
return debug; |
|
872 |
} |
|
873 |
} |
|
874 |
||
875 |
} |
|
876 |
||
877 |
// ===================================================================== // |
|
878 |
// Socket Channel Read/Write // |
|
879 |
// ===================================================================== // |
|
880 |
static final int MAX_BUFFERS = 3; |
|
881 |
static final List<ByteBuffer> EOF = List.of(); |
|
882 |
||
883 |
private List<ByteBuffer> readAvailable() throws IOException { |
|
884 |
ByteBuffer buf = buffersSource.get(); |
|
885 |
assert buf.hasRemaining(); |
|
886 |
||
56315
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
887 |
int read = -1; |
48083 | 888 |
int pos = buf.position(); |
889 |
List<ByteBuffer> list = null; |
|
890 |
while (buf.hasRemaining()) { |
|
56315
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
891 |
try { |
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
892 |
while ((read = channel.read(buf)) > 0) { |
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
893 |
if (!buf.hasRemaining()) break; |
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
894 |
} |
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
895 |
} catch (IOException x) { |
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
896 |
if (buf.position() == pos) { |
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
897 |
throw x; |
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
898 |
} else { |
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
899 |
errorRef.compareAndSet(null, x); |
ec8799d412fb
http-client-branch: add a test for 8199683; make sure no data is left in the queue after server closes the connection
dfuchs
parents:
56305
diff
changeset
|
900 |
} |
48083 | 901 |
} |
902 |
||
903 |
// nothing read; |
|
904 |
if (buf.position() == pos) { |
|
905 |
// An empty list signal the end of data, and should only be |
|
906 |
// returned if read == -1. |
|
907 |
// If we already read some data, then we must return what we have |
|
908 |
// read, and -1 will be returned next time the caller attempts to |
|
909 |
// read something. |
|
910 |
if (list == null && read == -1) { // eof |
|
911 |
list = EOF; |
|
912 |
break; |
|
913 |
} |
|
914 |
} |
|
915 |
buf.limit(buf.position()); |
|
916 |
buf.position(pos); |
|
917 |
if (list == null) { |
|
918 |
list = List.of(buf); |
|
919 |
} else { |
|
920 |
if (!(list instanceof ArrayList)) { |
|
921 |
list = new ArrayList<>(list); |
|
922 |
} |
|
923 |
list.add(buf); |
|
924 |
} |
|
925 |
if (read <= 0 || list.size() == MAX_BUFFERS) break; |
|
926 |
buf = buffersSource.get(); |
|
927 |
pos = buf.position(); |
|
928 |
assert buf.hasRemaining(); |
|
929 |
} |
|
930 |
return list; |
|
931 |
} |
|
932 |
||
933 |
private long writeAvailable(List<ByteBuffer> bytes) throws IOException { |
|
934 |
ByteBuffer[] srcs = bytes.toArray(Utils.EMPTY_BB_ARRAY); |
|
935 |
final long remaining = Utils.remaining(srcs); |
|
936 |
long written = 0; |
|
937 |
while (remaining > written) { |
|
938 |
long w = channel.write(srcs); |
|
939 |
if (w == -1 && written == 0) return -1; |
|
940 |
if (w == 0) break; |
|
941 |
written += w; |
|
942 |
} |
|
943 |
return written; |
|
944 |
} |
|
945 |
||
946 |
private void resumeEvent(SocketFlowEvent event, |
|
947 |
Consumer<Throwable> errorSignaler) { |
|
948 |
boolean registrationRequired; |
|
949 |
synchronized(lock) { |
|
950 |
registrationRequired = !event.registered(); |
|
951 |
event.resume(); |
|
952 |
} |
|
953 |
try { |
|
954 |
if (registrationRequired) { |
|
955 |
client.registerEvent(event); |
|
956 |
} else { |
|
957 |
client.eventUpdated(event); |
|
958 |
} |
|
959 |
} catch(Throwable t) { |
|
960 |
errorSignaler.accept(t); |
|
961 |
} |
|
962 |
} |
|
963 |
||
964 |
private void pauseEvent(SocketFlowEvent event, |
|
965 |
Consumer<Throwable> errorSignaler) { |
|
966 |
synchronized(lock) { |
|
967 |
event.pause(); |
|
968 |
} |
|
969 |
try { |
|
970 |
client.eventUpdated(event); |
|
971 |
} catch(Throwable t) { |
|
972 |
errorSignaler.accept(t); |
|
973 |
} |
|
974 |
} |
|
975 |
||
976 |
@Override |
|
977 |
public void connectFlows(TubePublisher writePublisher, |
|
978 |
TubeSubscriber readSubscriber) { |
|
979 |
debug.log(Level.DEBUG, "connecting flows"); |
|
980 |
this.subscribe(readSubscriber); |
|
981 |
writePublisher.subscribe(this); |
|
982 |
} |
|
983 |
||
984 |
||
985 |
@Override |
|
986 |
public String toString() { |
|
987 |
return dbgString(); |
|
988 |
} |
|
989 |
||
990 |
final String dbgString() { |
|
991 |
return "SocketTube("+id+")"; |
|
992 |
} |
|
993 |
} |