Skip to content

Commit

Permalink
Use doFinally in order to handle cancel and error signals (#3356)
Browse files Browse the repository at this point in the history
  • Loading branch information
violetagg authored Jul 17, 2024
1 parent 8fe4229 commit 0bd9db9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
3 changes: 3 additions & 0 deletions reactor-netty-http/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ task japicmp(type: JapicmpTask) {

compatibilityChangeExcludes = [ "METHOD_NEW_DEFAULT" ]
methodExcludes = [
'reactor.netty.http.HttpOperations$PostHeadersNettyOutbound#accept(java.lang.Object)',
'reactor.netty.http.HttpOperations$PostHeadersNettyOutbound#accept(java.lang.Throwable)',
'reactor.netty.http.HttpOperations$PostHeadersNettyOutbound#run()'
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import io.netty.util.ReferenceCountUtil;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import reactor.core.publisher.SignalType;
import reactor.netty.Connection;
import reactor.netty.ConnectionObserver;
import reactor.netty.FutureMono;
Expand Down Expand Up @@ -469,7 +470,7 @@ protected HttpMessage prepareHttpMessage(ByteBuf buffer) {

static final Pattern SCHEME_PATTERN = Pattern.compile("^(https?|wss?)://.*$");

protected static final class PostHeadersNettyOutbound extends AtomicBoolean implements NettyOutbound, Consumer<Throwable>, Runnable {
protected static final class PostHeadersNettyOutbound extends AtomicBoolean implements NettyOutbound {

final Mono<Void> source;
final HttpOperations<?, ?> parent;
Expand All @@ -478,29 +479,20 @@ protected static final class PostHeadersNettyOutbound extends AtomicBoolean impl
public PostHeadersNettyOutbound(Mono<Void> source, HttpOperations<?, ?> parent, @Nullable ByteBuf msg) {
this.msg = msg;
if (msg != null) {
this.source = source.doOnError(this)
.doOnCancel(this);
this.source = source.doFinally(signalType -> {
if (signalType == SignalType.CANCEL || signalType == SignalType.ON_ERROR) {
if (msg.refCnt() > 0 && compareAndSet(false, true)) {
msg.release();
}
}
});
}
else {
this.source = source;
}
this.parent = parent;
}

@Override
public void run() {
if (msg != null && msg.refCnt() > 0 && compareAndSet(false, true)) {
msg.release();
}
}

@Override
public void accept(Throwable throwable) {
if (msg != null && msg.refCnt() > 0 && compareAndSet(false, true)) {
msg.release();
}
}

@Override
public Mono<Void> then() {
return source;
Expand Down

0 comments on commit 0bd9db9

Please sign in to comment.