Search in sources :

Example 6 with GSVRoutingMetadata

use of org.kin.rsocket.core.metadata.GSVRoutingMetadata in project kin-rsocket-broker by huangjianqin.

the class BrokerRequestHandler method requestResponse.

@Nonnull
@Override
public Mono<Payload> requestResponse(Payload payload) {
    BinaryRoutingMetadata binaryRoutingMetadata = BinaryRoutingMetadata.extract(payload.metadata());
    GSVRoutingMetadata gsvRoutingMetadata;
    if (binaryRoutingMetadata != null) {
        gsvRoutingMetadata = binaryRoutingMetadata.toGSVRoutingMetadata();
    } else {
        RSocketCompositeMetadata compositeMetadata = RSocketCompositeMetadata.of(payload.metadata());
        gsvRoutingMetadata = compositeMetadata.getMetadata(RSocketMimeType.ROUTING);
        if (gsvRoutingMetadata == null) {
            return Mono.error(new InvalidException("No Routing metadata"));
        }
    }
    // request filters
    Mono<RSocket> destination;
    if (this.filterChain.isFiltersPresent()) {
        RSocketFilterContext filterContext = RSocketFilterContext.of(FrameType.REQUEST_RESPONSE, gsvRoutingMetadata, this.upstreamBrokerMetadata, payload);
        // filter可能会改变gsv metadata的数据, 影响路由结果
        destination = filterChain.filter(filterContext).then(findDestination(gsvRoutingMetadata));
    } else {
        destination = findDestination(gsvRoutingMetadata);
    }
    // call destination
    return destination.flatMap(rsocket -> {
        MetricsUtils.metrics(gsvRoutingMetadata, FrameType.REQUEST_RESPONSE.name());
        return rsocket.requestResponse(payload);
    });
}
Also used : RSocketCompositeMetadata(org.kin.rsocket.core.metadata.RSocketCompositeMetadata) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) BinaryRoutingMetadata(org.kin.rsocket.core.metadata.BinaryRoutingMetadata) InvalidException(io.rsocket.exceptions.InvalidException) RSocket(io.rsocket.RSocket) AbstractRSocket(org.kin.rsocket.core.AbstractRSocket) Nonnull(javax.annotation.Nonnull)

Example 7 with GSVRoutingMetadata

use of org.kin.rsocket.core.metadata.GSVRoutingMetadata in project kin-rsocket-broker by huangjianqin.

the class RSocketResponderHandlerSupport method localRequestChannel.

/**
 * 本地调用服务接口方法并针对RequestChannel Frame Type场景定制额外逻辑
 */
@SuppressWarnings("ReactiveStreamsNullableInLambdaInTransform")
protected Flux<Payload> localRequestChannel(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, MessageAcceptMimeTypesMetadata acceptMimeTypesMetadata, Payload signal, Flux<Payload> payloads) {
    String service = routing.getService();
    String handler = routing.getHandler();
    try {
        ReactiveMethodInvoker methodInvoker = LocalRSocketServiceRegistry.INSTANCE.getInvoker(routing.handlerId());
        if (methodInvoker != null) {
            Object result;
            if (methodInvoker.getParamCount() == 1) {
                Flux<Object> paramFlux = payloads.map(payload -> Codecs.INSTANCE.decodeResult(dataEncodingMetadata.getMessageMimeType(), payload.data(), methodInvoker.getInferredClassForParameter(0)));
                result = methodInvoker.invoke(paramFlux);
            } else {
                Object paramFirst = Codecs.INSTANCE.decodeResult(dataEncodingMetadata.getMessageMimeType(), signal.data(), methodInvoker.getParameterTypes()[0]);
                Flux<Object> paramFlux = payloads.map(payload -> Codecs.INSTANCE.decodeResult(dataEncodingMetadata.getMessageMimeType(), payload.data(), methodInvoker.getInferredClassForParameter(1)));
                result = methodInvoker.invoke(paramFirst, paramFlux);
            }
            // composite data for return value
            RSocketMimeType resultEncodingType = resultEncodingType(acceptMimeTypesMetadata, dataEncodingMetadata.getMessageMimeType(), methodInvoker);
            // result return
            return ReactiveObjAdapter.INSTANCE.toFlux(result).map(object -> Codecs.INSTANCE.encodeResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, Codecs.INSTANCE.getDefaultCompositeMetadataByteBuf(resultEncodingType)));
        } else {
            return Flux.error(new InvalidException(noServiceMethodInvokerFoundTips(service, handler)));
        }
    } catch (Exception e) {
        error(failCallLog(service, handler), e);
        // release
        ReferenceCountUtil.safeRelease(signal);
        payloads.subscribe(ReferenceCountUtil::safeRelease);
        return Flux.error(new InvalidException(failCallTips(service, handler, e)));
    }
}
Also used : MessageAcceptMimeTypesMetadata(org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata) Flux(reactor.core.publisher.Flux) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) Payload(io.rsocket.Payload) MessageMimeTypeMetadata(org.kin.rsocket.core.metadata.MessageMimeTypeMetadata) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LoggerOprs(org.kin.framework.log.LoggerOprs) Codecs(org.kin.rsocket.core.codec.Codecs) Mono(reactor.core.publisher.Mono) ExceptionUtils(org.kin.framework.utils.ExceptionUtils) InvalidException(io.rsocket.exceptions.InvalidException) ByteBufPayload(io.rsocket.util.ByteBufPayload) InvalidException(io.rsocket.exceptions.InvalidException) InvalidException(io.rsocket.exceptions.InvalidException)

