-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
feat: Use new Node.js' connection closing API if available #3925
Conversation
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.
After seeing this... I would prefer a slightly different approach.
- Add a
closeConnectionsOnClose
option at the factory or to theclose()
method. Three options:false
,'idle'
,'all'
. - The default value should
'idle'
if it is available. - throw if
closeConnectionOnExit
is set to a value that is not available on the current Node.js runtime.
The new behavior is applied automatically to fastify.close()
and no new methods are added.
Ok, makes sense. I'll rework it soon. |
@mcollina As agreed, I've simplified this PR a lot.
Hope this helps! |
Can you update the PR title? |
build/build-validation.js
Outdated
@@ -24,7 24,7 @@ module.exports.defaultInitOptions = ${JSON.stringify(defaultInitOptions)} | |||
const defaultInitOptions = { | |||
connectionTimeout: 0, // 0 sec | |||
keepAliveTimeout: 72000, // 72 seconds | |||
forceCloseConnections: false, // keep-alive connections | |||
forceCloseConnections: true, // keep-alive connections |
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 dangerous. There are ramifications that the user must know and account for. I disagree with enabling this by 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.
I can easily revert this. But enabling this will IMHO make close behave a bit more like someone might expect.
Which ramification are you talking about? I can definitely update the docs if needed.
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.
What ramifications? Those sockets are idle, there is no upstanding requests (minus the in-flight that we have not received yet).
On the other hand, making this the default improves the development experience and matches what people expects.
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 vote for not changing the default
.
Add new option for closing behavior and default as idle
, and when forceCloseConnections
is true
. It will be equal to all
(which means call closeAllConnections
).
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.
Note that forceCloseConnections
is not equal to all
right now, only idle
.
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.
@mcollina Actually, after checking the source code, I realized that fastify behavior is more similar to http.Server.closeAllConnections
.
Despite of the internal variable naming of keepAliveConnections
, the .close
will destroy ALL connected clients, not idle one only.
Shall I changed the method used?
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.
Yes, the current implementation is actually .closeAllConnections
. It will destory all connections no matter the state.
The value from forceCloseConnections
should be translated as follow to match the current behavior.
true
should translate toall
false
should translate toidle
orfalse
. I preferidle
here because it do not harms anyone if it close the idle connections.
Adding an extra option to explicitly disable the auto closing connections feature (in separate PR).
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 just updated the PR. I think that false
should disable connection closing at all. The next PR might instead add idle
for the other behavior.
Reason is fastify is not capable at the moment of closing idle connections only.
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 in this pr, forceCloseConnections
is equal to all
, I think it's better to use false
by 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.
I agree.
I've update it and the description as well. |
conn.destroy() | ||
fastify[kKeepAliveConnections].delete(conn) | ||
if (serverSupportsCloseAllConnections && options.forceCloseConnections) { | ||
instance.server.closeAllConnections() |
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.
instance.server.closeAllConnections() | |
instance.server.closeIdleConnections() |
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.
Well, here's the thing. If we just want to translate the existing behavior to the Node.js core provided one, then it's equivalent to closeAllConnections
.
If instead we want to change the semantics, then yes, I will apply your suggestion.
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.
ah gosh. Let's not flip the default and add an option to close only the idle connections.
Can you rebase and regenerate the configValidator? |
@mcollina Done |
(sorry, my finger slipped) |
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.
What I would like to achieve is that by default, if closeIdleConnections
is available, it's used to shorten down the wait time.
build/build-validation.js
Outdated
@@ -24,7 24,7 @@ module.exports.defaultInitOptions = ${JSON.stringify(defaultInitOptions)} | |||
const defaultInitOptions = { | |||
connectionTimeout: 0, // 0 sec | |||
keepAliveTimeout: 72000, // 72 seconds | |||
forceCloseConnections: false, // keep-alive connections | |||
forceCloseConnections: true, // keep-alive connections |
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 agree.
I have updated the PR completely, see the new description above. How does it look now? @mcollina I haven't changed the default as new APIs are not available in Node 16. We might flip this later once 18 reaches LTS. |
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've force pushed the branch again. PTAL! |
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
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
This PR aims to leverage the new Node.js 18.2.0 APIs if available and changes the
forceCloseConnection
option as follows:forceCloseConnection=false
- No connection handling is doneforceCloseConnection=true
- UsecloseAllConnections
if available, otherwise the fastify connection handling.forceCloseConnection="idle"
- UsecloseIdleConnections
if available, otherwise throws an error.The option defaults to
"idle"
if the server supportscloseIdleConnections
, otherwise tofalse
.Fixes #3924.