Search code examples
node.jshttpkeep-alive

How To use Node Http Server Keep Alive


I've spent a while setting up my own server using the http library and when I came to load test it using jmeter I noticed I hadn't set it up to utilize keep-alive.

I've spent hours trying to figure this one out - and perhaps I have an issue elsewhere - so in very simple terms, how should keep alive be set up?

I've set the relevant headers and tried the following methods I've found online:

server.on('connection', (socket: Socket) => {
            socket.setTimeout(30 * 1000);
            socket.setKeepAlive(true);
        })

and

handleRequest(request: http.IncomingMessage, response: http.ServerResponse) : void {
        request.socket.setKeepAlive(true);
        request.socket.write('hello, world');
        request.socket.end();
}

These just cause jmeter to crash as it the headers make it think the connections are kept alive when they are not. Nothing I am doing seems to let me keep the connection open. Please advise :)


Solution

  • Seems I got myself into a bit of a fuzz over nothing; seem's there isn't anything wrong with my original implementation; however I run into an issue with Ephemeral Port Limits when running my tests in ApacheBench. This blog post by Daniel Mendel explains the problem: here