【发布时间】:2011-10-25 00:49:41
【问题描述】:
我已经构建了以下简单的服务器,我正在使用ab 对其进行压力测试。
如果我运行 ab 发出 3000 个总请求(300 个并发),它就可以工作。如果我再次运行它,它会显示:
apr_socket_connect(): Connection reset by peer (54)
如果在此错误之后我尝试使用 curl 发出单个请求而不重新启动服务器,它可以工作。如果我再次运行ab,它会显示相同的错误。
它似乎无法处理太多的并发连接。代码下方:
public static void main(String[] args) throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool()));
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
@Override
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(new StringEncoder(), new MyServerHandler());
}
});
bootstrap.bind(new InetSocketAddress(9090));
System.out.println("Running");
}
这里是处理程序:
public class MyServerHandler extends SimpleChannelUpstreamHandler {
private static AtomicLong request = new AtomicLong();
@Override
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
throws Exception {
ChannelFuture channelFuture = e.getChannel().write("This is request #" + request.incrementAndGet() + "\n");
channelFuture.addListener(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
throws Exception {
System.out.println(e.getCause());
e.getChannel().close();
}
}
如您所见,它非常简单,它只显示处理的请求总数。 有什么建议吗?
谢谢
【问题讨论】:
标签: sockets concurrency nio netty