author | dfuchs |
Fri, 16 Mar 2018 12:32:34 +0000 | |
branch | http-client-branch |
changeset 56315 | ec8799d412fb |
parent 56234 | fc391230cf7b |
child 56437 | f8b3f053cfbb |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
56088
38fac6d0521d
http-client-branch: update copyright year ranges in prep for file moves
chegar
parents:
56044
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.common; |
48083 | 27 |
|
28 |
import java.io.Closeable; |
|
29 |
import java.lang.System.Logger.Level; |
|
30 |
import java.nio.ByteBuffer; |
|
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:
56234
diff
changeset
|
31 |
import java.util.ArrayList; |
48083 | 32 |
import java.util.List; |
33 |
import java.util.Objects; |
|
34 |
import java.util.concurrent.CompletableFuture; |
|
35 |
import java.util.concurrent.ConcurrentLinkedQueue; |
|
36 |
import java.util.concurrent.Flow; |
|
37 |
import java.util.concurrent.Flow.Subscriber; |
|
38 |
import java.util.concurrent.atomic.AtomicLong; |
|
39 |
import java.util.concurrent.atomic.AtomicReference; |
|
40 |
||
41 |
/** |
|
42 |
* A wrapper for a Flow.Subscriber. This wrapper delivers data to the wrapped |
|
43 |
* Subscriber which is supplied to the constructor. This class takes care of |
|
44 |
* downstream flow control automatically and upstream flow control automatically |
|
45 |
* by default. |
|
46 |
* <p> |
|
47 |
* Processing is done by implementing the {@link #incoming(List, boolean)} method |
|
48 |
* which supplies buffers from upstream. This method (or any other method) |
|
49 |
* can then call the outgoing() method to deliver processed buffers downstream. |
|
50 |
* <p> |
|
51 |
* Upstream error signals are delivered downstream directly. Cancellation from |
|
52 |
* downstream is also propagated upstream immediately. |
|
53 |
* <p> |
|
54 |
* Each SubscriberWrapper has a {@link java.util.concurrent.CompletableFuture}{@code <Void>} |
|
55 |
* which propagates completion/errors from downstream to upstream. Normal completion |
|
56 |
* can only occur after onComplete() is called, but errors can be propagated upwards |
|
57 |
* at any time. |
|
58 |
*/ |
|
59 |
public abstract class SubscriberWrapper |
|
60 |
implements FlowTube.TubeSubscriber, Closeable, Flow.Processor<List<ByteBuffer>,List<ByteBuffer>> |
|
61 |
// TODO: SSLTube Subscriber will never change? Does this really need to be a TS? |
|
62 |
{ |
|
63 |
static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag. |
|
64 |
final System.Logger logger = |
|
65 |
Utils.getDebugLogger(this::dbgString, DEBUG); |
|
66 |
||
67 |
public enum SchedulingAction { CONTINUE, RETURN, RESCHEDULE } |
|
68 |
||
69 |
volatile Flow.Subscription upstreamSubscription; |
|
70 |
final SubscriptionBase downstreamSubscription; |
|
71 |
volatile boolean upstreamCompleted; |
|
72 |
volatile boolean downstreamCompleted; |
|
73 |
volatile boolean completionAcknowledged; |
|
74 |
private volatile Subscriber<? super List<ByteBuffer>> downstreamSubscriber; |
|
75 |
// processed byte to send to the downstream subscriber. |
|
76 |
private final ConcurrentLinkedQueue<List<ByteBuffer>> outputQ; |
|
77 |
private final CompletableFuture<Void> cf; |
|
78 |
private final SequentialScheduler pushScheduler; |
|
79 |
private final AtomicReference<Throwable> errorRef = new AtomicReference<>(); |
|
80 |
final AtomicLong upstreamWindow = new AtomicLong(0); |
|
81 |
||
82 |
/** |
|
83 |
* Wraps the given downstream subscriber. For each call to {@link |
|
84 |
* #onNext(List<ByteBuffer>) } the given filter function is invoked |
|
85 |
* and the list (if not empty) returned is passed downstream. |
|
86 |
* |
|
87 |
* A {@code CompletableFuture} is supplied which can be used to signal an |
|
88 |
* error from downstream and which terminates the wrapper or which signals |
|
89 |
* completion of downstream activity which can be propagated upstream. Error |
|
90 |
* completion can be signaled at any time, but normal completion must not be |
|
91 |
* signaled before onComplete() is called. |
|
92 |
*/ |
|
93 |
public SubscriberWrapper() |
|
94 |
{ |
|
95 |
this.outputQ = new ConcurrentLinkedQueue<>(); |
|
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
96 |
this.cf = new MinimalFuture<Void>(); |
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
97 |
cf.whenComplete((v,t) -> { |
56207
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56092
diff
changeset
|
98 |
if (t != null) |
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
99 |
errorCommon(t); |
56207
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56092
diff
changeset
|
100 |
}); |
48083 | 101 |
this.pushScheduler = |
102 |
SequentialScheduler.synchronizedScheduler(new DownstreamPusher()); |
|
103 |
this.downstreamSubscription = new SubscriptionBase(pushScheduler, |
|
104 |
this::downstreamCompletion); |
|
105 |
} |
|
106 |
||
107 |
@Override |
|
108 |
public final void subscribe(Subscriber<? super List<ByteBuffer>> downstreamSubscriber) { |
|
109 |
Objects.requireNonNull(downstreamSubscriber); |
|
110 |
this.downstreamSubscriber = downstreamSubscriber; |
|
111 |
} |
|
112 |
||
113 |
/** |
|
114 |
* Wraps the given downstream wrapper in this. For each call to |
|
115 |
* {@link #onNext(List<ByteBuffer>) } the incoming() method is called. |
|
116 |
* |
|
117 |
* The {@code downstreamCF} from the downstream wrapper is linked to this |
|
118 |
* wrappers notifier. |
|
119 |
* |
|
120 |
* @param downstreamWrapper downstream destination |
|
121 |
*/ |
|
122 |
public SubscriberWrapper(Subscriber<? super List<ByteBuffer>> downstreamWrapper) |
|
123 |
{ |
|
124 |
this(); |
|
125 |
subscribe(downstreamWrapper); |
|
126 |
} |
|
127 |
||
128 |
/** |
|
129 |
* Delivers data to be processed by this wrapper. Generated data to be sent |
|
130 |
* downstream, must be provided to the {@link #outgoing(List, boolean)}} |
|
131 |
* method. |
|
132 |
* |
|
133 |
* @param buffers a List of ByteBuffers. |
|
134 |
* @param complete if true then no more data will be added to the list |
|
135 |
*/ |
|
136 |
protected abstract void incoming(List<ByteBuffer> buffers, boolean complete); |
|
137 |
||
138 |
/** |
|
139 |
* This method is called to determine the window size to use at any time. The |
|
140 |
* current window is supplied together with the current downstream queue size. |
|
141 |
* {@code 0} should be returned if no change is |
|
142 |
* required or a positive integer which will be added to the current window. |
|
143 |
* The default implementation maintains a downstream queue size of no greater |
|
144 |
* than 5. The method can be overridden if required. |
|
145 |
* |
|
146 |
* @param currentWindow the current upstream subscription window |
|
147 |
* @param downstreamQsize the current number of buffers waiting to be sent |
|
148 |
* downstream |
|
149 |
* |
|
150 |
* @return value to add to currentWindow |
|
151 |
*/ |
|
152 |
protected long upstreamWindowUpdate(long currentWindow, long downstreamQsize) { |
|
153 |
if (downstreamQsize > 5) { |
|
154 |
return 0; |
|
155 |
} |
|
156 |
||
157 |
if (currentWindow == 0) { |
|
158 |
return 1; |
|
159 |
} else { |
|
160 |
return 0; |
|
161 |
} |
|
162 |
} |
|
163 |
||
164 |
/** |
|
165 |
* Override this if anything needs to be done after the upstream subscriber |
|
166 |
* has subscribed |
|
167 |
*/ |
|
168 |
protected void onSubscribe() { |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Override this if anything needs to be done before checking for error |
|
173 |
* and processing the input queue. |
|
174 |
* @return |
|
175 |
*/ |
|
176 |
protected SchedulingAction enterScheduling() { |
|
177 |
return SchedulingAction.CONTINUE; |
|
178 |
} |
|
179 |
||
180 |
protected boolean signalScheduling() { |
|
181 |
if (downstreamCompleted || pushScheduler.isStopped()) { |
|
182 |
return false; |
|
183 |
} |
|
184 |
pushScheduler.runOrSchedule(); |
|
185 |
return true; |
|
186 |
} |
|
187 |
||
188 |
/** |
|
189 |
* Delivers buffers of data downstream. After incoming() |
|
190 |
* has been called complete == true signifying completion of the upstream |
|
191 |
* subscription, data may continue to be delivered, up to when outgoing() is |
|
192 |
* called complete == true, after which, the downstream subscription is |
|
193 |
* completed. |
|
194 |
* |
|
195 |
* It's an error to call outgoing() with complete = true if incoming() has |
|
196 |
* not previously been called with it. |
|
197 |
*/ |
|
198 |
public void outgoing(ByteBuffer buffer, boolean complete) { |
|
199 |
Objects.requireNonNull(buffer); |
|
200 |
assert !complete || !buffer.hasRemaining(); |
|
201 |
outgoing(List.of(buffer), complete); |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* Sometime it might be necessary to complete the downstream subscriber |
|
206 |
* before the upstream completes. For instance, when an SSL server |
|
207 |
* sends a notify_close. In that case we should let the outgoing |
|
56234
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
208 |
* complete before upstream is completed. |
48083 | 209 |
* @return true, may be overridden by subclasses. |
210 |
*/ |
|
211 |
public boolean closing() { |
|
212 |
return false; |
|
213 |
} |
|
214 |
||
215 |
public void outgoing(List<ByteBuffer> buffers, boolean complete) { |
|
216 |
Objects.requireNonNull(buffers); |
|
217 |
if (complete) { |
|
218 |
assert Utils.remaining(buffers) == 0; |
|
219 |
boolean closing = closing(); |
|
220 |
logger.log(Level.DEBUG, |
|
56234
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
221 |
"completionAcknowledged upstreamCompleted:%s," |
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
222 |
+ " downstreamCompleted:%s, closing:%s", |
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
223 |
upstreamCompleted, downstreamCompleted, closing); |
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
224 |
if (!upstreamCompleted && !closing) { |
48083 | 225 |
throw new IllegalStateException("upstream not completed"); |
56234
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
226 |
} |
48083 | 227 |
completionAcknowledged = true; |
228 |
} else { |
|
229 |
logger.log(Level.DEBUG, () -> "Adding " |
|
56234
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
230 |
+ Utils.remaining(buffers) + " to outputQ queue"); |
48083 | 231 |
outputQ.add(buffers); |
232 |
} |
|
56234
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
233 |
logger.log(Level.DEBUG, () -> "pushScheduler" |
48083 | 234 |
+ (pushScheduler.isStopped() ? " is stopped!" : " is alive")); |
235 |
pushScheduler.runOrSchedule(); |
|
236 |
} |
|
237 |
||
238 |
/** |
|
239 |
* Returns a CompletableFuture which completes when this wrapper completes. |
|
240 |
* Normal completion happens with the following steps (in order): |
|
241 |
* 1. onComplete() is called |
|
242 |
* 2. incoming() called with complete = true |
|
243 |
* 3. outgoing() may continue to be called normally |
|
244 |
* 4. outgoing called with complete = true |
|
245 |
* 5. downstream subscriber is called onComplete() |
|
246 |
* |
|
247 |
* If the subscription is canceled or onComplete() is invoked the |
|
248 |
* CompletableFuture completes exceptionally. Exceptional completion |
|
249 |
* also occurs if downstreamCF completes exceptionally. |
|
250 |
*/ |
|
251 |
public CompletableFuture<Void> completion() { |
|
252 |
return cf; |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* Invoked whenever it 'may' be possible to push buffers downstream. |
|
257 |
*/ |
|
258 |
class DownstreamPusher implements Runnable { |
|
259 |
@Override |
|
260 |
public void run() { |
|
261 |
try { |
|
262 |
run1(); |
|
263 |
} catch (Throwable t) { |
|
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
264 |
errorCommon(t); |
48083 | 265 |
} |
266 |
} |
|
267 |
||
268 |
private void run1() { |
|
269 |
if (downstreamCompleted) { |
|
270 |
logger.log(Level.DEBUG, "DownstreamPusher: downstream is already completed"); |
|
271 |
return; |
|
272 |
} |
|
273 |
switch (enterScheduling()) { |
|
274 |
case CONTINUE: break; |
|
275 |
case RESCHEDULE: pushScheduler.runOrSchedule(); return; |
|
276 |
case RETURN: return; |
|
277 |
default: |
|
278 |
errorRef.compareAndSet(null, |
|
279 |
new InternalError("unknown scheduling command")); |
|
280 |
break; |
|
281 |
} |
|
282 |
// If there was an error, send it downstream. |
|
283 |
Throwable error = errorRef.get(); |
|
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:
56234
diff
changeset
|
284 |
if (error != null && outputQ.isEmpty()) { |
48083 | 285 |
synchronized(this) { |
56234
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
286 |
if (downstreamCompleted) |
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
287 |
return; |
48083 | 288 |
downstreamCompleted = true; |
289 |
} |
|
290 |
logger.log(Level.DEBUG, |
|
291 |
() -> "DownstreamPusher: forwarding error downstream: " + error); |
|
292 |
pushScheduler.stop(); |
|
293 |
outputQ.clear(); |
|
294 |
downstreamSubscriber.onError(error); |
|
295 |
return; |
|
296 |
} |
|
297 |
||
298 |
// OK - no error, let's proceed |
|
299 |
if (!outputQ.isEmpty()) { |
|
300 |
logger.log(Level.DEBUG, |
|
301 |
"DownstreamPusher: queue not empty, downstreamSubscription: %s", |
|
302 |
downstreamSubscription); |
|
303 |
} else { |
|
304 |
logger.log(Level.DEBUG, |
|
305 |
"DownstreamPusher: queue empty, downstreamSubscription: %s", |
|
306 |
downstreamSubscription); |
|
307 |
} |
|
308 |
||
309 |
final boolean dbgOn = logger.isLoggable(Level.DEBUG); |
|
310 |
while (!outputQ.isEmpty() && downstreamSubscription.tryDecrement()) { |
|
311 |
List<ByteBuffer> b = outputQ.poll(); |
|
312 |
if (dbgOn) logger.log(Level.DEBUG, |
|
313 |
"DownstreamPusher: Pushing " |
|
314 |
+ Utils.remaining(b) |
|
315 |
+ " bytes downstream"); |
|
316 |
downstreamSubscriber.onNext(b); |
|
317 |
} |
|
318 |
upstreamWindowUpdate(); |
|
319 |
checkCompletion(); |
|
320 |
} |
|
321 |
} |
|
322 |
||
323 |
void upstreamWindowUpdate() { |
|
324 |
long downstreamQueueSize = outputQ.size(); |
|
56234
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
325 |
long upstreamWindowSize = upstreamWindow.get(); |
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
326 |
long n = upstreamWindowUpdate(upstreamWindowSize, downstreamQueueSize); |
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
327 |
logger.log(Level.DEBUG, "upstreamWindowUpdate, " |
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
328 |
+ "downstreamQueueSize:%d, upstreamWindow:%d", |
fc391230cf7b
http-client-branch: some SSLFlowDelegate fixes from Daniel and Chris
chegar
parents:
56225
diff
changeset
|
329 |
downstreamQueueSize, upstreamWindowSize); |
48083 | 330 |
if (n > 0) |
331 |
upstreamRequest(n); |
|
332 |
} |
|
333 |
||
334 |
@Override |
|
335 |
public void onSubscribe(Flow.Subscription subscription) { |
|
336 |
if (upstreamSubscription != null) { |
|
337 |
throw new IllegalStateException("Single shot publisher"); |
|
338 |
} |
|
339 |
this.upstreamSubscription = subscription; |
|
340 |
upstreamRequest(upstreamWindowUpdate(0, 0)); |
|
341 |
logger.log(Level.DEBUG, |
|
342 |
"calling downstreamSubscriber::onSubscribe on %s", |
|
343 |
downstreamSubscriber); |
|
344 |
downstreamSubscriber.onSubscribe(downstreamSubscription); |
|
345 |
onSubscribe(); |
|
346 |
} |
|
347 |
||
348 |
@Override |
|
349 |
public void onNext(List<ByteBuffer> item) { |
|
350 |
logger.log(Level.DEBUG, "onNext"); |
|
351 |
long prev = upstreamWindow.getAndDecrement(); |
|
352 |
if (prev <= 0) |
|
353 |
throw new IllegalStateException("invalid onNext call"); |
|
354 |
incomingCaller(item, false); |
|
355 |
upstreamWindowUpdate(); |
|
356 |
} |
|
357 |
||
358 |
private void upstreamRequest(long n) { |
|
359 |
logger.log(Level.DEBUG, "requesting %d", n); |
|
360 |
upstreamWindow.getAndAdd(n); |
|
361 |
upstreamSubscription.request(n); |
|
362 |
} |
|
363 |
||
364 |
protected void requestMore() { |
|
365 |
if (upstreamWindow.get() == 0) { |
|
366 |
upstreamRequest(1); |
|
367 |
} |
|
368 |
} |
|
369 |
||
370 |
public long upstreamWindow() { |
|
371 |
return upstreamWindow.get(); |
|
372 |
} |
|
373 |
||
374 |
@Override |
|
375 |
public void onError(Throwable throwable) { |
|
376 |
logger.log(Level.DEBUG, () -> "onError: " + throwable); |
|
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
377 |
errorCommon(Objects.requireNonNull(throwable)); |
48083 | 378 |
} |
379 |
||
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
380 |
protected boolean errorCommon(Throwable throwable) { |
56044
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
55973
diff
changeset
|
381 |
assert throwable != null || |
a8423a38386e
http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents:
55973
diff
changeset
|
382 |
(throwable = new AssertionError("null throwable")) != null; |
48083 | 383 |
if (errorRef.compareAndSet(null, throwable)) { |
384 |
logger.log(Level.DEBUG, "error", throwable); |
|
385 |
pushScheduler.runOrSchedule(); |
|
386 |
upstreamCompleted = true; |
|
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
387 |
cf.completeExceptionally(throwable); |
48083 | 388 |
return true; |
389 |
} |
|
390 |
return false; |
|
391 |
} |
|
392 |
||
393 |
@Override |
|
394 |
public void close() { |
|
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
395 |
errorCommon(new RuntimeException("wrapper closed")); |
56207
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56092
diff
changeset
|
396 |
} |
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56092
diff
changeset
|
397 |
|
03c89ed2070f
http-client-branch: Fixed SSLFlowDelegate to not call Subscriber.onError directly
michaelm
parents:
56092
diff
changeset
|
398 |
public void close(Throwable t) { |
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
399 |
errorCommon(t); |
48083 | 400 |
} |
401 |
||
402 |
private void incomingCaller(List<ByteBuffer> l, boolean complete) { |
|
403 |
try { |
|
404 |
incoming(l, complete); |
|
405 |
} catch(Throwable t) { |
|
56208
d37c08ce784a
http-client-branch: tweaked last revision wrong CF reference was being kept
michaelm
parents:
56207
diff
changeset
|
406 |
errorCommon(t); |
48083 | 407 |
} |
408 |
} |
|
409 |
||
410 |
@Override |
|
411 |
public void onComplete() { |
|
412 |
logger.log(Level.DEBUG, () -> "upstream completed: " + toString()); |
|
413 |
upstreamCompleted = true; |
|
414 |
incomingCaller(Utils.EMPTY_BB_LIST, true); |
|
415 |
// pushScheduler will call checkCompletion() |
|
416 |
pushScheduler.runOrSchedule(); |
|
417 |
} |
|
418 |
||
419 |
/** Adds the given data to the input queue. */ |
|
420 |
public void addData(ByteBuffer l) { |
|
421 |
if (upstreamSubscription == null) { |
|
422 |
throw new IllegalStateException("can't add data before upstream subscriber subscribes"); |
|
423 |
} |
|
424 |
incomingCaller(List.of(l), false); |
|
425 |
} |
|
426 |
||
427 |
void checkCompletion() { |
|
428 |
if (downstreamCompleted || !upstreamCompleted) { |
|
429 |
return; |
|
430 |
} |
|
431 |
if (!outputQ.isEmpty()) { |
|
432 |
return; |
|
433 |
} |
|
434 |
if (errorRef.get() != null) { |
|
435 |
pushScheduler.runOrSchedule(); |
|
436 |
return; |
|
437 |
} |
|
438 |
if (completionAcknowledged) { |
|
439 |
logger.log(Level.DEBUG, "calling downstreamSubscriber.onComplete()"); |
|
440 |
downstreamSubscriber.onComplete(); |
|
441 |
// Fix me subscriber.onComplete.run(); |
|
442 |
downstreamCompleted = true; |
|
443 |
cf.complete(null); |
|
444 |
} |
|
445 |
} |
|
446 |
||
447 |
// called from the downstream Subscription.cancel() |
|
448 |
void downstreamCompletion() { |
|
449 |
upstreamSubscription.cancel(); |
|
450 |
cf.complete(null); |
|
451 |
} |
|
452 |
||
453 |
public void resetDownstreamDemand() { |
|
454 |
downstreamSubscription.demand.reset(); |
|
455 |
} |
|
456 |
||
457 |
@Override |
|
458 |
public String toString() { |
|
459 |
StringBuilder sb = new StringBuilder(); |
|
460 |
sb.append("SubscriberWrapper:") |
|
461 |
.append(" upstreamCompleted: ").append(Boolean.toString(upstreamCompleted)) |
|
462 |
.append(" upstreamWindow: ").append(upstreamWindow.toString()) |
|
463 |
.append(" downstreamCompleted: ").append(Boolean.toString(downstreamCompleted)) |
|
464 |
.append(" completionAcknowledged: ").append(Boolean.toString(completionAcknowledged)) |
|
465 |
.append(" outputQ size: ").append(Integer.toString(outputQ.size())) |
|
466 |
//.append(" outputQ: ").append(outputQ.toString()) |
|
467 |
.append(" cf: ").append(cf.toString()) |
|
468 |
.append(" downstreamSubscription: ").append(downstreamSubscription.toString()); |
|
469 |
||
470 |
return sb.toString(); |
|
471 |
} |
|
472 |
||
473 |
public String dbgString() { |
|
474 |
return "SubscriberWrapper"; |
|
475 |
} |
|
476 |
} |