ノードv6.17でこのコードを実行すると、キープアライブが機能していないことが示唆されます
function notifyNotKeptAlive(httpObject, eventName, httpObjectName){
httpObject.on(eventName, function(socket){
socket.on('close', function(){
console.log("Yeeouch ! "+httpObjectName+" connection was not kept alive!");
});
});
}
var http=require('http');
var count=1;
var srv = http.createServer(function (req, res) {
var secs;
if ( (count%2)==1){secs=3;}
else {secs=1;}
console.log("Got http request #"+count+" so server waits "+secs+" seconds to respond");
var countForThisClosure=count;
setTimeout(function(){
console.log("Waking up from sleep of "+secs);
res.writeHead(200, {'Content-Type': 'text/plain'});
if (secs===3){
res.end('Visit #'+countForThisClosure+' costing a wait of '+secs+' seconds !!');
} else {
res.end('Visit #'+countForThisClosure+' costing a wait of '+secs+' seconds !!');
}
}, secs*1000);
count++;
}).listen(8090);
notifyNotKeptAlive(srv, 'connection', 'http server');
for (var i=0;i<2;i++){
var req=http.request( {'host': 'localhost', 'port':8090}, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('INCOMING <---' + chunk);
});
}
);
notifyNotKeptAlive(req, 'socket', 'http client');
req.end();
}
IT は、Epeli が提供してくれた http lib ソースの 1263 行目に感嘆符を追加したときにのみ機能しました。したがって、"if (req.shouldKeepAlive){" と書かれている以下の行は、代わりに "if (! req.shouldKeepAlive){" res.on('end', function() {
if (req.shouldKeepAlive) {
socket.removeListener('close', closeListener);
socket.removeListener('error', errorListener);
socket.emit('free');
}
});
この行は、1113 行目から始まる ClientRequest.prototype.onSocket クロージャー内に設定された on'end' コールバック内にあります。
この http lib ソースへのリンクは (Epeli のものと同じ) https://github.com/joyent/node/blob/e8067cb68563e3f3ab760e3ce8d0aeb873198f36/lib/http.js#L889です。