2

I have a node app that makes large number of request to another server using restify.

As the number of requests increases,memory keeps on growing and ultimately the app crashes.

Below is the snippet of my code.

var restify = require('restify');
var client = restify.createStringClient({
    url: "https://api-3t.sandbox.paypal.com/nvp"
});

function makeRequest(cb) {
    counter++;
    client.post({
            headers: null
        }, null, function(err, req, res, data) {
            if(err)
            {
                cb(err, req, res, data);
            }
            else
            {
                cb(null, req, res, data);
            } 
    });
}

function makeRequestCb(error,request,response,data){
    counter++;
    console.log("result",counter);
}

setInterval(function(){
        makeRequest(makeRequestCb)
}, 1000);

On checking the heap snapshot in chrome I found that 'buffer' is taking up most of the space.

I have also tries using 'request' module instead of 'restify' What am I doing wrong here. What is holding up most of the space?

I have checked this post as well. But in my case the memory usage just keeps on increasing.

Please help.

Community
  • 1
  • 1
Sarita
  • 837
  • 11
  • 19
  • Well it seems to me like your DoS your own app, you are posting to a new request every second, regardless of whether the external server has handled the previous request or not, and expecting it to just handle it. The external server could be running slow or simply not able to handle that many requests p/sec and blocking your requests but you just keep queuing them up regardless, is it really that surprising that your server eventually runs out of memory? – James Nov 18 '14 at 10:06
  • @James The average response time of my requests are around 1sec - 1.5sec. The request and response counter are almost the same when interval is 1 sec, so there are not that many pending requests. Have tried increasing the interval to 3 secs as well. – Sarita Nov 18 '14 at 10:42

0 Answers0