Skip to content

Instantly share code, notes, and snippets.

@shuhei

shuhei/README.md

Last active May 14, 2019
Embed
What would you like to do?
Node.js server.keepAliveTimeout
const http = require("http");
const net = require("net");
const PORT = 8080;
const agent = new http.Agent({
keepAlive: true,
maxFreeSockets: Infinity
});
let delay = parseInt(process.argv[2], 10);
if (Number.isNaN(delay)) {
delay = 100;
}
console.log("%d ms delay", delay);
function startClients() {
for (let i = 0; i < 500; i++) {
makeRequest(e => {
if (e) {
console.log(i, "error on first request", e);
return;
}
setTimeout(() => {
makeRequest(err => {
if (err) {
console.log(i, "error on second request", err);
}
});
}, delay - 50 + Math.random() * 100);
});
}
}
function makeRequest(cb) {
const req = http.request(
{
host: "localhost",
port: PORT,
agent
},
res => {
res.on("error", cb);
res.on("end", cb);
res.resume();
}
);
req.on("error", cb);
req.end();
}
function countSockets(sockets) {
return Object.values(sockets).reduce((total, socs) => total + socs.length, 0);
}
startClients();
setInterval(() => {
console.log(
"%d sockets, %d free sockets",
countSockets(agent.sockets),
countSockets(agent.freeSockets)
);
}, 1000);
const http = require("http");
const PORT = 8080;
const keepAliveTimeout = parseInt(process.argv[2], 10);
const server = http.createServer((req, res) => {
// req.resume();
res.end("hello");
});
if (!Number.isNaN(keepAliveTimeout)) {
console.log("keepAliveTimeout %d ms", keepAliveTimeout);
server.keepAliveTimeout = keepAliveTimeout;
}
server.headersTimeout = 10 * 1000;
server.listen(PORT, () => {
const { address, port } = server.address();
console.log("Listening on %s:%d", address, port);
let connections = 0;
server.on("connection", socket => {
connections += 1;
socket.once("close", () => {
connections -= 1;
});
});
setInterval(() => {
console.log("%d connections", connections);
}, 1000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.