Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js: What is the difference between server.setTimeout, server.timeout and server.keepAliveTimeout

Tags:

http

node.js

The official documentation is here.

There is no mention in server.setTimeout about servicing next request. So I presume that it is independent of any future requests or time to service the first request? What if the first request takes more time to service then server.setTimeout?

server.timeout is time of inactivity before the socket times out. It does not distinguish between activity of incoming data or sending outgoing data.

server.keepAliveTimeout refers again to inactivity but to activity concerning incoming data - Time a server needs to wait for additional incoming data, after it has finished writing the last response. If the server receives new data before the keep-alive timeout has fired, it will reset the regular inactivity timeout, i.e., server.timeout. When does it reset? After the first byte of the new data is received or after the last byte? Does new data refer to data of new request?

server.timeout is tied to inactivity (both incoming and outgoing data). server.keepAlive refers only to incoming data that impacts the resetting of server.timeout.

How are these parameters impacted in HTTP 2.0?

Sounds confusing to me. Is there a more clear explanation documented somewhere? Or can someone explain this in more clear terms?

Edit: I am using Version 8.x LTS

like image 806
Sunny Avatar asked Aug 20 '18 17:08

Sunny


Video Answer


1 Answers

Consider the following programs and their behavior whenever we make a request to the listening server with curl localhost:3000. They will all share the following:

const http= require("http");
const server = http.createServer((req, res) => {
    console.log("Got request");

    setTimeout(() => {
        res.end("Hello\n");
    }, 10 * 1000);
});

// ... snippets below

server.listen(3000);

on("timeout") with server.timeout set

We just listen for the timeout event. The timeout value is set to 5000 and it prints as such.

server.on("timeout", () => {
    console.log(server.timeout);
    console.log("Timeout event");
});

server.timeout = 5 * 1000;

setTimeout() and server.timeout set

Even though we set a time in setTimeout(), we don't see the Timeout CB until after 5 seconds.

server.setTimeout(1 * 1000, () => {
    console.log(server.timeout);
    console.log("Timeout CB");
});

server.timeout = 5 * 1000;

setTimeout() with no server.timeout set

We see the timeout handler happen after 1 second as apposed to the default 2 minutes

server.on("timeout", () => {
    console.log(server.timeout);
    console.log("Timeout event");
});

on("timeout") with no server.timeout

This also sets the server to respond after 10 minutes instead of 10 seconds. After the default 2 minutes, we see the callback triggered normally.

With both on() and setTimeout()

The callback is triggered after 1 second and the server.timeout is set to 1000.

Conclusion

From that, we can assume that setTimeout is a way to set timeout programmatically. timeout will set the timeout but not set a callback. on("timeout") registers a callback that will fire after a timeout event fires.

like image 160
zero298 Avatar answered Sep 19 '22 12:09

zero298