forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReactorServerCalls.java
143 lines (132 loc) · 5.94 KB
/
ReactorServerCalls.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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.function.Function;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* A collection of methods to convert server-side stream calls to Reactor calls.
*/
public final class ReactorServerCalls {
private ReactorServerCalls() {}
/**
* Implements a unary -> unary call as Mono -> Mono
*
* @param request request
* @param responseObserver response StreamObserver
* @param func service implementation
*/
public static <T, R> void oneToOne(T request, StreamObserver<R> responseObserver, Function<Mono<T>, Mono<R>> func) {
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);
}
}
/**
* Implements a unary -> stream call as Mono -> Flux
*
* @param request request
* @param responseObserver response StreamObserver
* @param func service implementation
*/
public static <T, R> void oneToMany(
T request, StreamObserver<R> responseObserver, Function<Mono<T>, Flux<R>> func) {
try {
Flux<R> response = func.apply(Mono.just(request));
ServerTripleReactorSubscriber<R> subscriber = response.subscribeWith(new ServerTripleReactorSubscriber<>());
subscriber.subscribe((ServerCallToObserverAdapter<R>) responseObserver);
} catch (Throwable throwable) {
responseObserver.onError(throwable);
}
}
/**
* Implements a stream -> unary call as Flux -> Mono
*
* @param responseObserver response StreamObserver
* @param func service implementation
* @return request StreamObserver
*/
public static <T, R> StreamObserver<T> manyToOne(
StreamObserver<R> responseObserver, Function<Flux<T>, Mono<R>> func) {
ServerTripleReactorPublisher<T> serverPublisher =
new ServerTripleReactorPublisher<>((CallStreamObserver<R>) responseObserver);
try {
Mono<R> responseMono = func.apply(Flux.from(serverPublisher));
responseMono.subscribe(
value -> {
// Don't try to respond if the server has already canceled the request
if (!serverPublisher.isCancelled()) {
responseObserver.onNext(value);
}
},
throwable -> {
// Don't try to respond if the server has already canceled the request
if (!serverPublisher.isCancelled()) {
responseObserver.onError(throwable);
}
},
responseObserver::onCompleted);
serverPublisher.startRequest();
} catch (Throwable throwable) {
responseObserver.onError(throwable);
}
return serverPublisher;
}
/**
* Implements a stream -> stream call as Flux -> Flux
*
* @param responseObserver response StreamObserver
* @param func service implementation
* @return request StreamObserver
*/
public static <T, R> StreamObserver<T> manyToMany(
StreamObserver<R> responseObserver, Function<Flux<T>, Flux<R>> func) {
// responseObserver is also a subscription of publisher, we can use it to request more data
ServerTripleReactorPublisher<T> serverPublisher =
new ServerTripleReactorPublisher<>((CallStreamObserver<R>) responseObserver);
try {
Flux<R> responseFlux = func.apply(Flux.from(serverPublisher));
ServerTripleReactorSubscriber<R> serverSubscriber =
responseFlux.subscribeWith(new ServerTripleReactorSubscriber<>());
serverSubscriber.subscribe((CallStreamObserver<R>) responseObserver);
serverPublisher.startRequest();
} catch (Throwable throwable) {
responseObserver.onError(throwable);
}
return serverPublisher;
}
private static void doOnResponseHasException(Throwable throwable, StreamObserver<?> responseObserver) {
StatusRpcException statusRpcException =
TriRpcStatus.getStatus(throwable).asException();
responseObserver.onError(statusRpcException);
}
}