Skip to content

Commit 88e9dce

Browse files
committed
Consistently apply onCompletion/onError handling
Follow-up change in addition to dd22b8f. See gh-23096
1 parent 4a4edeb commit 88e9dce

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

spring-web/src/main/java/org/springframework/http/server/reactive/AbstractListenerReadPublisher.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,23 @@ private void changeToDemandState(State oldState) {
224224
}
225225
}
226226

227+
private void handleCompletionOrErrorBeforeDemand() {
228+
State state = this.state.get();
229+
if (!state.equals(State.UNSUBSCRIBED) && !state.equals(State.SUBSCRIBING)) {
230+
if (this.completionBeforeDemand) {
231+
rsReadLogger.trace(getLogPrefix() + "Completed before demand");
232+
this.state.get().onAllDataRead(this);
233+
}
234+
Throwable ex = this.errorBeforeDemand;
235+
if (ex != null) {
236+
if (rsReadLogger.isTraceEnabled()) {
237+
rsReadLogger.trace(getLogPrefix() + "Completed with error before demand: " + ex);
238+
}
239+
this.state.get().onError(this, ex);
240+
}
241+
}
242+
}
243+
227244
private Subscription createSubscription() {
228245
return new ReadSubscription();
229246
}
@@ -283,7 +300,7 @@ <T> void subscribe(AbstractListenerReadPublisher<T> publisher, Subscriber<? supe
283300
publisher.subscriber = subscriber;
284301
subscriber.onSubscribe(subscription);
285302
publisher.changeState(SUBSCRIBING, NO_DEMAND);
286-
handleCompletionOrErrorBeforeDemand(publisher);
303+
publisher.handleCompletionOrErrorBeforeDemand();
287304
}
288305
else {
289306
throw new IllegalStateException("Failed to transition to SUBSCRIBING, " +
@@ -294,30 +311,13 @@ <T> void subscribe(AbstractListenerReadPublisher<T> publisher, Subscriber<? supe
294311
@Override
295312
<T> void onAllDataRead(AbstractListenerReadPublisher<T> publisher) {
296313
publisher.completionBeforeDemand = true;
297-
handleCompletionOrErrorBeforeDemand(publisher);
314+
publisher.handleCompletionOrErrorBeforeDemand();
298315
}
299316

300317
@Override
301318
<T> void onError(AbstractListenerReadPublisher<T> publisher, Throwable ex) {
302319
publisher.errorBeforeDemand = ex;
303-
handleCompletionOrErrorBeforeDemand(publisher);
304-
}
305-
306-
private <T> void handleCompletionOrErrorBeforeDemand(AbstractListenerReadPublisher<T> publisher) {
307-
if (publisher.state.get().equals(NO_DEMAND)) {
308-
if (publisher.completionBeforeDemand) {
309-
rsReadLogger.trace(publisher.getLogPrefix() + "Completed before demand");
310-
publisher.state.get().onAllDataRead(publisher);
311-
}
312-
Throwable ex = publisher.errorBeforeDemand;
313-
if (ex != null) {
314-
if (rsReadLogger.isTraceEnabled()) {
315-
String prefix = publisher.getLogPrefix();
316-
rsReadLogger.trace(prefix + "Completed with error before demand: " + ex);
317-
}
318-
publisher.state.get().onError(publisher, ex);
319-
}
320-
}
320+
publisher.handleCompletionOrErrorBeforeDemand();
321321
}
322322
},
323323

@@ -337,11 +337,13 @@ <T> void request(AbstractListenerReadPublisher<T> publisher, long n) {
337337
@Override
338338
<T> void onAllDataRead(AbstractListenerReadPublisher<T> publisher) {
339339
publisher.completionBeforeDemand = true;
340+
publisher.handleCompletionOrErrorBeforeDemand();
340341
}
341342

342343
@Override
343344
<T> void onError(AbstractListenerReadPublisher<T> publisher, Throwable ex) {
344345
publisher.errorBeforeDemand = ex;
346+
publisher.handleCompletionOrErrorBeforeDemand();
345347
}
346348
},
347349

0 commit comments

Comments
 (0)