Netty 4 を使用してかなり単純なサーバーを作成しました。数千の接続を処理するようにスケールアップすることができましたが、最大 40 スレッドを超えることはありません。
テストするために、数千の接続を作成するテスト クライアントも作成しました。残念ながら、これは接続を作成するのと同じ数のスレッドを作成します。クライアントのスレッドを最小限に抑えることを望んでいました。私はこれについて多くの投稿を見てきました。多くの例は、単一接続のセットアップを示しています。 これとこれは、クライアント間で NioEventLoopGroup を共有することを示しています。限られた数の nioEventLoopGroup を取得していますが、他の場所で接続ごとにスレッドを取得しています。パイプラインで意図的にスレッドを作成しているのではなく、何ができるかわかりません。
これは、クライアント コードのセットアップの一部です。これまでの調査に基づいて、一定のスレッド数を維持する必要があるようです。クライアント接続ごとのスレッドを防ぐために私がしなければならないことはありますか?
主要
final EventLoopGroup group = new NioEventLoopGroup();
for (int i=0; i<100; i++)){
MockClient client = new MockClient(i, group);
client.connect();
}
モッククライアント
public class MockClient implements Runnable {
private final EventLoopGroup group;
private int identity;
public MockClient(int identity, final EventLoopGroup group) {
this.identity = identity;
this.group = group;
}
@Override
public void run() {
try {
connect();
} catch (Exception e) {}
}
public void connect() throws Exception{
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new MockClientInitializer(identity, this));
final Runnable that = this;
// Start the connection attempt
b.connect(config.getHost(), config.getPort()).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (future.isSuccess()) {
Channel ch = future.sync().channel();
} else {
//if the server is down, try again in a few seconds
future.channel().eventLoop().schedule(that, 15, TimeUnit.SECONDS);
}
}
});
}
}