Skip to content

Limit requests per connection - #40082

Closed
fatal10110 wants to merge 9 commits into
nodejs:masterfrom
fatal10110:max_request_per_socket
Closed

Limit requests per connection#40082
fatal10110 wants to merge 9 commits into
nodejs:masterfrom
fatal10110:max_request_per_socket

Conversation

@fatal10110

@fatal10110fatal10110 commented Sep 11, 2021

Copy link
Copy Markdown
Contributor

Trying to close#40071

Still missing tests and docs

@nodejs-github-botnodejs-github-bot added http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Sep 11, 2021
Comment threadlib/_http_server.js Outdated

Copy link
Copy Markdown
ContributorAuthor

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

Copy link
Copy Markdown
Member

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

Comment threadlib/_http_outgoing.js Outdated

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

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.

Comment threadlib/_http_server.js Outdated

Copy link
Copy Markdown
Member

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?

@fatal10110fatal10110Sep 11, 2021

Copy link
Copy Markdown
ContributorAuthor

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

Comment threadlib/_http_server.js Outdated

Copy link
Copy Markdown
Member

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.

Comment threadlib/_http_server.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
res.shouldKeepAlive=server.maxRequestsPerSocket>state.requestsCount
res.shouldKeepAlive=server.maxRequestsPerSocket>=state.requestsCount

I think this should go to false if they are the same.

Copy link
Copy Markdown
ContributorAuthor

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

@fatal10110

Copy link
Copy Markdown
ContributorAuthor

If you start adding unit tests (even failing) it would help.

Sure, Im on it right now, it just a bit complicated for me to understand how to run them (only my tests)

@mcollina

mcollina commented Sep 11, 2021

Copy link
Copy Markdown
Member

./node test/parallel/test-your-new-test.js

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

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.

@fatal10110

fatal10110 commented Sep 12, 2021

Copy link
Copy Markdown
ContributorAuthor

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
My guess is because the last one (the third one is "marked" as last=true because of shouldKeepAlive=false
not sure what to do now.

This is what I get as a response for 4 requests when limit is set to 3

HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sun, 12 Sep 2021 19:17:24 GMT
Connection: keep-alive
Keep-Alive: timeout=5, max=3
Transfer-Encoding: chunked
c
Hello World!
0
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sun, 12 Sep 2021 19:17:24 GMT
Connection: keep-alive
Keep-Alive: timeout=5, max=3
Transfer-Encoding: chunked
c
Hello World!
0
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sun, 12 Sep 2021 19:17:24 GMT
Connection: close
Transfer-Encoding: chunked
c
Hello World!
0

@fatal10110

fatal10110 commented Sep 12, 2021

Copy link
Copy Markdown
ContributorAuthor

I used another flag instead of shouldKeepAlive and it worked, but there may be side effects if shouldKeepAlive is actually true,
but I send connection: close

results:

HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sun, 12 Sep 2021 19:32:02 GMT
Connection: keep-alive
Keep-Alive: timeout=5, max=3
Transfer-Encoding: chunked
c
Hello World!
0
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sun, 12 Sep 2021 19:32:02 GMT
Connection: keep-alive
Keep-Alive: timeout=5, max=3
Transfer-Encoding: chunked
c
Hello World!
0
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sun, 12 Sep 2021 19:32:02 GMT
Connection: close
Transfer-Encoding: chunked
c
Hello World!
0
HTTP/1.1 503 Service Unavailable
Date: Sun, 12 Sep 2021 19:31:52 GMT
Connection: close
Transfer-Encoding: chunked
0

Comment threadlib/_http_outgoing.js Outdated
@fatal10110
fatal10110 marked this pull request as ready for review September 12, 2021 19:48
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@mcollina

Copy link
Copy Markdown
Member

@fatal10110

Copy link
Copy Markdown
ContributorAuthor

not sure what is "test-asan" and why it failed on timeout

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

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.

@fatal10110

Copy link
Copy Markdown
ContributorAuthor

Done, thanks!

@artur-ma

Copy link
Copy Markdown

@mcollina anything else should be done? or should I close this PR ?

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@artur-ma

Copy link
Copy Markdown

Filed again with some strange error

10:08:27 duration_ms: 21.359
10:08:27 severity: fail
10:08:27 exitcode: 1
10:08:27 stack: |-
10:08:27 /home/iojs/build/workspace/node-test-binary-arm/test/common/debugger.js:84
10:08:27 reject(new Error([
10:08:27 ^
10:08:27 10:08:27 Error: Timeout (10000) while waiting for /(?:assert|break|break on start|debugCommand|exception|other|promiseRejection) in/i; found: < Debugger ending on ws://127.0.0.1:9229/0161d4a8-8f01-4590-b7e9-18412e6e9a13
10:08:27 < For help, see: https://nodejs.org/en/docs/inspector
10:08:27 < 10:08:27 debug> 10:08:27 10:08:27 < Debugger listening on ws://127.0.0.1:9229/f8ab1b5e-afa3-4a18-8975-1d76592e7ade
10:08:27 < For help, see: https://nodejs.org/en/docs/inspector
10:08:27 < 10:08:27 10:08:27 debug> 10:08:27 10:08:27 connecting to 127.0.0.1:9229 ...
10:08:27 ok
10:08:27 10:08:27 debug> 10:08:27 10:08:27 < Debugger attached.
10:08:27 < 10:08:27 debug> 10:08:27 at Timeout.<anonymous> (/home/iojs/build/workspace/node-test-binary-arm/test/common/debugger.js:84:18)
10:08:27 at listOnTimeout (node:internal/timers:557:17)
10:08:27 at processTimers (node:internal/timers:500:7)
10:08:27 10:08:27 Node.js v17.0.0-pre
10:08:27 ...

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@artur-ma

Copy link
Copy Markdown

Should be any semver label here? (minor/major)

@mcollinamcollina added semver-minor PRs that contain new features and should be released in the next minor version. author ready PRs that have at least one approval, no pending requests for changes, and a CI started. and removed needs-ci PRs that need a full CI run. labels Sep 19, 2021

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina

Copy link
Copy Markdown
Member

Landed in 4e8f11d

mcollina pushed a commit that referenced this pull request Sep 19, 2021
Fixes: #40071
PR-URL: #40082
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
BethGriggs pushed a commit that referenced this pull request Sep 21, 2021
Fixes: #40071
PR-URL: #40082
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
BethGriggs added a commit that referenced this pull request Sep 21, 2021
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
BethGriggs pushed a commit that referenced this pull request Sep 21, 2021
Fixes: #40071
PR-URL: #40082
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
BethGriggs added a commit that referenced this pull request Sep 21, 2021
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
@BethGriggsBethGriggs mentioned this pull request Sep 21, 2021
1 task
BethGriggs added a commit that referenced this pull request Sep 22, 2021
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
BethGriggs added a commit that referenced this pull request Sep 22, 2021
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
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.httpIssues or PRs related to the http subsystem.semver-minorPRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Max requests per socket

6 participants

@fatal10110@mcollina@nodejs-github-bot@artur-ma@richardlau@ronag