简体   繁体   English

Nodejs,需要时间的多个请求

[英]Nodejs, multiple requests that takes time

i have a question that is keeping me busy and i wondered if anyone might have the answer.我有一个问题让我很忙,我想知道是否有人可以回答。

Lets assume that i have an express app that listens for a post request, This post request triggers a function in the program that calls over 1000 times with axios to a service (for that matter an sms provider that with the api call sends an sms message). Lets assume that i have an express app that listens for a post request, This post request triggers a function in the program that calls over 1000 times with axios to a service (for that matter an sms provider that with the api call sends an sms message )。 Now, assuming that one request takes 200 ms for the SMS provider, 1000 of them will take over 2 minutes.现在,假设 SMS 提供商的一个请求需要 200 毫秒,其中 1000 个请求将需要 2 分钟以上。 Of course i will send a response from the server earlier saying that the request have been recieved.当然,我会在早些时候从服务器发送一个响应,说请求已经收到。 My question is, lets say that there are 20 or even 100 requests of it at the same time, how can i get the app to handle that traffic?我的问题是,假设同时有 20 个甚至 100 个请求,我怎样才能让应用程序处理这些流量? Even if it would it will take very long to perform all of those actions.即使执行所有这些操作需要很长时间。

How can it be performed?如何执行? Is there a preferd language to do so?有首选的语言吗? Is it possible with node?节点可以吗?

If you are broadcasting same sms to many users, you should search for a broadcasting command in the sms api.如果您向许多用户广播相同的短信,您应该在短信 api 中搜索广播命令。 This would decrease number of operations/bandwidth required on your server.这将减少服务器上所需的操作/带宽数量。

If broadcasting support doesn't exist, you should let other requests be fetched while processing those sms tasks.如果不存在广播支持,您应该在处理这些短信任务时获取其他请求。 Something like:就像是:

function smsTask(n)
{
     doSmsWork(function(response){ 
          if(n)
          {
                setTimeout(function(){ 
                    smsTask(n-1);
                },0);
          }
     })
}

smsTask(num);

Between if(n) and smsTask(n-1) , other asynchronous tasks can find time to work, including fetching/intercepting new smsTasks and RAM requirement depends only on number of requests in-flight, not number of sms tasks.if(n)smsTask(n-1)之间,其他异步任务可以找到工作时间,包括获取/拦截新的 smsTasks 和 RAM 需求仅取决于正在进行的请求数,而不是 sms 任务数。 If you need to cap the bandwidth for all sms tasks, you can use a dynamical waiting value (instead of 0) for the setTimeout function and this can dedicate more bandwidth to other tasks (like serving web pages, etc) instead of being fully consumed by sms spam.如果您需要限制所有短信任务的带宽,您可以为setTimeout function 使用动态等待值(而不是 0),这可以将更多带宽专用于其他任务(例如服务 web 页面等),而不是完全消耗通过短信垃圾邮件。

If you don't ask for asynchronously letting new requests be handled, then you can complete whole work per request much quicker than 2 minutes, if its I/O bound:如果您不要求异步处理新请求,那么您可以在 2 分钟内完成每个请求的整个工作,如果它的 I/O 绑定:

function smsTask(n)
{
     for(let i=0;i<n;i++)
         doSmsWork(function(response){ 
          
         })
}

smsTask(num);

in perfect I/O conditions, this can complete within 200ms-300ms even for n=1000.在完美的 I/O 条件下,即使 n=1000,这也可以在 200ms-300ms 内完成。 This still doesn't stop new tasks/requests but puts too much pressure on the queue and probably consumes more memory while other version keeps stead memory consumption depending on number of requests in flight only.这仍然不会停止新的任务/请求,但会给队列带来太大的压力,并且可能会消耗更多的 memory 而其他版本保持不变 memory 消耗仅取决于飞行中的请求数量。

If you need even less I/O contention, you can put the tasks into a queue and have a dedicated function for processing the queue:如果您需要更少的 I/O 争用,您可以将任务放入队列中,并使用专用的 function 来处理队列:

let work = [];
function smsTask(n){ work.push(some_task(n)); }

setInterval(function(){
    if(work.length > 0)
    {
        let task = work.shift();
        task?.compute(); // does sms work
    }
},1000);

This is steady-state processing and still doesn't stop because of sudden request spikes as long as work queue does not overflow memory.这是稳态处理,只要工作队列没有溢出 memory,就不会因为突然的请求峰值而停止。 Maybe setTimeout version of this is better for CPU.也许这个的 setTimeout 版本更适合 CPU。

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

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