我正在编写一个应用程序,我需要跟踪发送/接收的字节和带宽。我正在使用一个本身使用Netty 5.0的库。
我对Netty代码的唯一访问是扩展我正在使用的库的class。
如何在此课程中创建和设置TrafficCounter?我无法在网上找到任何如何做到这一点的例子(至少在Netty 5.0中没有)。我有多个使用Netty的线程,所以我需要每个通道的流量。
答案 0 :(得分:0)
您可以查看此PR,其中仍然包含有关如何使用TrafficCounter的完整示例(实际上是TrafficShapingHandler):
特别是对于包含全局和频道流量整形的DiscardServer,其中最后一个(ChannelTrafficShaping)是您正在寻找的那个。
所以您可以拥有以下内容:
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
public void initChannel(Channel channel) throws Exception {
getPacketProtocol().newClientSession(client, TcpClientSession.this);
channel.config().setOption(ChannelOption.IP_TOS, 0x18);
channel.config().setOption(ChannelOption.TCP_NODELAY, false);
ChannelPipeline pipeline = channel.pipeline();
refreshReadTimeoutHandler(channel);
refreshWriteTimeoutHandler(channel);
pipeline.addLast("encryption", new TcpPacketEncryptor(TcpClientSession.this));
pipeline.addLast("traffic", new ChannelTrafficShapingHandler(0, MAXCHANNELTHROUGHPUT, 1000)); // ADDED
pipeline.addLast("sizer", new TcpPacketSizer(TcpClientSession.this));
pipeline.addLast("codec", new TcpPacketCodec(TcpClientSession.this));
pipeline.addLast("manager", TcpClientSession.this);
}
}).group(this.group).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeout() * 1000);
当然要看API:Traffic,特别是ChannelTrafficShapingHandler