如何在 Netty 4+ 中使用 ChannelTrafficShapingHandler?
How to use ChannelTrafficShapingHandler in Netty 4+?
我需要向客户端推送一个大文件,但是我想限制速度(比如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
In your handler, you should consider to use the channel.isWritable()
and channelWritabilityChanged(ctx) to handle writability, or through
future.addListener(new GenericFutureListener()) on the future returned
by ctx.write().
You shall also consider to have object size in read or write
operations relatively adapted to the bandwidth you required: for
instance having 10 MB objects for 10KB/s will lead to burst effect,
while having 100 KB objects for 1 MB/s should be smoothly handle by
this TrafficShaping handler.
以及初始化:
- 第一项是Write limit in B/s(这里强烈不推荐1,接近1024的最小值,1KB/s)
- 第二项是Read limit in B/S(这里强烈不推荐1,接近1024的是最小的,对于1KB/s,或者0表示没有限制)
- 第一项是以毫秒为单位的间隔检查(这里的1L表示每毫秒,强烈不推荐,接近1000的东西是最小的,每1秒)
你可以看到一个例子(使用丢弃的例子)here,特别是:
我需要向客户端推送一个大文件,但是我想限制速度(比如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
In your handler, you should consider to use the channel.isWritable() and channelWritabilityChanged(ctx) to handle writability, or through future.addListener(new GenericFutureListener()) on the future returned by ctx.write().
You shall also consider to have object size in read or write operations relatively adapted to the bandwidth you required: for instance having 10 MB objects for 10KB/s will lead to burst effect, while having 100 KB objects for 1 MB/s should be smoothly handle by this TrafficShaping handler.
以及初始化:
- 第一项是Write limit in B/s(这里强烈不推荐1,接近1024的最小值,1KB/s)
- 第二项是Read limit in B/S(这里强烈不推荐1,接近1024的是最小的,对于1KB/s,或者0表示没有限制)
- 第一项是以毫秒为单位的间隔检查(这里的1L表示每毫秒,强烈不推荐,接近1000的东西是最小的,每1秒)
你可以看到一个例子(使用丢弃的例子)here,特别是: