-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Conversation
2b5d67f
to
661251d
Compare
@@ -486,6 +487,7 @@ function connectionListenerInternal(server, socket) { | |||
// need to pause TCP socket/HTTP parser, and wait until the data will be | |||
// sent to the client. | |||
outgoingData: 0, | |||
requestsCount: 0, |
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
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.
If you start adding unit tests (even failing) it would help.
lib/_http_server.js
Outdated
@@ -876,6 +878,7 @@ function parserOnIncoming(server, socket, state, req, keepAlive) { | |||
|
|||
const res = new server[kServerResponse](req); | |||
res._keepAliveTimeout = server.keepAliveTimeout; | |||
res._maxRequestsPerSocket = server.maxRequestsPerSocket; |
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.
Why is this copied here?
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.
I'm passing maxRequestsPerSocket
to res
since I need to set the max
value on "keep-alive` header on response
https://github.com/nodejs/node/pull/40082/files/64132000c63934af235ae59095faad2015b3a045#diff-48d21edbddb6e855d1ee5716c49bcdc0d913c11ee8a24a98ea7dbc60cd253556R463
Didn't found a way to do it better
lib/_http_server.js
Outdated
|
||
if (server.maxRequestsPerSocket < ++state.requestsCount) { | ||
res.writeHead(503); | ||
res.end(); |
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.
Is connection close correctly set here? I think shouldKeepAlive should be set to false.
lib/_http_server.js
Outdated
res.end(); | ||
} | ||
|
||
res.shouldKeepAlive = server.maxRequestsPerSocket > state.requestsCount |
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.
res.shouldKeepAlive = server.maxRequestsPerSocket > state.requestsCount | |
res.shouldKeepAlive = server.maxRequestsPerSocket >= state.requestsCount |
I think this should go to false if they are the same.
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.
In the current code the shouldKeepAlive
will be false
if they are equal
the shouldKeepAlive
is set to true only if requestsCount
is small enough
server.maxRequestsPerSocket > state.requestsCount
Sure, Im on it right now, it just a bit complicated for me to understand how to run them (only my tests) |
./node test/parallel/test-your-new-test.js |
12d6866
to
d64d618
Compare
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.
can you please add another test with a pipelined request coming in before the "last" (allowed) one completes.
I created a test sending 4 requests in a row with a timeout on the server side before response (timeout 5 sec) it is for sure received after the the last allow is completed, also see the code is reaching the 503 handler res.writeHead(503);
res.end(); But it is not returned from the server (503) as a response, I see only the first 3 This is what I get as a response for 4 requests when limit is set to 3
|
I used another flag instead of results:
|
f533750
to
84d4e8e
Compare
not sure what is "test-asan" and why it failed on timeout |
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.
According to
https://datatracker.ietf.org/doc/html/draft-thomson-hybi-http-timeout-03#section-2.2.1
The "max" parameter has been used to indicate the maximum number of
requests that would be made on the connection. This parameter is
deprecated. Any limit on requests can be enforced by sending
"Connection: close" and closing the connection.
max
is deprecated and should not be sent.
Adding a limit is actually fine and could help in production deployments let's just add that.
2ed33eb
to
4d0ca75
Compare
Done, thanks! |
4d0ca75
to
5b35ffe
Compare
@mcollina anything else should be done? or should I close this PR ? |
5b35ffe
to
c5ae814
Compare
Filed again with some strange error
|
Should be any semver label here? (minor/major) |
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.
lgtm
Landed in 4e8f11d |
Notable changes: crypto: * (SEMVER-MINOR) add rsa-pss keygen parameters (Filip Skokan) #39927 doc: * add Ayase-252 to collaborators (Qingyu Deng) #40078 fs: * (SEMVER-MINOR) make `open` and `close` stream override optional when unused (Antoine du Hamel) #40013 http: * (SEMVER-MINOR) limit requests per connection (Artur K) #40082 src: * (SEMVER-MINOR) add --no-global-search-paths cli option (Cheng Zhao) #39754 * (SEMVER-MINOR) add option to disable global search paths (Cheng Zhao) #39754 * (SEMVER-MINOR) make napi_create_reference accept symbol (JckXia) #39926 stream: * (SEMVER-MINOR) add signal support to pipeline generators (Robert Nagy) #39067 PR-URL: TODO
Notable changes: crypto: * (SEMVER-MINOR) add rsa-pss keygen parameters (Filip Skokan) #39927 doc: * add Ayase-252 to collaborators (Qingyu Deng) #40078 fs: * (SEMVER-MINOR) make `open` and `close` stream override optional when unused (Antoine du Hamel) #40013 http: * (SEMVER-MINOR) limit requests per connection (Artur K) #40082 src: * (SEMVER-MINOR) add --no-global-search-paths cli option (Cheng Zhao) #39754 * (SEMVER-MINOR) add option to disable global search paths (Cheng Zhao) #39754 * (SEMVER-MINOR) make napi_create_reference accept symbol (JckXia) #39926 stream: * (SEMVER-MINOR) add signal support to pipeline generators (Robert Nagy) #39067 PR-URL: TODO
Notable changes: crypto: * (SEMVER-MINOR) add rsa-pss keygen parameters (Filip Skokan) #39927 doc: * add Ayase-252 to collaborators (Qingyu Deng) #40078 fs: * (SEMVER-MINOR) make `open` and `close` stream override optional when unused (Antoine du Hamel) #40013 http: * (SEMVER-MINOR) limit requests per connection (Artur K) #40082 src: * (SEMVER-MINOR) add --no-global-search-paths cli option (Cheng Zhao) #39754 * (SEMVER-MINOR) add option to disable global search paths (Cheng Zhao) #39754 * (SEMVER-MINOR) make napi_create_reference accept symbol (JckXia) #39926 stream: * (SEMVER-MINOR) add signal support to pipeline generators (Robert Nagy) #39067 PR-URL: #40175
Notable changes: crypto: * (SEMVER-MINOR) add rsa-pss keygen parameters (Filip Skokan) #39927 doc: * add Ayase-252 to collaborators (Qingyu Deng) #40078 fs: * (SEMVER-MINOR) make `open` and `close` stream override optional when unused (Antoine du Hamel) #40013 http: * (SEMVER-MINOR) limit requests per connection (Artur K) #40082 src: * (SEMVER-MINOR) add --no-global-search-paths cli option (Cheng Zhao) #39754 * (SEMVER-MINOR) add option to disable global search paths (Cheng Zhao) #39754 * (SEMVER-MINOR) make napi_create_reference accept symbol (JckXia) #39926 stream: * (SEMVER-MINOR) add signal support to pipeline generators (Robert Nagy) #39067 PR-URL: #40175
Trying to close #40071
Still missing tests and docs