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