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