0

I have an Express NodeJS instance installed on my server. API stop responding after when "I guess" is overloaded. Response is below.

POST /my/api/result - - ms - -

I tried following in ./bin/www

server.on('connection', function(socket) {
  console.log("A new connection was made by a client.");
  socket.setTimeout(300 * 1000);
});

and /app.js

var timeout = require('connect-timeout');
app.use(timeout(6000000));
app.use(haltOnTimedout);
function haltOnTimedout(req, res, next){
    if (!req.timedout) next();
}

but this din't fix the issue.

API definition

router.post('/search',function (req,res,next) {
if(typeof req.body.q == '' || typeof req.body.q == 'undefined'){
    return res.format({
        json: function () {
            res.send({
                status: 404,
                message: "Mandatory parameter/header is missing."
            });
        }
    });
}else{
    mySQLConn.pool(function (err, conn) {
        if(err){
            return res.format({
                json: function () {
                    res.send({
                        status: 500,
                        message: "Unable to connect to database. " + err
                    })
                }
            });
        }else{
            var q = req.body.q;
            var returnData = [];
            if(q.length> 0){

                var query = "select * from abc where title like '" + q + "%' limit 10"
                conn.query(query, function (err, result) {
                    if(err){
                        return res.format({
                            json: function () {
                                res.send({
                                    status: 500,
                                    message: "error while fetching data "+ err
                                })
                            }
                        });
                    }else{

                        return res.format({
                            json: function () {
                                res.send({
                                    status: 200,
                                    data: result
                                })
                            }
                        });
                    }
                })
            }else{
                return res.format({
                    json: function () {
                        res.send({
                            status: 200,
                            data: returnData
                        })
                    }
                });
            }
        }
    });
}

});

once the response is halted top on linux server has following response.

 PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                         
16042 nod       20   0  157700   2144   1520 R  0.3  0.2   0:00.36 top                             
24692 mysql     20   0 1830988 374416   4800 S  0.3 36.8   5:39.80 mysqld 

nod is the user from which the node application is running.

Khan
  • 5,052
  • 5
  • 22
  • 23
  • Typicaly this kind of error happend when you'r missing a `next()` somewhere in a middleware, reaching a deadend – Boris Charpentier Oct 20 '16 at 12:22
  • @BorisCharpentier: edit my question and add how I am defining the API, i believe i believe we can not pass third parameter `next` when working with post. BTW i am new to NODEJS, if something is wrong with this definition you can guide me. – Khan Oct 20 '16 at 12:27
  • See if this helps: http://stackoverflow.com/questions/23925284/how-to-modify-the-nodejs-request-default-timeout-time – Tom Oct 23 '16 at 11:29
  • @Tom this did not resolve the issue `server.timeout = 1000;` – Khan Oct 23 '16 at 17:13
  • is there any module which i can use to prevent this issue? – Khan Oct 24 '16 at 11:25

2 Answers2

0

Every middleware you include must either call next() or it must end the request via res.end(). The latter often happens deeper in the framework code when you call something like res.send() or res.render(), etc.

App.post and router.post still take middleware functions, so you can pass the third param of next if you need it.

Any path in the request that does not ultimately end up with a res.end() will result in a timeout that will crash your server.

Paul
  • 35,689
  • 11
  • 93
  • 122
  • i have place complete code of the API in the question and as you mentioned the API is ending with `res.send()` when and where it finished. – Khan Oct 23 '16 at 17:04
  • Except it's not. If you're getting a server crash b/c of timeout and no response, then there's a code path that either doesn't ever finish, or else it's taking too long to finish. Perhaps the MySQL connection is slow or it's a long running query. Hard to say from the evidence you've provided. – Paul Oct 23 '16 at 17:22
  • I have added server response in the question for your review. – Khan Oct 23 '16 at 17:24
0

I resolve the issue by releasing the connection before sending response and when error happen. conn.release()

Khan
  • 5,052
  • 5
  • 22
  • 23