Search in sources :

Example 1 with ClientHandler

use of com.alipay.sofa.rpc.transport.ClientHandler in project sofa-rpc by sofastack.

the class AbstractHttp2ClientTransport method doSend.

protected void doSend(final SofaRequest request, AbstractHttpClientHandler callback, final int timeoutMills) {
    AbstractByteBuf data = null;
    try {
        // 序列化
        byte serializeType = request.getSerializeType();
        Serializer serializer = SerializerFactory.getSerializer(serializeType);
        data = serializer.encode(request, null);
        request.setData(data);
        // 记录请求序列化大小 不是很准,没有记录HTTP头
        RpcInternalContext.getContext().setAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE, data.readableBytes());
        // 转换请求
        FullHttpRequest httpRequest = convertToHttpRequest(request);
        // 发送请求
        final int requestId = sendHttpRequest(httpRequest, callback);
        if (request.isAsync()) {
            TIMEOUT_TIMER.newTimeout(new TimerTask() {

                @Override
                public void run(Timeout timeout) throws Exception {
                    Map.Entry<ChannelFuture, AbstractHttpClientHandler> entry = responseChannelHandler.removePromise(requestId);
                    if (entry != null) {
                        ClientHandler handler = entry.getValue();
                        Exception e = timeoutException(request, timeoutMills, null);
                        handler.onException(e);
                    }
                }
            }, timeoutMills, TimeUnit.MILLISECONDS);
        }
    } finally {
        if (data != null) {
            data.release();
        }
    }
}
Also used : AbstractByteBuf(com.alipay.sofa.rpc.transport.AbstractByteBuf) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TimerTask(io.netty.util.TimerTask) Timeout(io.netty.util.Timeout) ClientHandler(com.alipay.sofa.rpc.transport.ClientHandler) TimeoutException(java.util.concurrent.TimeoutException) SofaTimeOutException(com.alipay.sofa.rpc.core.exception.SofaTimeOutException) ExecutionException(java.util.concurrent.ExecutionException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Serializer(com.alipay.sofa.rpc.codec.Serializer)

Aggregations

Serializer (com.alipay.sofa.rpc.codec.Serializer)1 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)1 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)1 AbstractByteBuf (com.alipay.sofa.rpc.transport.AbstractByteBuf)1 ClientHandler (com.alipay.sofa.rpc.transport.ClientHandler)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 Timeout (io.netty.util.Timeout)1 TimerTask (io.netty.util.TimerTask)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1