Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node server, socket, request and response timeouts

Problem

Node's default configuration timeouts requests after 2 minutes. I would like to change the request timeouts to:

  • 1 minute for 'normal' requests
  • 5 minutes for requests that serve static files (big assets in this case)
  • 8 hours for uploads (couple of thousand pictures per request)

Research

Reading through Node's documentation, I've discovered that there are numerous ways of defining timeouts.

  1. server.setTimeout
  2. socket.setTimeout
  3. request.setTimeout
  4. response.setTimeout

I'm using Express which also provides middleware to define timeout's for (specific) routes. I've tried that, without success.

Question

I'm confused about how to properly configure the timeout limit globally and per route. Should I configure all of the above timeouts? How is setting the server's timeout different to setting the socket's or request's timeout?

like image 619
ndequeker Avatar asked Aug 12 '16 20:08

ndequeker


People also ask

What is request timeout in Nodejs?

timeout is an inbuilt application programming interface of class Server within http module which is used to get the default Timeout value in milliseconds. In Node. js, default server timeout is 0 milliseconds for the latest version and 2min i.e. (120000 milliseconds) for the older version.

What is 200 in writeHead in node JS?

response. writeHead(200) sends a response header to the request. The status code is a 3-digit HTTP status code, like 404.

What are socket timeouts?

TCP Socket Timeouts are caused when a TCP socket times out talking to the far end. Socket timeouts can occur when attempting to connect to a remote server, or during communication, especially long-lived ones.

How many request Nodejs can handle?

js can handle ~15K requests per second, and the vanilla HTTP module can handle 70K rps.


2 Answers

As I saw on your other question concerning the usage of the timeout middleware, you are using it somehow differently.

See documentation of timeout-connect middleware.

Add your errorHandler-function as an EventListener to the request, as it is an EventEmitter and the middleware causes it to emit the timeout-event:

req.on("timeout", function (evt) {
    if (req.timedout) {
      if (!res.headersSent) {
        res
          .status(408)
          .send({
            success: true,
            message: 'Timeout error'
        });
      }
    }
}); 

This is called outside of the middleware stack, causing the function call to next(err) to be invalid. Also, you have to keep in mind, that if the timeout happens while the request is hanging server-side, you have to prevent your server code from further processing this request (because headers are already sent and its underlying connection will no longer be available).

like image 88
krassdanke Avatar answered Nov 03 '22 00:11

krassdanke


Summary

  • nodejs timeout API are all inactivity timeout
  • expressjs/timeout package is response hard timeout

nodejs timeout API

server.timeout

  • inactivity/idle timeout
  • equal to socket timeout
  • default 2min

server.setTimeout

  • inactivity/idle timeout
  • equal to socket timeout
  • default 2min
  • have callback

socket.setTimeout

  • inactivity/idle timeout
  • callback responsible to end(), destroy() socket
  • default no timeout

response.setTimeout

  • socket.setTimeout front end

request.setTimeout

  • socket.setTimeout front end

expressjs/timeout package

  • response hard-timeout (vs inactivity)
  • have callback

Conclusion

  • max. time allowed for an action(request+response), express/timeout package is needed.

    This is properly what you need, but the callback need to end the request/response. As the timeout only trigger the callback, it does not change the state or interfere with the connection. It is the callback job.

  • idle timeout, set nodejs api request/response timeout

    I don't recommend touching these, as it is not necessary in most cases. Unless you want to allow a connection to idle(no traffic) over 2min.

like image 38
John Siu Avatar answered Nov 03 '22 00:11

John Siu