node.js process out of memory in http.request loop

后端 未结 3 1857
天涯浪人
天涯浪人 2021-02-02 18:02

In my node.js server i cant figure out, why it runs out of memory. My node.js server makes a remote http request for each http request it receives, therefore i\'ve tried to repl

3条回答
  •  日久生厌
    2021-02-02 18:29

    yes, you trying to queue 1000000 requests before even starting them. This version keeps limited number of request (100):

      function do_1000000_req( cb )
      {
          num_active = 0;
          num_finished = 0;
          num_sheduled = 0;
    
          function shedule()
          {
             while (num_active < 100 && num_sheduled < 1000000) {
                num_active++;
                num_sheduled++;
                mypost(function() {
                   num_active--;
                   num_finished++;
                   if (num_finished == 1000000)
                   {
                      cb();
                      return;
                   } else if (num_sheduled < 1000000)
                      shedule();
               });
             }
          }
      }
    
      do_1000000_req( function() {
          console.log('done!');
      });
    

提交回复
热议问题