Search in sources :

Example 1 with RecycleResponse

use of rpc.turbo.protocol.recycle.RecycleResponse in project turbo-rpc by hank-whu.

the class NettyRpcServerHandler method channelRead0.

protected void channelRead0(ChannelHandlerContext ctx, final Request request) throws Exception {
    final int requestId = request.getRequestId();
    final Invoker<CompletableFuture<?>> invoker = invokerFactory.get(request.getServiceId());
    if (invoker == null) {
        if (logger.isErrorEnabled()) {
            logger.error("not support this serviceId: " + request.getServiceId());
        }
        ctx.channel().close();
        return;
    }
    boolean allowHandle = doRequestFilter(request, invoker);
    final RecycleResponse response = RecycleResponse.newInstance(request);
    if (!allowHandle) {
        response.setRequestId(requestId);
        response.setStatusCode(ResponseStatus.SERVER_FILTER_DENY);
        response.setTracer(null);
        response.setResult(RpcServerFilter.SERVER_FILTER_DENY);
        doResponseFilter(request, response, invoker, null);
        ctx.writeAndFlush(response, ctx.voidPromise());
        return;
    }
    CompletableFuture<?> future = invoker.invoke(request.getMethodParam());
    future.whenComplete((result, throwable) -> {
        response.setRequestId(requestId);
        response.setTracer(null);
        if (result != null) {
            response.setStatusCode(ResponseStatus.OK);
            response.setResult(result);
            doResponseFilter(request, response, invoker, null);
        } else if (throwable != null) {
            response.setStatusCode(ResponseStatus.SERVER_ERROR);
            response.setResult(Arrays.toString(throwable.getStackTrace()));
            doResponseFilter(request, response, invoker, throwable);
        } else {
            response.setStatusCode(ResponseStatus.SERVER_ERROR);
            response.setResult(UNKNOWN);
            doResponseFilter(request, response, invoker, UNKNOWN);
        }
        ctx.writeAndFlush(response, ctx.voidPromise());
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RecycleResponse(rpc.turbo.protocol.recycle.RecycleResponse)

Aggregations

CompletableFuture (java.util.concurrent.CompletableFuture)1 RecycleResponse (rpc.turbo.protocol.recycle.RecycleResponse)1