我需要将一个大文件推送到客户端,但是我想限制速度(例如100Kb / s),如何使用ChannelTrafficShapingHandler?
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(
new StringEncoder(CharsetUtil.UTF_8),
new LineBasedFrameDecoder(8192),
new StringDecoder(CharsetUtil.UTF_8),
new ChannelTrafficShapingHandler(1,1,10L),
new ChunkedWriteHandler(),
new FileServerHandler()
);
}
});
该演示不起作用,为什么?
您是否在FileServerHandler中管理通道写入功能?
如Netty API中所述,用于ChannelTrafficShapingHandler
在处理程序中,应该考虑使用channel.isWritable()和channelWritabilityChanged(ctx)处理可写性,或者在ctx.write()返回的将来通过future.addListener(new GenericFutureListener())处理可写性。
您还应该考虑使读写操作中的对象大小相对适合您所需的带宽:例如,以10KB / s的速度包含10 MB的对象将导致突发效果,而以1 MB / s的速度包含100 KB的对象应平稳由此TrafficShaping处理程序处理。
和初始化:
您可以在此处看到一个示例(使用“丢弃”示例),尤其是:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句