/**
* Create a new instance.
*
* @param executor
* the {@link ScheduledExecutorService} to use for the {@link TrafficCounter}.
* @param writeGlobalLimit
* 0 or a limit in bytes/s
* @param readGlobalLimit
* 0 or a limit in bytes/s
* @param writeChannelLimit
* 0 or a limit in bytes/s
* @param readChannelLimit
* 0 or a limit in bytes/s
* @param checkInterval
* The delay between two computations of performances for
* channels or 0 if no stats are to be computed.
* @param maxTime
* The maximum delay to wait in case of traffic excess.
*/
public GlobalChannelTrafficShapingHandler(ScheduledExecutorService executor,
long writeGlobalLimit, long readGlobalLimit,
long writeChannelLimit, long readChannelLimit,
long checkInterval, long maxTime) {
super(writeGlobalLimit, readGlobalLimit, checkInterval, maxTime);
createGlobalTrafficCounter(executor);
this.writeChannelLimit = writeChannelLimit;
this.readChannelLimit = readChannelLimit;
}
使用流量整形的代码示例:
// Netty 提供的用于流量整形的 Handler
final GlobalTrafficShapingHandler globalTrafficShapingHandler
// 这里需要的 Executor 比较轻量级,只是服务于 TrafficCounter,可以直接复用上面的 executors,也可以直接 new 出来;
// 读写控制都定成 100M
= new GlobalTrafficShapingHandler(new NioEventLoopGroup(), 100 * 1024 * 1024, 100 * 1024 * 1024);
// 流量整形的 Handler 要放到靠前一点的位置
pipeline.addLast("TShandler", globalTrafficShapingHandler);