On 20. Jul 2017, at 08:50, Ronggen Liu <rongg...@gmail.com> wrote:Hello,problem:some connections were lost when there are too many clients were trying to connect to the server during a short time, for example, send out about 10k connections during about 3 seconds.findings:as i know, all the connections would be accepted in method doReadMessage of NioServerSocketChannel, and, in fact, we did find out that some connections were not accepted from there, but, actually, the TCP have been created successfully with completed 3-ways handsharke.meanwhile, we found, to some degree, we can avoid this issue by increasing the option SO_BACKLOG of ChannelOption, currently, we set it with 8, should be a little bit low.
questions:how to handle this issue? as i know, the client should receive connection refused error when the number of the connections exceed the SO_BACKLOG, but the client didn't receive it.
Thanks,Gary--
You received this message because you are subscribed to the Google Groups "Netty discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netty+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/3f5aae56-1fd6-4e0b-8a6e-cfed66653c1e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/netty/bbccbf19-aa67-4346-83dd-566257893296%40googlegroups.com.
public class EchoServer {
private final int port;
EventLoopGroup serverEventLoop = new NioEventLoopGroup(1);
EventLoopGroup btsEventLoop = new NioEventLoopGroup();
public EchoServer(int port) {
this.port = port;
}
public static void main(String[] args)
throws Exception {
int port = Integer.parseInt(args[0]);
int backlog = Integer.parseInt(args[1]);
new EchoServer(port).start(backlog);
}
public void start(int backlog) throws Exception {
final EchoServerHandler serverHandler = new EchoServerHandler();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(serverEventLoop, btsEventLoop)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(serverHandler);
}
})
.option(ChannelOption.SO_BACKLOG, backlog)
// .option(ChannelOption.AUTO_READ, false)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind().sync();
System.out.println("backlog: " + b.config().options().get(ChannelOption.SO_BACKLOG));
// System.out.println("autoRead: " + b.config().options().get(ChannelOption.AUTO_READ));
System.out.println("Server started and listening for connections on " + f.channel().localAddress());
f.channel().closeFuture().sync();
} finally {
serverEventLoop.shutdownGracefully().sync();
btsEventLoop.shutdownGracefully().sync();
System.out.println("finished...");
}
}
}
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
private static volatile int channelNum;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
System.out.println(
"Server received: " + in.toString(CharsetUtil.UTF_8));
ctx.write(Unpooled.copiedBuffer("msg from server: " + ctx.channel().remoteAddress(), CharsetUtil.UTF_8));
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
channelNum++;
System.out.println("Active Channel..." + channelNum);
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
// System.out.println("Register...");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx)
throws Exception {
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER)
.addListener(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx,
Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
Description of SO_BACKLOG option:
The maximum queue length for incoming connection indications (a request to connect) is set to the backlog parameter. If a connection indication arrives when the queue is full, the connection is refused.