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);
}
}
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;
}
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;
}
Aggregations