-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
http: added scheduling option to http agent #33278
Conversation
In some cases, it is preferable to use a lifo scheduling strategy for the free sockets instead of default one, which is fifo. This commit introduces a scheduling option to add the ability to choose which strategy best fits your needs.
Please update docs |
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.
Adding a "request changes" only because it's missing docs.
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
doc/api/http.md
Outdated
@@ -132,6 +137,9 @@ added: v0.3.4 | |||
* `maxFreeSockets` {number} Maximum number of sockets to leave open | |||
in a free state. Only relevant if `keepAlive` is set to `true`. | |||
**Default:** `256`. | |||
* `scheduling` {string} Scheduling strategy to apply when picking | |||
the next free socket to use. It can be `'fifo'` or `'lifo'`. | |||
**Default:** `'fifo'` |
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.
Non-blocking: Would be ideal to have fifo
and lifo
briefly explained as not all readers may be familiar with the terms. Might also be good to briefly explain why one would stray from the default.
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.
Thank you for the suggestion! Let me know what you think :)
maxSockets: 5 | ||
}); | ||
|
||
bulkRequest(url, agent, (ports) => { |
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.
Might be a bit nicer to convert these into async functions with await syntax and to wrap this stack into a helper function so it can be reused in both tests.
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.
Thank you for the suggestion! Unfortunately, I don't have time for refactoring this test at the moment, is it ok if we leave it as is?
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.
@jasnell is not clear if you have an hard block on the tests being converted or not. Can we land this anyway?
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.
No hard block!
Added scheduling http agent option.
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
I think this can land
Landed in daa65fb |
In some cases, it is preferable to use a lifo scheduling strategy for the free sockets instead of default one, which is fifo. This commit introduces a scheduling option to add the ability to choose which strategy best fits your needs. PR-URL: #33278 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
In some cases, it is preferable to use a lifo scheduling strategy for the free sockets instead of default one, which is fifo. This commit introduces a scheduling option to add the ability to choose which strategy best fits your needs. PR-URL: #33278 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
In some cases, it is preferable to use a lifo scheduling strategy for the free sockets instead of default one, which is fifo. This commit introduces a scheduling option to add the ability to choose which strategy best fits your needs. PR-URL: nodejs/node#33278 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
Hello everyone!
This pr introduces a new configuration option in the HTTP agent, named
scheduling
.By default, the agent iterates over the free sockets in a FIFO fashion, the newly added
scheduling
option allows using a LIFO instead.If nothing is configured, the behavior will be the same as before, so this change should not be considered as breaking.
Why this is useful?
I've encountered a case where the application was sending requests at a high rate (around 200 req/sec), with low response time. Every now and then, the response time increased massively for a few seconds and the agent started opening as many sockets as it could, later, the response time was low again, and only a few sockets were used at a given time.
The issue is that the agent will not close the
freeSockets
, because since the default scheduling sequence is FIFO, every socket will be reused before it reaches its timeout.This situation was happening in a system with multiple instances of Node, and the receiver of the request was overwhelmed by the high number of open and never closed sockets.
By switching to a LIFO scheduling, I saw that the spikes were handled as expected, but as soon as the response time was low again, the
freeSockets
start decreasing, and only the minimum amount of sockets was used.Given that both scheduling strategies have pros and cons, I've decided to expose them as an option, so the user can have more control based on their needs.
How does it work?
The user only need to configure the
scheduling
option, onlyfifo
andlifo
are accepted.I didn't update the documentation yet, I'll do that once we settle on the feature and the implementation :)
If possible, I would love to see this change landing both in Node.js v12 and v14.
Related: #31526
cc @mcollina @ronag
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes