-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Limit requests per connection #40082
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0ab2852
http: limit requests per connection
fatal10110 dc611ee
http: fix condition
fatal10110 8f55f74
http: close on last request
fatal10110 3dd4770
http: add test, fix write after end
fatal10110 719eba9
http: refactor test
fatal10110 b1d70f6
http: fix 503 on max reached
fatal10110 729fd5a
http: fix lint
fatal10110 063a83c
http: max requests per socket docs
fatal10110 c5ae814
http: remove "max" hint
fatal10110 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 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,116 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const net = require('net'); | ||
const http = require('http'); | ||
const assert = require('assert'); | ||
|
||
const bodySent = 'This is my request'; | ||
|
||
function assertResponse(headers, body, expectClosed) { | ||
if (expectClosed) { | ||
assert.match(headers, /Connection: close\r\n/m); | ||
assert.strictEqual(headers.search(/Keep-Alive: timeout=5\r\n/m), -1); | ||
assert.match(body, /Hello World!/m); | ||
} else { | ||
assert.match(headers, /Connection: keep-alive\r\n/m); | ||
assert.match(headers, /Keep-Alive: timeout=5\r\n/m); | ||
assert.match(body, /Hello World!/m); | ||
} | ||
} | ||
|
||
function writeRequest(socket, withBody) { | ||
if (withBody) { | ||
socket.write('POST / HTTP/1.1\r\n'); | ||
socket.write('Connection: keep-alive\r\n'); | ||
socket.write('Content-Type: text/plain\r\n'); | ||
socket.write(`Content-Length: ${bodySent.length}\r\n\r\n`); | ||
socket.write(`${bodySent}\r\n`); | ||
socket.write('\r\n\r\n'); | ||
} else { | ||
socket.write('GET / HTTP/1.1\r\n'); | ||
socket.write('Connection: keep-alive\r\n'); | ||
socket.write('\r\n\r\n'); | ||
} | ||
} | ||
|
||
const server = http.createServer((req, res) => { | ||
let body = ''; | ||
req.on('data', (data) => { | ||
body += data; | ||
}); | ||
|
||
req.on('end', () => { | ||
if (req.method === 'POST') { | ||
assert.strictEqual(bodySent, body); | ||
} | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.write('Hello World!'); | ||
res.end(); | ||
}); | ||
}); | ||
|
||
function initialRequests(socket, numberOfRequests, cb) { | ||
let buffer = ''; | ||
|
||
writeRequest(socket); | ||
|
||
socket.on('data', (data) => { | ||
buffer += data; | ||
|
||
if (buffer.endsWith('\r\n\r\n')) { | ||
if (--numberOfRequests === 0) { | ||
socket.removeAllListeners('data'); | ||
cb(); | ||
} else { | ||
const [headers, body] = buffer.trim().split('\r\n\r\n'); | ||
assertResponse(headers, body); | ||
buffer = ''; | ||
writeRequest(socket, true); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
|
||
server.maxRequestsPerSocket = 3; | ||
server.listen(0, common.mustCall((res) => { | ||
const socket = new net.Socket(); | ||
const anotherSocket = new net.Socket(); | ||
|
||
socket.on('end', common.mustCall(() => { | ||
server.close(); | ||
})); | ||
|
||
socket.on('ready', common.mustCall(() => { | ||
// Do 2 of 3 allowed requests and ensure they still alive | ||
initialRequests(socket, 2, common.mustCall(() => { | ||
anotherSocket.connect({ port: server.address().port }); | ||
})); | ||
})); | ||
|
||
anotherSocket.on('ready', common.mustCall(() => { | ||
// Do another 2 requests with another socket | ||
// enusre that this will not affect the first socket | ||
initialRequests(anotherSocket, 2, common.mustCall(() => { | ||
let buffer = ''; | ||
|
||
// Send the rest of the calls to the first socket | ||
// and see connection is closed | ||
socket.on('data', common.mustCall((data) => { | ||
buffer += data; | ||
|
||
if (buffer.endsWith('\r\n\r\n')) { | ||
const [headers, body] = buffer.trim().split('\r\n\r\n'); | ||
assertResponse(headers, body, true); | ||
anotherSocket.end(); | ||
socket.end(); | ||
} | ||
})); | ||
|
||
writeRequest(socket, true); | ||
})); | ||
})); | ||
|
||
socket.connect({ port: server.address().port }); | ||
})); |
86 changes: 86 additions & 0 deletions
86
test/parallel/test-http-keep-alive-pipeline-max-requests.js
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,86 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const net = require('net'); | ||
const http = require('http'); | ||
const assert = require('assert'); | ||
|
||
const bodySent = 'This is my request'; | ||
|
||
function assertResponse(headers, body, expectClosed) { | ||
if (expectClosed) { | ||
assert.match(headers, /Connection: close\r\n/m); | ||
assert.strictEqual(headers.search(/Keep-Alive: timeout=5\r\n/m), -1); | ||
assert.match(body, /Hello World!/m); | ||
} else { | ||
assert.match(headers, /Connection: keep-alive\r\n/m); | ||
assert.match(headers, /Keep-Alive: timeout=5\r\n/m); | ||
assert.match(body, /Hello World!/m); | ||
} | ||
} | ||
|
||
function writeRequest(socket) { | ||
socket.write('POST / HTTP/1.1\r\n'); | ||
socket.write('Connection: keep-alive\r\n'); | ||
socket.write('Content-Type: text/plain\r\n'); | ||
socket.write(`Content-Length: ${bodySent.length}\r\n\r\n`); | ||
socket.write(`${bodySent}\r\n`); | ||
socket.write('\r\n\r\n'); | ||
} | ||
|
||
const server = http.createServer((req, res) => { | ||
let body = ''; | ||
req.on('data', (data) => { | ||
body += data; | ||
}); | ||
|
||
req.on('end', () => { | ||
if (req.method === 'POST') { | ||
assert.strictEqual(bodySent, body); | ||
} | ||
|
||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.write('Hello World!'); | ||
res.end(); | ||
}); | ||
}); | ||
|
||
server.maxRequestsPerSocket = 3; | ||
|
||
server.listen(0, common.mustCall((res) => { | ||
const socket = new net.Socket(); | ||
|
||
socket.on('end', common.mustCall(() => { | ||
server.close(); | ||
})); | ||
|
||
socket.on('ready', common.mustCall(() => { | ||
writeRequest(socket); | ||
writeRequest(socket); | ||
writeRequest(socket); | ||
writeRequest(socket); | ||
})); | ||
|
||
let buffer = ''; | ||
|
||
socket.on('data', (data) => { | ||
buffer += data; | ||
|
||
const responseParts = buffer.trim().split('\r\n\r\n'); | ||
|
||
if (responseParts.length === 8) { | ||
assertResponse(responseParts[0], responseParts[1]); | ||
assertResponse(responseParts[2], responseParts[3]); | ||
assertResponse(responseParts[4], responseParts[5], true); | ||
|
||
assert.match(responseParts[6], /HTTP\/1\.1 503 Service Unavailable/m); | ||
assert.match(responseParts[6], /Connection: close\r\n/m); | ||
assert.strictEqual(responseParts[6].search(/Keep-Alive: timeout=5\r\n/m), -1); | ||
assert.strictEqual(responseParts[7].search(/Hello World!/m), -1); | ||
|
||
socket.end(); | ||
} | ||
}); | ||
|
||
socket.connect({ port: server.address().port }); | ||
})); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure yet if state is per all sockets or there is a state per socket
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is for each individual socket