Example 8 with GSVRoutingMetadata

use of org.kin.rsocket.core.metadata.GSVRoutingMetadata in project kin-rsocket-broker by huangjianqin.

the class CanaryFilter method filter.

@Override
public Mono<Void> filter(RSocketFilterContext exchange) {
    if (roundRobinIndex % 100 < trafficRating) {
        GSVRoutingMetadata routingMetadata = exchange.getRoutingMetadata();
        routingMetadata.setVersion(canaryVersion);
    }
    roundRobinIndex = (roundRobinIndex + 1) % 100;
    return Mono.empty();
}
Also used : GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata)

Example 9 with GSVRoutingMetadata

use of org.kin.rsocket.core.metadata.GSVRoutingMetadata in project kin-rsocket-broker by huangjianqin.

the class RSocketResponderHandlerSupport method localRequestResponse.

/**
 * 本地调用服务接口方法并针对RequestResponse Frame Type场景定制额外逻辑
 */
protected Mono<Payload> localRequestResponse(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, MessageAcceptMimeTypesMetadata acceptMimeTypesMetadata, Payload payload) {
    String service = routing.getService();
    String handler = routing.getHandler();
    try {
        ReactiveMethodInvoker methodInvoker = LocalRSocketServiceRegistry.INSTANCE.getInvoker(routing.handlerId());
        if (methodInvoker != null) {
            Object result;
            if (methodInvoker.isAsyncReturn()) {
                result = invokeServiceMethod(methodInvoker, dataEncodingMetadata, payload);
            } else {
                result = Mono.create((sink) -> {
                    try {
                        Object resultObj = invokeServiceMethod(methodInvoker, dataEncodingMetadata, payload);
                        if (resultObj == null) {
                            sink.success();
                        } else if (resultObj instanceof Mono) {
                            Mono<Object> monoObj = (Mono<Object>) resultObj;
                            monoObj.doOnError(sink::error).doOnNext(sink::success).thenEmpty(Mono.fromRunnable(sink::success)).subscribe();
                        } else {
                            sink.success(resultObj);
                        }
                    } catch (Exception e) {
                        error(failCallLog(service, handler), e);
                        sink.error(e);
                    }
                });
            }
            // composite data for return value
            RSocketMimeType resultEncodingType = resultEncodingType(acceptMimeTypesMetadata, dataEncodingMetadata.getMessageMimeType(), methodInvoker);
            return ReactiveObjAdapter.INSTANCE.toMono(result).map(object -> Codecs.INSTANCE.encodeResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, Codecs.INSTANCE.getDefaultCompositeMetadataByteBuf(resultEncodingType)));
        } else {
            ReferenceCountUtil.safeRelease(payload);
            return Mono.error(new InvalidException(noServiceMethodInvokerFoundTips(service, handler)));
        }
    } catch (Exception e) {
        error(failCallLog(service, handler), e);
        ReferenceCountUtil.safeRelease(payload);
        return Mono.error(new InvalidException(failCallTips(service, handler, e)));
    }
}
Also used : MessageAcceptMimeTypesMetadata(org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata) Flux(reactor.core.publisher.Flux) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) Payload(io.rsocket.Payload) MessageMimeTypeMetadata(org.kin.rsocket.core.metadata.MessageMimeTypeMetadata) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LoggerOprs(org.kin.framework.log.LoggerOprs) Codecs(org.kin.rsocket.core.codec.Codecs) Mono(reactor.core.publisher.Mono) ExceptionUtils(org.kin.framework.utils.ExceptionUtils) InvalidException(io.rsocket.exceptions.InvalidException) ByteBufPayload(io.rsocket.util.ByteBufPayload) Mono(reactor.core.publisher.Mono) InvalidException(io.rsocket.exceptions.InvalidException) InvalidException(io.rsocket.exceptions.InvalidException)

