Skip to content

Instantly share code, notes, and snippets.

@Samjin
Forked from shuhei/headers-timeout-keep-alive.js
Created December 15, 2021 17:28
Show Gist options
  • Save Samjin/c0f2e0952059ccab60a8137e105d8da2 to your computer and use it in GitHub Desktop.
Save Samjin/c0f2e0952059ccab60a8137e105d8da2 to your computer and use it in GitHub Desktop.
A test case for server.headersTimeout + keep alive (fails on Node v10.15.2 and newer)
const http = require("http");
const net = require("net");
const server = http.createServer((req, res) => {
req.resume();
res.end("hello");
});
server.keepAliveTimeout = 6 * 1000;
server.headersTimeout = 4 * 1000;
server.listen(0, () => {
const { address, port } = server.address();
console.log("Listening on %s:%d", address, port);
startClient(port);
});
function startClient(port) {
const socket = net.createConnection({ port });
let responseCount = 0;
socket.on("data", (chunk) => {
console.log("client data:", chunk.toString("utf8").split("\r\n")[0]);
responseCount += 1;
if (responseCount === 2) {
process.exit(0);
}
});
socket.on("error", (err) => {
console.log("client error:", err);
process.exit(1);
});
socket.write("GET / HTTP/1.1\r\n");
socket.write("Host: localhost\r\n");
socket.write("Connection: keep-alive\r\n");
socket.write("Agent: node\r\n");
socket.write("\r\n");
setTimeout(() => {
socket.write("GET / HTTP/1.1\r\n");
socket.write("Host: localhost\r\n");
socket.write("Connection: keep-alive\r\n");
socket.write("Agent: node\r\n");
// `headersTimeout` doesn't seem to fire if request headers
// are sent in one packet.
setTimeout(() => {
socket.write("\r\n");
}, 10);
}, 5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment