Search in sources :

Example 1 with RequestWithFuture

use of rpc.turbo.transport.client.future.RequestWithFuture in project turbo-rpc by hank-whu.

the class BatchSender method doBatchSend.

private void doBatchSend() {
    if (sendBuffer.isEmpty()) {
        return;
    }
    for (int r = 0; r < MAX_SEND_LOOP_COUNT; r++) {
        for (int i = 0; i < MAX_BATCH_SIZE; i++) {
            RequestWithFuture request = sendBuffer.poll();
            if (request != null) {
                batchList.add(request);
            } else {
                break;
            }
        }
        if (!batchList.isEmpty()) {
            channel.write(batchList, voidPromise);
            batchList.clear();
        }
        if (sendBuffer.isEmpty()) {
            break;
        }
    }
    channel.flush();
}
Also used : RequestWithFuture(rpc.turbo.transport.client.future.RequestWithFuture)

Example 2 with RequestWithFuture

use of rpc.turbo.transport.client.future.RequestWithFuture 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)

Aggregations

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