首页 > 解决方案 > Netty 客户端接收消息延迟

问题描述

1.场景说明:设备发送数据到Netty Server(大约##20ms间隔##),Netty Server将msg转发给客户端##立即##(IOS或Android)。2.关联业务代码ctx.writeAndFlush(msg)

protected void doWrite(ChannelOutboundBuffer in) throws Exception {
    int writeSpinCount = -1;

    boolean setOpWrite = false;
    for (;;) {
        //Get the data of the first node that needs to be flushed
        Object msg = in.current();

        if (msg instanceof ByteBuf) {
            
            ByteBuf buf = (ByteBuf) msg;

            boolean done = false;
            long flushedAmount = 0;
            // Get the number of spin lock iterations
            if (writeSpinCount == -1) {
                writeSpinCount = config().getWriteSpinCount();
            }
            // Spin, write out the current node
            for (int i = writeSpinCount - 1; i >= 0; i --) {
                int localFlushedAmount = doWriteBytes(buf);
                if (localFlushedAmount == 0) {
                    setOpWrite = true;
                    break;
                }

                flushedAmount += localFlushedAmount;
                if (!buf.isReadable()) {
                    done = true;
                    break;
                }
            }

            in.progress(flushedAmount);

            // After writing, delete the current node
            if (done) {
                in.remove();
            } else {
                break;
            }
        } 
    }
}
protected int doWriteBytes(ByteBuf buf) throws Exception {
    final int expectedWrittenBytes = buf.readableBytes();
    return buf.readBytes(javaChannel(), expectedWrittenBytes);
}

3.issue netty Server 可以及时接收设备数据,netty Server 也可以及时将数据写入 Socket Buffer。但是Netty客户端收到消息延迟!!!(比如5s延迟)

4.服务器带宽配置 Inbound 100M/bps bit per seconds。每秒出站 5M/bps 比特。

标签: javasocketstcpnetty

解决方案


客户端长度加壳引起的问题。


推荐阅读