简体   繁体   English

由于多个请求,对 Node.js 服务器的请求超时

[英]Requests to Node.js server timing out due to multiple requests

So I'm not super experienced with node so bear with me.所以我对节点不是很有经验,所以请耐心等待。

I have 2 routes on a node.js server.我在 node.js 服务器上有 2 条路由。 During a typical day, both routes will be hit with requests at the same time.在典型的一天中,两条路线将同时收到请求。 Route 1 will run smoothly but route 2 is a long running processes that returns several promises, so route 1 will take up the resource causing route 2 to pause (I have determined this is happening via data logs).路线 1 将顺利运行,但路线 2 是一个长时间运行的进程,会返回多个承诺,因此路线 1 将占用导致路线 2 暂停的资源(我已确定这是通过数据日志发生的)。

Route 1 looks like this:路线 1 如下所示:

  app.post('/route1', function (req, res) {
    doStuff().then(function(data){
      res.end();
    })
  }

Route 2 is handling an array of data that needs to be parsed through so 1 record in the array is processed at a time路由 2 正在处理需要解析的数据数组,因此一次处理数组中的 1 条记录

app.post('/route2', function (req, res){
 async function processArray(array) {
      for (const item of array) { 
         await file.test1()(item, res);
           await file.test2()(item, res);
             //await test3,test4,test5,test6 
      }
  }
  processArray(data).then(function() {
    res.end();
  }
}

So I'm guessing the problem is that the async/await is waiting for resources to become available before it continues to process records.所以我猜测问题是 async/await 在继续处理记录之前正在等待资源变得可用。
Is there a way for me to write this to where route1 will not interfere with route2?有没有办法让我把这个写到 route1 不会干扰 route2 的地方?

In Node, almost everything you can await for (or call then on) is asynchronous.在节点中,几乎所有你能await的(或致电then上)是异步的。 It does not block execution thread but rather offloads the task to another layer you don't control, and then just awaits for it to be finished while being free to work on something else.它不会阻塞执行线程,而是将任务卸载到您无法控制的另一层,然后等待它完成,同时可以自由地处理其他事情。 That includes working with filesystem and network requests.这包括处理文件系统和网络请求。 There are ways to block the thread still, for example, using synchronous versions of filesystem methods (like readFileSync instead of readFile ) or doing heavy computations on javascript (like calculating factorial of 4569485960485096)仍然有一些方法可以阻止线程,例如,使用文件系统方法的同步版本(如readFileSync而不是readFile )或在 javascript 上进行大量计算(如计算 4569485960485096 的阶乘)

Given your route1 doesn't do any of this, it does not take any resources from route2.鉴于您的路由 1 不执行任何这些操作,因此它不会从路由 2 中获取任何资源。 They are running in parallel.它们并行运行。 It's hard to tell without seeing the actual code, but I'm pretty sure you are getting connection timeout because your route2 is poorly written and it takes a long time to resolve (or doesn't resolve at all) for reasons not related to Node performance or blocking.如果没有看到实际代码,很难判断,但我很确定您的连接超时是因为您的 route2 写得不好,并且由于与 Node 无关的原因需要很长时间才能解决(或根本无法解决)性能或阻塞。 Node is just chilling while waiting for your filesystem to run those endless tests 6 times in every array item (or whatever is going on there) and while this happens, browser stops waiting for response and shows you connection timeout. Node 只是在等待你的文件系统在每个数组项(或那里发生的任何事情)中运行这些无休止的测试 6 次时令人不寒而栗,当发生这种情况时,浏览器停止等待响应并显示连接超时。 It's most likely that you don't need to await for every test on every array in the data instead of just running them all in parallel很可能您不需要等待数据中每个数组的每个测试,而不仅仅是并行运行它们

Read more here https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/ and here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all在这里阅读更多https://nodejs.org/en/docs/guides/blocking-vs-non-blocking/和这里https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects /承诺/所有

NodeJs is single threaded. NodeJs 是单线程的。 This is why you break the cpu/resource instensive services into micro-services.这就是您将 CPU/资源密集型服务分解为微服务的原因。

If these route1 and route2 need to be in the same server then see if you can change the algorithm or way you handle the computation to optimize the performance or break them so that they are handled by different cores in multi-core architecture.如果这些 route1 和 route2 需要在同一台服务器中,那么看看您是否可以更改算法或处理计算的方式以优化性能或打破它们,以便它们在多核架构中由不同的内核处理。

Again if you are talking about production situation with huge user base then its not definately good idea to put them together and run it in the same server.同样,如果您正在谈论具有庞大用户群的生产情况,那么将它们放在一起并在同一服务器上运行绝对不是一个好主意。

See this for more information 请参阅此了解更多信息

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

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