-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
http: added closeAllConnections and closeIdleConnections to http.server #42812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6781776
http: added connection closing methods
ShogunPanda d1be124
http: fix set push/pop
ShogunPanda ed4cf86
http: assert connections count in test
ShogunPanda 432062a
http: linted docs
ShogunPanda 96530af
http: make sure connections checking is started
ShogunPanda 0d07a99
http: linted code
ShogunPanda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,57 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const { createServer } = require('http'); | ||
const { connect } = require('net'); | ||
|
||
let connections = 0; | ||
|
||
const server = createServer(common.mustCall(function(req, res) { | ||
res.writeHead(200, { Connection: 'keep-alive' }); | ||
res.end(); | ||
}), { | ||
headersTimeout: 0, | ||
keepAliveTimeout: 0, | ||
requestTimeout: common.platformTimeout(60000), | ||
}); | ||
|
||
server.on('connection', function() { | ||
connections++; | ||
}); | ||
|
||
server.listen(0, function() { | ||
const port = server.address().port; | ||
|
||
// Create a first request but never finish it | ||
const client1 = connect(port); | ||
|
||
client1.on('close', common.mustCall()); | ||
|
||
client1.on('error', () => {}); | ||
|
||
client1.write('GET / HTTP/1.1'); | ||
|
||
// Create a second request, let it finish but leave the connection opened using HTTP keep-alive | ||
const client2 = connect(port); | ||
let response = ''; | ||
|
||
client2.on('data', common.mustCall((chunk) => { | ||
response += chunk.toString('utf-8'); | ||
|
||
if (response.endsWith('0\r\n\r\n')) { | ||
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive')); | ||
assert.strictEqual(connections, 2); | ||
|
||
server.closeAllConnections(); | ||
ShogunPanda marked this conversation as resolved.
Show resolved
Hide resolved
|
||
server.close(common.mustCall()); | ||
|
||
// This timer should never go off as the server.close should shut everything down | ||
setTimeout(common.mustNotCall(), common.platformTimeout(1500)).unref(); | ||
} | ||
})); | ||
|
||
client2.on('close', common.mustCall()); | ||
|
||
client2.write('GET / HTTP/1.1\r\n\r\n'); | ||
}); |
This file contains hidden or 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,69 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const { createServer } = require('http'); | ||
const { connect } = require('net'); | ||
|
||
let connections = 0; | ||
|
||
const server = createServer(common.mustCall(function(req, res) { | ||
res.writeHead(200, { Connection: 'keep-alive' }); | ||
res.end(); | ||
}), { | ||
headersTimeout: 0, | ||
keepAliveTimeout: 0, | ||
requestTimeout: common.platformTimeout(60000), | ||
}); | ||
|
||
server.on('connection', function() { | ||
connections++; | ||
}); | ||
|
||
server.listen(0, function() { | ||
const port = server.address().port; | ||
let client1Closed = false; | ||
let client2Closed = false; | ||
|
||
// Create a first request but never finish it | ||
const client1 = connect(port); | ||
|
||
client1.on('close', common.mustCall(() => { | ||
client1Closed = true; | ||
})); | ||
|
||
client1.on('error', () => {}); | ||
|
||
client1.write('GET / HTTP/1.1'); | ||
|
||
// Create a second request, let it finish but leave the connection opened using HTTP keep-alive | ||
const client2 = connect(port); | ||
let response = ''; | ||
|
||
client2.on('data', common.mustCall((chunk) => { | ||
response += chunk.toString('utf-8'); | ||
|
||
if (response.endsWith('0\r\n\r\n')) { | ||
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive')); | ||
assert.strictEqual(connections, 2); | ||
|
||
server.closeIdleConnections(); | ||
server.close(common.mustCall()); | ||
|
||
// Check that only the idle connection got closed | ||
setTimeout(common.mustCall(() => { | ||
assert(!client1Closed); | ||
assert(client2Closed); | ||
|
||
server.closeAllConnections(); | ||
server.close(common.mustCall()); | ||
}), common.platformTimeout(500)).unref(); | ||
} | ||
})); | ||
|
||
client2.on('close', common.mustCall(() => { | ||
client2Closed = true; | ||
})); | ||
|
||
client2.write('GET / HTTP/1.1\r\n\r\n'); | ||
}); |
This file contains hidden or 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,68 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
if (!common.hasCrypto) { | ||
common.skip('missing crypto'); | ||
} | ||
|
||
const { createServer } = require('https'); | ||
const { connect } = require('tls'); | ||
|
||
const fixtures = require('../common/fixtures'); | ||
|
||
const options = { | ||
key: fixtures.readKey('agent1-key.pem'), | ||
cert: fixtures.readKey('agent1-cert.pem') | ||
}; | ||
|
||
let connections = 0; | ||
|
||
const server = createServer(options, common.mustCall(function(req, res) { | ||
res.writeHead(200, { Connection: 'keep-alive' }); | ||
res.end(); | ||
}), { | ||
headersTimeout: 0, | ||
keepAliveTimeout: 0, | ||
requestTimeout: common.platformTimeout(60000), | ||
}); | ||
|
||
server.on('connection', function() { | ||
connections++; | ||
}); | ||
|
||
server.listen(0, function() { | ||
const port = server.address().port; | ||
|
||
// Create a first request but never finish it | ||
const client1 = connect({ port, rejectUnauthorized: false }); | ||
|
||
client1.on('close', common.mustCall()); | ||
|
||
client1.on('error', () => {}); | ||
|
||
client1.write('GET / HTTP/1.1'); | ||
|
||
// Create a second request, let it finish but leave the connection opened using HTTP keep-alive | ||
const client2 = connect({ port, rejectUnauthorized: false }); | ||
let response = ''; | ||
|
||
client2.on('data', common.mustCall((chunk) => { | ||
response += chunk.toString('utf-8'); | ||
|
||
if (response.endsWith('0\r\n\r\n')) { | ||
assert(response.startsWith('HTTP/1.1 200 OK\r\nConnection: keep-alive')); | ||
assert.strictEqual(connections, 2); | ||
|
||
server.closeAllConnections(); | ||
server.close(common.mustCall()); | ||
|
||
// This timer should never go off as the server.close should shut everything down | ||
setTimeout(common.mustNotCall(), common.platformTimeout(1500)).unref(); | ||
} | ||
})); | ||
|
||
client2.on('close', common.mustCall()); | ||
|
||
client2.write('GET / HTTP/1.1\r\n\r\n'); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.