- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要向客户端推送一个大文件,但是我想限制速度(比如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 中管理 channel 写入功能?
如 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.
和初始化:
可以看一个例子(使用Discard的例子)here ,特别是:
关于netty - 如何在 Netty 4+ 中使用 ChannelTrafficShapingHandler?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34326230/
我是一名优秀的程序员,十分优秀!