Improve cancel handling in AbstractListenerReadPublisher

Closes gh-30393
This commit is contained in:
rstoyanchev 2024-01-16 12:17:54 +00:00
parent e3f185a696
commit c4a34fa26c
3 changed files with 22 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -123,7 +123,7 @@ public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
} }
/** /**
* Subclasses can call this method to delegate a contain notification when * Subclasses can call this method to delegate a container notification when
* all data has been read. * all data has been read.
*/ */
public void onAllDataRead() { public void onAllDataRead() {
@ -362,6 +362,12 @@ public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
publisher.errorPending = ex; publisher.errorPending = ex;
publisher.handlePendingCompletionOrError(); publisher.handlePendingCompletionOrError();
} }
@Override
<T> void cancel(AbstractListenerReadPublisher<T> publisher) {
publisher.completionPending = true;
publisher.handlePendingCompletionOrError();
}
}, },
NO_DEMAND { NO_DEMAND {
@ -435,6 +441,13 @@ public abstract class AbstractListenerReadPublisher<T> implements Publisher<T> {
publisher.errorPending = ex; publisher.errorPending = ex;
publisher.handlePendingCompletionOrError(); publisher.handlePendingCompletionOrError();
} }
@Override
<T> void cancel(AbstractListenerReadPublisher<T> publisher) {
publisher.discardData();
publisher.completionPending = true;
publisher.handlePendingCompletionOrError();
}
}, },
COMPLETED { COMPLETED {

View File

@ -104,6 +104,9 @@ class ListenerReadPublisherTests {
@Override @Override
protected DataBuffer read() { protected DataBuffer read() {
if (this.discardCalls != 0) {
return null;
}
this.readCalls++; this.readCalls++;
return mock(); return mock();
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2022 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -290,8 +290,10 @@ public abstract class AbstractListenerWebSocketSession<T> extends AbstractWebSoc
@Override @Override
protected void discardData() { protected void discardData() {
Queue<Object> queue = this.pendingMessages;
this.pendingMessages = Queues.empty().get(); // prevent further reading
while (true) { while (true) {
WebSocketMessage message = (WebSocketMessage) this.pendingMessages.poll(); WebSocketMessage message = (WebSocketMessage) queue.poll();
if (message == null) { if (message == null) {
return; return;
} }