Example 10 with GSVRoutingMetadata

use of org.kin.rsocket.core.metadata.GSVRoutingMetadata in project kin-rsocket-broker by huangjianqin.

the class RSocketResponderHandlerSupport method localRequestStream.

/**
 * 本地调用服务接口方法并针对RequestStream Frame Type场景定制额外逻辑
 */
protected Flux<Payload> localRequestStream(GSVRoutingMetadata routing, MessageMimeTypeMetadata dataEncodingMetadata, MessageAcceptMimeTypesMetadata acceptMimeTypesMetadata, Payload payload) {
    String service = routing.getService();
    String handler = routing.getHandler();
    try {
        ReactiveMethodInvoker methodInvoker = LocalRSocketServiceRegistry.INSTANCE.getInvoker(routing.handlerId());
        if (methodInvoker != null) {
            Object result = invokeServiceMethod(methodInvoker, dataEncodingMetadata, payload);
            // composite data for return value
            RSocketMimeType resultEncodingType = resultEncodingType(acceptMimeTypesMetadata, dataEncodingMetadata.getMessageMimeType(), methodInvoker);
            return ReactiveObjAdapter.INSTANCE.toFlux(result).map(object -> Codecs.INSTANCE.encodeResult(object, resultEncodingType)).map(dataByteBuf -> ByteBufPayload.create(dataByteBuf, Codecs.INSTANCE.getDefaultCompositeMetadataByteBuf(resultEncodingType)));
        } else {
            ReferenceCountUtil.safeRelease(payload);
            return Flux.error(new InvalidException(noServiceMethodInvokerFoundTips(service, handler)));
        }
    } catch (Exception e) {
        error(failCallLog(service, handler), e);
        ReferenceCountUtil.safeRelease(payload);
        return Flux.error(new InvalidException(failCallTips(service, handler, e)));
    }
}
Also used : MessageAcceptMimeTypesMetadata(org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata) Flux(reactor.core.publisher.Flux) GSVRoutingMetadata(org.kin.rsocket.core.metadata.GSVRoutingMetadata) Payload(io.rsocket.Payload) MessageMimeTypeMetadata(org.kin.rsocket.core.metadata.MessageMimeTypeMetadata) ReferenceCountUtil(io.netty.util.ReferenceCountUtil) LoggerOprs(org.kin.framework.log.LoggerOprs) Codecs(org.kin.rsocket.core.codec.Codecs) Mono(reactor.core.publisher.Mono) ExceptionUtils(org.kin.framework.utils.ExceptionUtils) InvalidException(io.rsocket.exceptions.InvalidException) ByteBufPayload(io.rsocket.util.ByteBufPayload) InvalidException(io.rsocket.exceptions.InvalidException) InvalidException(io.rsocket.exceptions.InvalidException)

Aggregations

GSVRoutingMetadata (org.kin.rsocket.core.metadata.GSVRoutingMetadata)11 InvalidException (io.rsocket.exceptions.InvalidException)7 RSocketCompositeMetadata (org.kin.rsocket.core.metadata.RSocketCompositeMetadata)7 Payload (io.rsocket.Payload)4 RSocket (io.rsocket.RSocket)4 ByteBufPayload (io.rsocket.util.ByteBufPayload)4 AbstractRSocket (org.kin.rsocket.core.AbstractRSocket)4 BinaryRoutingMetadata (org.kin.rsocket.core.metadata.BinaryRoutingMetadata)4 ByteBuf (io.netty.buffer.ByteBuf)3 ReferenceCountUtil (io.netty.util.ReferenceCountUtil)3 Nonnull (javax.annotation.Nonnull)3 LoggerOprs (org.kin.framework.log.LoggerOprs)3 ExceptionUtils (org.kin.framework.utils.ExceptionUtils)3 Codecs (org.kin.rsocket.core.codec.Codecs)3 MessageAcceptMimeTypesMetadata (org.kin.rsocket.core.metadata.MessageAcceptMimeTypesMetadata)3 MessageMimeTypeMetadata (org.kin.rsocket.core.metadata.MessageMimeTypeMetadata)3 Flux (reactor.core.publisher.Flux)3 Mono (reactor.core.publisher.Mono)3 RSocketEndpoint (org.kin.rsocket.broker.RSocketEndpoint)2 RSocketAppPrincipal (org.kin.rsocket.auth.RSocketAppPrincipal)1