有没有办法限制(或只是获取)netty 库(版本 4.x)中的传出连接数?
NioEventLoopGroup
中有一个nThreads
选项,它等于numberOfCpuCores * 2
,但每个线程对多个连接使用一个Selector
。
附注这就是提出请求的方式。
Bootstrap b = new Bootstrap();
b.group(eventLoopGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
.handler(new MyHandler());
ChannelFuture cf = b.connect(host, port);
cf.addListener(new MyConnectionListener());
最佳答案
我认为您需要实现自己的 ChannelOutboundHandler。使用Semaphore
来控制连接数。当 connect
获取时,当 disconnect
、close
和 exceptionCaught
时释放。注意避免反复释放导致Semaphore
为负值。
关于java - 如何限制Netty客户端的传出连接数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34227394/