Skip to content
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

lib: add flag to drop connection when running in cluster mode #54927

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,12 +599,25 @@ changes:

* {integer}

Set this property to reject connections when the server's connection count gets
high.
When the number of connections reaches the `server.maxConnections` threshold:

1. If the process is not running in cluster mode, Node.js will close the connection.

2. If the process is running in cluster mode, Node.js will, by default, route the connection to another worker process. To close the connection instead, set \[`server.dropMaxConnection`]\[] to `true`.

It is not recommended to use this option once a socket has been sent to a child
with [`child_process.fork()`][].

### `server.dropMaxConnection`

<!-- YAML
added: REPLACEME
-->

* {boolean}

Set this property to `true` to begin closing connections once the number of connections reaches the \[`server.maxConnections`]\[] threshold. This setting is only effective in cluster mode.

### `server.ref()`

<!-- YAML
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/cluster/child.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,9 @@ function onconnection(message, handle) {

if (accepted && server[owner_symbol]) {
const self = server[owner_symbol];
if (self.maxConnections != null && self._connections >= self.maxConnections) {
if (self.maxConnections != null &&
self._connections >= self.maxConnections &&
!self.dropMaxConnection) {
accepted = false;
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-net-server-drop-connections-in-cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
const common = require('../common');
const cluster = require('cluster');
const http = require('http');

if (cluster.isPrimary) {
cluster.fork();
} else {
const server = http.createServer();
server.maxConnections = 0;
server.dropMaxConnection = true;
// When dropMaxConnection is false, the main process will continue to
// distribute the request to the child process, if true, the child will
// close the connection directly and emit drop event.
server.on('drop', common.mustCall((a) => {
process.exit();
}));
server.listen(common.mustCall(() => {
http.get(`http://localhost:${server.address().port}`).on('error', console.error);
}));
}
Loading