Question

I use restify to implement a node.js server. Basically the server runs a time-consuming process per a HTTP POST request, but somehow the socket gets closed and the client receives an error message like this:

[Error: socket hang up] code: 'ECONNRESET'

According to the error type, the socket is definitely closed on the server side.

Is there any option that I can set in the createServer method of the restify to solve this problem?

Edit: The long running process is using Mongoose to run MongoDB process. Maybe it is also possible that the socket hangup is caused by the connection to MongoDB? How to increase the timeout for Mongoose? I found that the hang up happened in exactly 120 seconds, so it might be because of some default timeout configuration?

Thanks in advance!

Was it helpful?

Solution

You can use the standard socket on the req object, and manually call setTimeout to increase the time before node hangs up the socket. By default, node has a 2 minute timer on all sockets for inactivity, which is why you are getting hang ups at exactly 120s (this has nothing to do with restify). As an example of increasing that, set up a handler to run before your long running task like this:

server.use(function (req, res, next) {
    // This will set the idle timer to 10 minutes
    req.connection.setTimeout(600 * 1000);
    res.connection.setTimeout(600 * 1000); //**Edited**
    next();
});

OTHER TIPS

This seams not to be actually implemented

https://github.com/mcavage/node-restify/issues/288

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top