fix(3.2): The oneToOne method of the ReactorServerCalls class will cause the request to hang when the result is Mono Empty (#14121)

* Fix triple reactor request hung when result is Mono Empty

* code format

* fix compile

---------

Co-authored-by: caoyanan <caoyanan@growingio.com>
This commit is contained in:
caoyanan666 2024-05-08 14:17:18 +08:00 committed by GitHub
parent dfd135eb95
commit 1abe9f8087
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 20 additions and 11 deletions

View File

@ -19,10 +19,11 @@ package org.apache.dubbo.reactive.calls;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.reactive.ServerTripleReactorPublisher;
import org.apache.dubbo.reactive.ServerTripleReactorSubscriber;
import org.apache.dubbo.rpc.StatusRpcException;
import org.apache.dubbo.rpc.TriRpcStatus;
import org.apache.dubbo.rpc.protocol.tri.observer.CallStreamObserver;
import org.apache.dubbo.rpc.protocol.tri.observer.ServerCallToObserverAdapter;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import reactor.core.publisher.Flux;
@ -43,16 +44,18 @@ public final class ReactorServerCalls {
* @param func service implementation
*/
public static <T, R> void oneToOne(T request, StreamObserver<R> responseObserver, Function<Mono<T>, Mono<R>> func) {
func.apply(Mono.just(request)).subscribe(res -> {
CompletableFuture.completedFuture(res).whenComplete((r, t) -> {
if (t != null) {
responseObserver.onError(t);
} else {
responseObserver.onNext(r);
responseObserver.onCompleted();
}
});
});
try {
func.apply(Mono.just(request))
.subscribe(
res -> {
responseObserver.onNext(res);
responseObserver.onCompleted();
},
throwable -> doOnResponseHasException(throwable, responseObserver),
() -> doOnResponseHasException(TriRpcStatus.NOT_FOUND.asException(), responseObserver));
} catch (Throwable throwable) {
doOnResponseHasException(throwable, responseObserver);
}
}
/**
@ -131,4 +134,10 @@ public final class ReactorServerCalls {
return serverPublisher;
}
private static void doOnResponseHasException(Throwable throwable, StreamObserver<?> responseObserver) {
StatusRpcException statusRpcException =
TriRpcStatus.getStatus(throwable).asException();
responseObserver.onError(statusRpcException);
}
}