-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: destroy sockets after keepAliveTimeout
Implement server.keepAliveTimeout in addition to server.timeout to prevent temporary socket/memory leaking in keep-alive mode. PR-URL: #2534 Author: Timur Shemsedinov <timur.shemsedinov@gmail.com> Author: Alexey Orlenko <eaglexrlnk@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
- Loading branch information
1 parent
bb77cce
commit a7f5c9c
Showing
6 changed files
with
252 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
const net = require('net'); | ||
|
||
const tests = []; | ||
|
||
function test(fn) { | ||
if (!tests.length) { | ||
process.nextTick(run); | ||
} | ||
tests.push(fn); | ||
} | ||
|
||
function run() { | ||
const fn = tests.shift(); | ||
if (fn) fn(run); | ||
} | ||
|
||
test(function serverEndKeepAliveTimeoutWithPipeline(cb) { | ||
let socket; | ||
let destroyedSockets = 0; | ||
let timeoutCount = 0; | ||
let requestCount = 0; | ||
process.on('exit', () => { | ||
assert.strictEqual(timeoutCount, 1); | ||
assert.strictEqual(requestCount, 3); | ||
assert.strictEqual(destroyedSockets, 1); | ||
}); | ||
const server = http.createServer((req, res) => { | ||
socket = req.socket; | ||
requestCount++; | ||
res.end(); | ||
}); | ||
server.setTimeout(200, (socket) => { | ||
timeoutCount++; | ||
socket.destroy(); | ||
}); | ||
server.keepAliveTimeout = 50; | ||
server.listen(0, common.mustCall(() => { | ||
const options = { | ||
port: server.address().port, | ||
allowHalfOpen: true | ||
}; | ||
const c = net.connect(options, () => { | ||
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
}); | ||
setTimeout(() => { | ||
server.close(); | ||
if (socket.destroyed) destroyedSockets++; | ||
cb(); | ||
}, 1000); | ||
})); | ||
}); | ||
|
||
test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) { | ||
let socket; | ||
let destroyedSockets = 0; | ||
let timeoutCount = 0; | ||
let requestCount = 0; | ||
process.on('exit', () => { | ||
assert.strictEqual(timeoutCount, 1); | ||
assert.strictEqual(requestCount, 3); | ||
assert.strictEqual(destroyedSockets, 1); | ||
}); | ||
const server = http.createServer((req, res) => { | ||
socket = req.socket; | ||
requestCount++; | ||
}); | ||
server.setTimeout(200, (socket) => { | ||
timeoutCount++; | ||
socket.destroy(); | ||
}); | ||
server.keepAliveTimeout = 50; | ||
server.listen(0, common.mustCall(() => { | ||
const options = { | ||
port: server.address().port, | ||
allowHalfOpen: true | ||
}; | ||
const c = net.connect(options, () => { | ||
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
}); | ||
setTimeout(() => { | ||
server.close(); | ||
if (socket.destroyed) destroyedSockets++; | ||
cb(); | ||
}, 1000); | ||
})); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const https = require('https'); | ||
const tls = require('tls'); | ||
const fs = require('fs'); | ||
|
||
const tests = []; | ||
|
||
const serverOptions = { | ||
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'), | ||
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem') | ||
}; | ||
|
||
function test(fn) { | ||
if (!tests.length) { | ||
process.nextTick(run); | ||
} | ||
tests.push(fn); | ||
} | ||
|
||
function run() { | ||
const fn = tests.shift(); | ||
if (fn) fn(run); | ||
} | ||
|
||
test(function serverKeepAliveTimeoutWithPipeline(cb) { | ||
let socket; | ||
let destroyedSockets = 0; | ||
let timeoutCount = 0; | ||
let requestCount = 0; | ||
process.on('exit', function() { | ||
assert.strictEqual(timeoutCount, 1); | ||
assert.strictEqual(requestCount, 3); | ||
assert.strictEqual(destroyedSockets, 1); | ||
}); | ||
const server = https.createServer(serverOptions, (req, res) => { | ||
socket = req.socket; | ||
requestCount++; | ||
res.end(); | ||
}); | ||
server.setTimeout(200, (socket) => { | ||
timeoutCount++; | ||
socket.destroy(); | ||
}); | ||
server.keepAliveTimeout = 50; | ||
server.listen(0, common.mustCall(() => { | ||
const options = { | ||
port: server.address().port, | ||
allowHalfOpen: true, | ||
rejectUnauthorized: false | ||
}; | ||
const c = tls.connect(options, () => { | ||
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
}); | ||
setTimeout(() => { | ||
server.close(); | ||
if (socket.destroyed) destroyedSockets++; | ||
cb(); | ||
}, 1000); | ||
})); | ||
}); | ||
|
||
test(function serverNoEndKeepAliveTimeoutWithPipeline(cb) { | ||
let socket; | ||
let destroyedSockets = 0; | ||
let timeoutCount = 0; | ||
let requestCount = 0; | ||
process.on('exit', () => { | ||
assert.strictEqual(timeoutCount, 1); | ||
assert.strictEqual(requestCount, 3); | ||
assert.strictEqual(destroyedSockets, 1); | ||
}); | ||
const server = https.createServer(serverOptions, (req, res) => { | ||
socket = req.socket; | ||
requestCount++; | ||
}); | ||
server.setTimeout(200, (socket) => { | ||
timeoutCount++; | ||
socket.destroy(); | ||
}); | ||
server.keepAliveTimeout = 50; | ||
server.listen(0, common.mustCall(() => { | ||
const options = { | ||
port: server.address().port, | ||
allowHalfOpen: true, | ||
rejectUnauthorized: false | ||
}; | ||
const c = tls.connect(options, () => { | ||
c.write('GET /1 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
c.write('GET /2 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
c.write('GET /3 HTTP/1.1\r\nHost: localhost\r\n\r\n'); | ||
}); | ||
setTimeout(() => { | ||
server.close(); | ||
if (socket && socket.destroyed) destroyedSockets++; | ||
cb(); | ||
}, 1000); | ||
})); | ||
}); |