use of com.alibaba.rsocket.MutableContext in project alibaba-rsocket-broker by alibaba.
the class RSocketRequesterRpcProxy method invoke.
@Override
@RuntimeType
public Object invoke(@This Object proxy, @Origin Method method, @AllArguments Object[] allArguments) throws Throwable {
// interface default method validation for JDK Proxy only, not necessary for ByteBuddy
if (jdkProxy && method.isDefault()) {
return DefaultMethodHandler.getMethodHandle(method, serviceInterface).bindTo(proxy).invokeWithArguments(allArguments);
} else if (method.getDeclaringClass().equals(Object.class)) {
// delegate hashCode, equals, or toString methods to this
return method.invoke(this);
}
MutableContext mutableContext = new MutableContext();
if (!methodMetadataMap.containsKey(method)) {
methodMetadataMap.put(method, new ReactiveMethodMetadata(group, service, version, method, encodingType, this.acceptEncodingTypes, endpoint, sticky, sourceUri));
}
ReactiveMethodMetadata methodMetadata = methodMetadataMap.get(method);
mutableContext.put(ReactiveMethodMetadata.class, methodMetadata);
Object[] args = allArguments;
if (methodMetadata.isKotlinSuspend()) {
args = Arrays.copyOfRange(args, 0, args.length - 1);
mutableContext.put(Continuation.class, allArguments[allArguments.length - 1]);
}
// ----- return type deal------
if (methodMetadata.getRsocketFrameType() == FrameType.REQUEST_CHANNEL) {
metrics(methodMetadata);
Payload routePayload;
Flux<Object> source;
// 1 param or 2 params
if (args.length == 1) {
routePayload = ByteBufPayload.create(Unpooled.EMPTY_BUFFER, methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate());
source = methodMetadata.getReactiveAdapter().toFlux(args[0]);
} else {
ByteBuf bodyBuffer = encodingFacade.encodingResult(args[0], methodMetadata.getParamEncoding());
routePayload = ByteBufPayload.create(bodyBuffer, methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate());
source = methodMetadata.getReactiveAdapter().toFlux(args[1]);
}
Flux<Payload> payloadFlux = source.startWith(routePayload).map(obj -> {
if (obj instanceof Payload)
return (Payload) obj;
return ByteBufPayload.create(encodingFacade.encodingResult(obj, encodingType), methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate());
});
Flux<Payload> payloads = upstreamManager.getRSocket(this.serviceId).requestChannel(payloadFlux);
Flux<Object> fluxReturn = payloads.concatMap(payload -> {
try {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(payload.metadata());
return Mono.justOrEmpty(encodingFacade.decodeResult(extractPayloadDataMimeType(compositeMetadata, encodingType), payload.data(), methodMetadata.getInferredClassForReturn()));
} catch (Exception e) {
return Flux.error(e);
}
}).subscriberContext(mutableContext::putAll);
if (methodMetadata.isMonoChannel()) {
return fluxReturn.last();
} else {
return methodMetadata.getReactiveAdapter().fromPublisher(fluxReturn, method.getReturnType());
}
} else {
// body content
ByteBuf bodyBuffer = encodingFacade.encodingParams(args, methodMetadata.getParamEncoding());
Class<?> returnType = method.getReturnType();
if (methodMetadata.getRsocketFrameType() == FrameType.REQUEST_RESPONSE) {
metrics(methodMetadata);
Mono<Payload> payloadMono = remoteRequestResponse(methodMetadata, methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate(), bodyBuffer);
Mono<Object> result = payloadMono.handle((payload, sink) -> {
try {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(payload.metadata());
Object obj = encodingFacade.decodeResult(extractPayloadDataMimeType(compositeMetadata, encodingType), payload.data(), methodMetadata.getInferredClassForReturn());
if (obj != null) {
sink.next(obj);
}
sink.complete();
} catch (Exception e) {
sink.error(e);
}
});
return methodMetadata.getReactiveAdapter().fromPublisher(result, returnType, mutableContext);
} else if (methodMetadata.getRsocketFrameType() == FrameType.REQUEST_FNF) {
metrics(methodMetadata);
return remoteFireAndForget(methodMetadata, methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate(), bodyBuffer);
} else if (methodMetadata.getRsocketFrameType() == FrameType.REQUEST_STREAM) {
metrics(methodMetadata);
Flux<Payload> flux = remoteRequestStream(methodMetadata, methodMetadata.getCompositeMetadataByteBuf().retainedDuplicate(), bodyBuffer);
Flux<Object> result = flux.concatMap((payload) -> {
try {
RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.from(payload.metadata());
return Mono.justOrEmpty(encodingFacade.decodeResult(extractPayloadDataMimeType(compositeMetadata, encodingType), payload.data(), methodMetadata.getInferredClassForReturn()));
} catch (Exception e) {
return Mono.error(e);
}
});
return methodMetadata.getReactiveAdapter().fromPublisher(result, returnType, mutableContext);
} else {
ReferenceCountUtil.safeRelease(bodyBuffer);
return Mono.error(new Exception(RsocketErrorCode.message("RST-200405", methodMetadata.getRsocketFrameType())));
}
}
}
Aggregations