-
Notifications
You must be signed in to change notification settings - Fork 356
Send root-cause errors from connection to onClose of RSocket #797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ | |
import io.rsocket.lease.RequesterLeaseHandler; | ||
import io.rsocket.util.MonoLifecycleHandler; | ||
import java.nio.channels.ClosedChannelException; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; | ||
import java.util.function.Consumer; | ||
|
@@ -67,6 +68,7 @@ | |
import reactor.core.publisher.BaseSubscriber; | ||
import reactor.core.publisher.Flux; | ||
import reactor.core.publisher.Mono; | ||
import reactor.core.publisher.MonoProcessor; | ||
import reactor.core.publisher.SignalType; | ||
import reactor.core.publisher.UnicastProcessor; | ||
import reactor.util.concurrent.Queues; | ||
|
@@ -106,6 +108,7 @@ class RSocketRequester implements RSocket { | |
private final ByteBufAllocator allocator; | ||
private final KeepAliveFramesAcceptor keepAliveFramesAcceptor; | ||
private volatile Throwable terminationError; | ||
private final MonoProcessor<Void> onClose; | ||
|
||
RSocketRequester( | ||
DuplexConnection connection, | ||
|
@@ -126,14 +129,15 @@ class RSocketRequester implements RSocket { | |
this.leaseHandler = leaseHandler; | ||
this.senders = new SynchronizedIntObjectHashMap<>(); | ||
this.receivers = new SynchronizedIntObjectHashMap<>(); | ||
this.onClose = MonoProcessor.create(); | ||
|
||
// DO NOT Change the order here. The Send processor must be subscribed to before receiving | ||
this.sendProcessor = new UnboundedProcessor<>(); | ||
|
||
connection | ||
.onClose() | ||
.doFinally(signalType -> tryTerminateOnConnectionClose()) | ||
.subscribe(null, errorConsumer); | ||
.or(onClose) | ||
.subscribe(null, this::tryTerminateOnConnectionClose, this::tryTerminateOnConnectionClose); | ||
connection.send(sendProcessor).subscribe(null, this::handleSendProcessorError); | ||
|
||
connection.receive().subscribe(this::handleIncomingFrames, errorConsumer); | ||
|
@@ -181,17 +185,17 @@ public double availability() { | |
|
||
@Override | ||
public void dispose() { | ||
connection.dispose(); | ||
tryTerminate(() -> new CancellationException("Disposed")); | ||
} | ||
|
||
@Override | ||
public boolean isDisposed() { | ||
return connection.isDisposed(); | ||
return onClose.isDisposed(); | ||
} | ||
|
||
@Override | ||
public Mono<Void> onClose() { | ||
return connection.onClose(); | ||
return onClose; | ||
} | ||
|
||
private Mono<Void> handleFireAndForget(Payload payload) { | ||
|
@@ -619,6 +623,10 @@ private void tryTerminateOnKeepAlive(KeepAlive keepAlive) { | |
String.format("No keep-alive acks for %d ms", keepAlive.getTimeout().toMillis()))); | ||
} | ||
|
||
private void tryTerminateOnConnectionClose(Throwable e) { | ||
tryTerminate(() -> e); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
private void tryTerminateOnConnectionClose() { | ||
tryTerminate(() -> CLOSED_CHANNEL_EXCEPTION); | ||
} | ||
|
@@ -627,16 +635,16 @@ private void tryTerminateOnZeroError(ByteBuf errorFrame) { | |
tryTerminate(() -> Exceptions.from(0, errorFrame)); | ||
} | ||
|
||
private void tryTerminate(Supplier<Exception> errorSupplier) { | ||
private void tryTerminate(Supplier<Throwable> errorSupplier) { | ||
if (terminationError == null) { | ||
Exception e = errorSupplier.get(); | ||
Throwable e = errorSupplier.get(); | ||
if (TERMINATION_ERROR.compareAndSet(this, null, e)) { | ||
terminate(e); | ||
} | ||
} | ||
} | ||
|
||
private void terminate(Exception e) { | ||
private void terminate(Throwable e) { | ||
connection.dispose(); | ||
leaseHandler.dispose(); | ||
|
||
|
@@ -668,6 +676,7 @@ private void terminate(Exception e) { | |
receivers.clear(); | ||
sendProcessor.dispose(); | ||
errorConsumer.accept(e); | ||
onClose.onError(e); | ||
} | ||
|
||
private void removeStreamReceiver(int streamId) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,8 +34,12 @@ | |
import io.rsocket.internal.SynchronizedIntObjectHashMap; | ||
import io.rsocket.internal.UnboundedProcessor; | ||
import io.rsocket.lease.ResponderLeaseHandler; | ||
import java.nio.channels.ClosedChannelException; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; | ||
import java.util.function.Consumer; | ||
import java.util.function.LongConsumer; | ||
import java.util.function.Supplier; | ||
import javax.annotation.Nullable; | ||
import org.reactivestreams.Processor; | ||
import org.reactivestreams.Publisher; | ||
|
@@ -58,13 +62,21 @@ class RSocketResponder implements ResponderRSocket { | |
} | ||
} | ||
}; | ||
private static final Exception CLOSED_CHANNEL_EXCEPTION = new ClosedChannelException(); | ||
|
||
private final DuplexConnection connection; | ||
private final RSocket requestHandler; | ||
private final ResponderRSocket responderRSocket; | ||
private final PayloadDecoder payloadDecoder; | ||
private final Consumer<Throwable> errorConsumer; | ||
private final ResponderLeaseHandler leaseHandler; | ||
private final Disposable leaseHandlerDisposable; | ||
private final MonoProcessor<Void> onClose; | ||
|
||
private volatile Throwable terminationError; | ||
private static final AtomicReferenceFieldUpdater<RSocketResponder, Throwable> TERMINATION_ERROR = | ||
AtomicReferenceFieldUpdater.newUpdater( | ||
RSocketResponder.class, Throwable.class, "terminationError"); | ||
|
||
private final int mtu; | ||
|
||
|
@@ -94,28 +106,21 @@ class RSocketResponder implements ResponderRSocket { | |
this.leaseHandler = leaseHandler; | ||
this.sendingSubscriptions = new SynchronizedIntObjectHashMap<>(); | ||
this.channelProcessors = new SynchronizedIntObjectHashMap<>(); | ||
this.onClose = MonoProcessor.create(); | ||
|
||
// DO NOT Change the order here. The Send processor must be subscribed to before receiving | ||
// connections | ||
this.sendProcessor = new UnboundedProcessor<>(); | ||
|
||
connection | ||
.send(sendProcessor) | ||
.doFinally(this::handleSendProcessorCancel) | ||
.subscribe(null, this::handleSendProcessorError); | ||
connection.send(sendProcessor).subscribe(null, this::handleSendProcessorError); | ||
|
||
Disposable receiveDisposable = connection.receive().subscribe(this::handleFrame, errorConsumer); | ||
Disposable sendLeaseDisposable = leaseHandler.send(sendProcessor::onNextPrioritized); | ||
connection.receive().subscribe(this::handleFrame, errorConsumer); | ||
leaseHandlerDisposable = leaseHandler.send(sendProcessor::onNextPrioritized); | ||
|
||
this.connection | ||
.onClose() | ||
.doFinally( | ||
s -> { | ||
cleanup(); | ||
receiveDisposable.dispose(); | ||
sendLeaseDisposable.dispose(); | ||
}) | ||
.subscribe(null, errorConsumer); | ||
.or(onClose) | ||
.subscribe(null, this::tryTerminateOnConnectionClose, this::tryTerminateOnConnectionClose); | ||
} | ||
|
||
private void handleSendProcessorError(Throwable t) { | ||
|
@@ -142,32 +147,21 @@ private void handleSendProcessorError(Throwable t) { | |
}); | ||
} | ||
|
||
private void handleSendProcessorCancel(SignalType t) { | ||
if (SignalType.ON_ERROR == t) { | ||
return; | ||
} | ||
private void tryTerminateOnConnectionClose(Throwable e) { | ||
tryTerminate(() -> e); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
sendingSubscriptions | ||
.values() | ||
.forEach( | ||
subscription -> { | ||
try { | ||
subscription.cancel(); | ||
} catch (Throwable e) { | ||
errorConsumer.accept(e); | ||
} | ||
}); | ||
private void tryTerminateOnConnectionClose() { | ||
tryTerminate(() -> CLOSED_CHANNEL_EXCEPTION); | ||
} | ||
|
||
channelProcessors | ||
.values() | ||
.forEach( | ||
subscription -> { | ||
try { | ||
subscription.onComplete(); | ||
} catch (Throwable e) { | ||
errorConsumer.accept(e); | ||
} | ||
}); | ||
private void tryTerminate(Supplier<Throwable> errorSupplier) { | ||
if (terminationError == null) { | ||
Throwable e = errorSupplier.get(); | ||
if (TERMINATION_ERROR.compareAndSet(this, null, e)) { | ||
cleanup(e); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
|
@@ -250,23 +244,25 @@ public Mono<Void> metadataPush(Payload payload) { | |
|
||
@Override | ||
public void dispose() { | ||
connection.dispose(); | ||
tryTerminate(() -> new CancellationException("Disposed")); | ||
} | ||
|
||
@Override | ||
public boolean isDisposed() { | ||
return connection.isDisposed(); | ||
return onClose.isDisposed(); | ||
} | ||
|
||
@Override | ||
public Mono<Void> onClose() { | ||
return connection.onClose(); | ||
return onClose; | ||
} | ||
|
||
private void cleanup() { | ||
private void cleanup(Throwable e) { | ||
cleanUpSendingSubscriptions(); | ||
cleanUpChannelProcessors(); | ||
cleanUpChannelProcessors(e); | ||
|
||
connection.dispose(); | ||
leaseHandlerDisposable.dispose(); | ||
requestHandler.dispose(); | ||
sendProcessor.dispose(); | ||
} | ||
|
@@ -276,8 +272,16 @@ private synchronized void cleanUpSendingSubscriptions() { | |
sendingSubscriptions.clear(); | ||
} | ||
|
||
private synchronized void cleanUpChannelProcessors() { | ||
channelProcessors.values().forEach(Processor::onComplete); | ||
private synchronized void cleanUpChannelProcessors(Throwable e) { | ||
channelProcessors | ||
.values() | ||
.forEach(payloadPayloadProcessor -> { | ||
try { | ||
payloadPayloadProcessor.onError(e); | ||
} catch (Throwable t) { | ||
// noops | ||
} | ||
}); | ||
channelProcessors.clear(); | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.