【问题标题】:Limiting the size of inbound buffer in netty 4 cr6限制netty 4 cr6中入站缓冲区的大小
【发布时间】:2013-06-26 23:09:44
【问题描述】:

当客户端发送大文件时,如何限制 netty 创建的入站缓冲区的大小?我希望客户端在 ByteBuf 已满时停止通过网络发送数据,并在准备好接收更多数据后恢复。

这是我的处理程序代码:

public class MyDecoder extends ChannelInboundHandlerAdapter {

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    // limit the size of recieving buffer to 1024
    ctx.channel().config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(1024));
    }

@Override public void messageReceived(ChannelHandlerContext ctx, MessageList<Object> msgs) throws Exception {
    ByteBuf byteBuf = msgs.<ByteBuf>cast().get(0);
    // stop after this line in debugger to check size of the actual payload per messageReceived invocation
    int bufferSize = byteBuf.readableBytes();
    }
}

现在假设在我的测试中我写:

EmbeddedChannel ch = new EmbeddedChannel(new MyDecoder());
ch.writeInbound(Unpooled.wrappedBuffer(<<<here is a byte representation of a large file, say 100 mb>>>));
ch.readInbound(); // etc... 

现在,当我在调试器中到达计算 bufferSize 的行时,我总是得到我的大文件的完整大小。 FixedRecvByteBufAllocator 在这一点上的唯一区别是它的大小设置为 0 - 所有其他值产生相同的结果。

在 netty 4 cr2 中有一个方法可以达到这个目的,newInboundBuffer,但在 cr6 及更高版本中没有这样的构造。

【问题讨论】:

    标签: netty


    【解决方案1】:

    FixedRecvByteBufAllocator 应该将缓冲区限制为最大 1024 字节,如果不是这种情况,它是一个错误。

    【讨论】:

      猜你喜欢
      • 2021-11-11
      • 2022-11-11
      • 1970-01-01
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      相关资源
      最近更新 更多