Search in sources :

Example 1 with Request

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

the class ConnectorContext method execute.

/**
 * 远程调用
 *
 * @param serviceId
 *            远程serviceId
 * @param timeout
 *            millseconds
 * @param methodParam
 *            方法参数对象,无参类型为null
 * @param failoverInvoker
 *            失败回退
 * @return
 */
<T> CompletableFuture<T> execute(int serviceId, long timeout, MethodParam methodParam, Invoker<CompletableFuture<?>> failoverInvoker) {
    if (isClosed) {
        throw new RemoteException("已关闭的连接!");
    }
    int requestId = sequencer.next();
    for (int i = 0; i < connectCount; i++) {
        // 最多循环一遍
        if (isZombie(channelIndex(requestId))) {
            requestId = sequencer.next();
            continue;
        }
        break;
    }
    Request request = new Request();
    request.setServiceId(serviceId);
    request.setRequestId(requestId);
    if (methodParam instanceof EmptyMethodParam) {
        request.setMethodParam(null);
    } else {
        request.setMethodParam(methodParam);
    }
    if (globalTimeout > 0) {
        timeout = globalTimeout;
    }
    CompletableFuture<Response> future = new CompletableFuture<>();
    try {
        if (requestWaitSemaphore != null) {
            requestWaitSemaphore.acquire();
        }
        boolean allowSend = doRequestFilter(request);
        if (allowSend) {
            long expireTime = SystemClock.fast().mills() + timeout;
            // 
            connector.send(// 
            channelIndex(request), new RequestWithFuture(request, future, expireTime));
        } else {
            future.completeExceptionally(new RemoteException(RpcClientFilter.CLIENT_FILTER_DENY, false));
        }
    } catch (Exception e) {
        future.completeExceptionally(e);
    }
    if (failoverInvoker == null) {
        return handleResult(request, future);
    } else {
        return handleResult(request, future, failoverInvoker, methodParam);
    }
}
Also used : Response(rpc.turbo.protocol.Response) CompletableFuture(java.util.concurrent.CompletableFuture) RequestWithFuture(rpc.turbo.transport.client.future.RequestWithFuture) EmptyMethodParam(rpc.turbo.param.EmptyMethodParam) Request(rpc.turbo.protocol.Request) RemoteException(rpc.turbo.remote.RemoteException) RemoteException(rpc.turbo.remote.RemoteException) IOException(java.io.IOException)

Example 2 with Request

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

the class KryoSerializer method readRequest.

public Request readRequest(ByteBuf byteBuf) throws IOException {
    int requestId = byteBuf.readInt();
    int serviceId = ByteBufUtils.readVarInt(byteBuf);
    Tracer tracer = tracerSerializer.read(byteBuf);
    MethodParam methodParam = null;
    if (byteBuf.readBoolean()) {
        Class<?> clazz = getClass(serviceId);
        methodParam = (MethodParam) kryoContext().readObject(byteBuf, clazz);
    }
    Request request = RecycleRequest.newInstance(requestId, serviceId, tracer, methodParam);
    return request;
}
Also used : MethodParam(rpc.turbo.param.MethodParam) Tracer(rpc.turbo.trace.Tracer) Request(rpc.turbo.protocol.Request) RecycleRequest(rpc.turbo.protocol.recycle.RecycleRequest)

Example 3 with Request

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

the class ProtostuffSerializer method readRequest.

public Request readRequest(ByteBuf byteBuf) throws IOException {
    int requestId = byteBuf.readInt();
    int serviceId = ByteBufUtils.readVarInt(byteBuf);
    Tracer tracer = tracerSerializer.read(byteBuf);
    Schema<MethodParam> schema = schema(serviceId);
    MethodParam methodParam = null;
    if (EmptyMethodParam.class.equals(schema.typeClass())) {
        methodParam = EmptyMethodParam.empty();
    } else {
        ByteBufInput input = getOrUpdate(INPUT_ATTACHMENT_INDEX, INPUT_SUPPLIER);
        input.setByteBuf(byteBuf, true);
        methodParam = schema.newMessage();
        schema.mergeFrom(input, methodParam);
    }
    Request request = RecycleRequest.newInstance(requestId, serviceId, tracer, methodParam);
    return request;
}
Also used : EmptyMethodParam(rpc.turbo.param.EmptyMethodParam) MethodParam(rpc.turbo.param.MethodParam) Tracer(rpc.turbo.trace.Tracer) RecycleRequest(rpc.turbo.protocol.recycle.RecycleRequest) Request(rpc.turbo.protocol.Request)

Aggregations

Request (rpc.turbo.protocol.Request)3 EmptyMethodParam (rpc.turbo.param.EmptyMethodParam)2 MethodParam (rpc.turbo.param.MethodParam)2 RecycleRequest (rpc.turbo.protocol.recycle.RecycleRequest)2 Tracer (rpc.turbo.trace.Tracer)2 IOException (java.io.IOException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Response (rpc.turbo.protocol.Response)1 RemoteException (rpc.turbo.remote.RemoteException)1 RequestWithFuture (rpc.turbo.transport.client.future.RequestWithFuture)1