gpt4 book ai didi

sockets - Netty并发和 "Connection reset by peer"

转载 作者:行者123 更新时间:2023-12-02 02:21:40 24 4
gpt4 key购买 nike

我已经构建了以下简单的服务器,我正在使用 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();
}

}

如您所见,它非常简单,它只显示处理的请求总数。
有小费吗?

谢谢

最佳答案

“对等连接重置”通常意味着您已写入已被另一端关闭的连接。换句话说,应用程序协议(protocol)错误。您在后续读取或写入时会收到错误本身。

关于sockets - Netty并发和 "Connection reset by peer",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7883545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com