简体   繁体   English

针对大量出站HTTP请求优化Node.js?

[英]Optimizing Node.js for a large number of outbound HTTP requests?

My node.js server is experiencing times when it becomes slow or unresponsive, even occasionally resulting in 503 gateway timeouts when attempting to connect to the server. 我的node.js服务器遇到缓慢或无响应的时间,甚至偶尔会在尝试连接到服务器时导致503网关超时。

I am 99% sure (based upon tests that I have run) that this lag is coming specifically from the large number of outbound requests I am making with the node-oauth module to contact external APIs (Facebook, Twitter, and many others). 我99%肯定(基于我已经运行的测试),这种滞后特别来自我使用node-oauth模块联系外部API(Facebook,Twitter和许多其他人)的大量出站请求。 Admittedly, the number of outbound requests being made is relatively large (in the order of 30 or so per minute). 无可否认,正在进行的出站请求数量相对较大(每分钟大约30个左右)。 Even worse, this frequently means that the corresponding inbound requests to my server can take ~5-10 seconds to complete. 更糟糕的是,这经常意味着对我的服务器的相应入站请求可能需要大约5-10秒才能完成。 However, I had a previous version of my API which I had written in PHP which was able to handle this amount of outbound requests without any problem at all. 但是,我有一个以前用于PHP编写的API版本,它能够处理这一数量的出站请求而没有任何问题。 Actually, the CPU usage for the same number (or even fewer) requests with my Node.js API is about 5x that of my PHP API. 实际上,使用我的Node.js API的相同数量(甚至更少)请求的CPU使用率大约是我的PHP API的5倍。

So, I'm trying to isolate where I can improve upon this, and most importantly to make sure that 503 timeouts do not occur. 所以,我试图找出可以改进的地方,最重要的是要确保不会发生503超时。 Here's some stuff I've read about or experimented with: 以下是我读过或试验过的一些内容:

  • This article (by LinkedIn) recommends turning off socket pooling. 本文(由LinkedIn)建议关闭套接字池。 However, when I contacted the author of the popular nodejs-request module , his response was that this was a very poor idea. 但是,当我联系流行的nodejs-request模块的作者时 ,他的回答是这是一个非常糟糕的主意。
  • I have heard it said that setting "http.globalAgent.maxSockets" to a large number can help, and indeed it did seem to reduce bottlenecking for me 我听说过将“http.globalAgent.maxSockets”设置为大量可能有所帮助,事实上它似乎确实减少了我的瓶颈

I could go on, but in short, I have been able to find very little definitive information about how to optimize performance so these outbound connections do not lag my inbound requests from clients. 我可以继续,但简而言之,我已经能够找到关于如何优化性能的确切信息,因此这些出站连接不会滞后于来自客户端的入站请求。

Thanks in advance for any thoughts or contributions. 提前感谢任何想法或贡献。

FWIW, I'm using express and mongoose as well, and my servers are hosted on the Amazon Cloud (2x M1.Large for the node servers, 2x load balancers, and 3x M1.Small MongoDB instances). FWIW,我也使用express和mongoose,我的服务器托管在Amazon Cloud上(2x M1.Large用于节点服务器,2x负载平衡器和3x M1.Small MongoDB实例)。

It sounds to me that the Agent is capping your requests to the default level of 5 per-host. 在我看来,代理正在将您的请求限制为每个主机的默认级别为5。 Your tests show that cranking up the agent's maxSockets helped... you should do that. 你的测试表明,启动代理的maxSockets有助于......你应该这样做。

You can prove this is the issue by firing up a packet sniffer, or adding more debugging code to your application, to show that this is the limiting factor. 您可以通过启动数据包嗅探器或向应用程序添加更多调试代码来证明这是问题,以表明这是限制因素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM