Skip to content

Commit

Permalink
net: enable autoSelectFamily by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Feb 23, 2023
1 parent 9e1824d commit 18928f3
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 37 deletions.
7 changes: 4 additions & 3 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,14 @@ added: v6.0.0
Enable FIPS-compliant crypto at startup. (Requires Node.js to be built
against FIPS-compatible OpenSSL.)

### `--enable-network-family-autoselection`
### `--no-network-family-autoselection`

<!-- YAML
added: v19.4.0
-->

Enables the family autoselection algorithm unless connection options explicitly
disables it.
Disables the family autoselection algorithm unless connection options explicitly
enables it.

### `--enable-source-maps`

Expand Down Expand Up @@ -1929,6 +1929,7 @@ Node.js options that are allowed are:
* `--no-extra-info-on-fatal-exception`
* `--no-force-async-hooks-checks`
* `--no-global-search-paths`
* `--no-network-family-autoselection`
* `--no-warnings`
* `--node-memory-debug`
* `--openssl-config`
Expand Down
17 changes: 12 additions & 5 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,12 @@ behavior.
<!-- YAML
added: v0.1.90
changes:
- version: REPLACEME
pr-url: REPLACEME
description: The default value for the autoSelectFamily option is now true.
The `--enable-network-family-autoselection` has been renamed
to `--network-family-autoselection`. The old name is now an
alias but it is discouraged.
- version: v19.4.0
pr-url: https://github.com/nodejs/node/pull/45777
description: The default value for autoSelectFamily option can be changed
Expand Down Expand Up @@ -936,12 +942,12 @@ For TCP connections, available `options` are:
option before timing out and trying the next address.
Ignored if the `family` option is not `0` or if `localAddress` is set.
Connection errors are not emitted if at least one connection succeeds.
**Default:** initially `false`, but it can be changed at runtime using [`net.setDefaultAutoSelectFamily(value)`][]
or via the command line option `--enable-network-family-autoselection`.
If all connections attempts fails a single `AggregateError` with all failed attempts is emitted.
**Default:** \[`net.getDefaultAutoSelectFamily()`]\[]
* `autoSelectFamilyAttemptTimeout` {number}: The amount of time in milliseconds to wait
for a connection attempt to finish before trying the next address when using the `autoSelectFamily` option.
If set to a positive integer less than `10`, then the value `10` will be used instead.
**Default:** initially `250`, but it can be changed at runtime using [`net.setDefaultAutoSelectFamilyAttemptTimeout(value)`][]
**Default:** \[`net.getDefaultAutoSelectFamilyAttemptTimeout()`]\[]

For [IPC][] connections, available `options` are:

Expand Down Expand Up @@ -1629,6 +1635,8 @@ added: v19.4.0
-->

Gets the current default value of the `autoSelectFamily` option of [`socket.connect(options)`][].
The initial default value is `true`, unless the command line option
`--no-network-family-autoselection` is provided.

* Returns: {boolean} The current default value of the `autoSelectFamily` option.

Expand All @@ -1649,6 +1657,7 @@ added: REPLACEME
-->

Gets the current default value of the `autoSelectFamilyAttemptTimeout` option of [`socket.connect(options)`][].
The initial default value is `250`.

* Returns: {number} The current default value of the `autoSelectFamilyAttemptTimeout` option.

Expand Down Expand Up @@ -1747,8 +1756,6 @@ net.isIPv6('fhqwhgads'); // returns false
[`net.createConnection(path)`]: #netcreateconnectionpath-connectlistener
[`net.createConnection(port, host)`]: #netcreateconnectionport-host-connectlistener
[`net.createServer()`]: #netcreateserveroptions-connectionlistener
[`net.setDefaultAutoSelectFamily(value)`]: #netsetdefaultautoselectfamilyvalue
[`net.setDefaultAutoSelectFamilyAttemptTimeout(value)`]: #netsetdefaultautoselectfamilyattempttimeoutvalue
[`new net.Socket(options)`]: #new-netsocketoptions
[`readable.setEncoding()`]: stream.md#readablesetencodingencoding
[`server.close()`]: #serverclosecallback
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function makeSyncWrite(fd) {
}

module.exports = {
kReinitializeHandle: Symbol('reinitializeHandle'),
kReinitializeHandle: Symbol('kReinitializeHandle'),
isIP,
isIPv4,
isIPv6,
Expand Down
2 changes: 1 addition & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ let cluster;
let dns;
let BlockList;
let SocketAddress;
let autoSelectFamilyDefault = getOptionValue('--enable-network-family-autoselection');
let autoSelectFamilyDefault = getOptionValue('--network-family-autoselection');
let autoSelectFamilyAttemptTimeoutDefault = 250;

const { clearTimeout, setTimeout } = require('timers');
Expand Down
10 changes: 6 additions & 4 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -358,10 +358,12 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
"returned)",
&EnvironmentOptions::dns_result_order,
kAllowedInEnvvar);
AddOption("--enable-network-family-autoselection",
"Enable network address family autodetection algorithm",
&EnvironmentOptions::enable_network_family_autoselection,
kAllowedInEnvvar);
AddOption("--network-family-autoselection",
"Disable network address family autodetection algorithm",
&EnvironmentOptions::network_family_autoselection,
kAllowedInEnvvar,
true);
AddAlias("--enable-network-family-autoselection", "--network-family-autoselection");
AddOption("--enable-source-maps",
"Source Map V3 support for stack traces",
&EnvironmentOptions::enable_source_maps,
Expand Down
2 changes: 1 addition & 1 deletion src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class EnvironmentOptions : public Options {
bool frozen_intrinsics = false;
int64_t heap_snapshot_near_heap_limit = 0;
std::string heap_snapshot_signal;
bool enable_network_family_autoselection = false;
bool network_family_autoselection = true;
uint64_t max_http_header_size = 16 * 1024;
bool deprecation = true;
bool force_async_hooks_checks = true;
Expand Down
29 changes: 13 additions & 16 deletions test/parallel/test-net-autoselectfamily-commandline-option.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

// Flags: --enable-network-family-autoselection
// Flags: --no-network-family-autoselection

const common = require('../common');
const { parseDNSPacket, writeDNSPacket } = require('../common/dns');
Expand Down Expand Up @@ -62,7 +62,7 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) {
});
}

// Test that IPV4 is reached if IPV6 is not reachable
// Test that IPV4 is NOT reached if IPV6 is not reachable and the option has been disabled via command line
{
createDnsServer('::1', '127.0.0.1', common.mustCall(function({ dnsServer, lookup }) {
const ipv4Server = createServer((socket) => {
Expand All @@ -77,28 +77,25 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) {

const connection = createConnection({
host: 'example.org',
port: port,
port,
lookup,
});

let response = '';
connection.setEncoding('utf-8');
connection.on('ready', common.mustNotCall());
connection.on('error', common.mustCall((error) => {
assert.strictEqual(connection.autoSelectFamilyAttemptedAddresses, undefined);

connection.on('ready', common.mustCall(() => {
assert.deepStrictEqual(connection.autoSelectFamilyAttemptedAddresses, [`::1:${port}`, `127.0.0.1:${port}`]);
}));

connection.on('data', (chunk) => {
response += chunk;
});
if (common.hasIPv6) {
assert.strictEqual(error.code, 'ECONNREFUSED');
assert.strictEqual(error.message, `connect ECONNREFUSED ::1:${port}`);
} else {
assert.strictEqual(error.code, 'EADDRNOTAVAIL');
assert.strictEqual(error.message, `connect EADDRNOTAVAIL ::1:${port} - Local (:::0)`);
}

connection.on('end', common.mustCall(() => {
assert.strictEqual(response, 'response-ipv4');
ipv4Server.close();
dnsServer.close();
}));

connection.write('request');
}));
}));
}
2 changes: 1 addition & 1 deletion test/parallel/test-tls-connect-hints-option.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ tls.connect({
port: 42,
lookup: common.mustCall((host, options) => {
assert.strictEqual(host, 'localhost');
assert.deepStrictEqual(options, { family: undefined, hints });
assert.deepStrictEqual(options, { family: undefined, hints, all: true });
}),
hints
});
2 changes: 1 addition & 1 deletion test/sequential/test-http-econnrefused.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function ping() {
console.log(`Error making ping req: ${error}`);
hadError = true;
assert.ok(!gotEnd);
afterPing(error.message);
afterPing(error.errors[0].message);
});
}

Expand Down
9 changes: 5 additions & 4 deletions test/sequential/test-net-better-error-messages-port.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const c = net.createConnection(common.PORT);

c.on('connect', common.mustNotCall());

c.on('error', common.mustCall(function(e) {
assert.strictEqual(e.code, 'ECONNREFUSED');
assert.strictEqual(e.port, common.PORT);
assert.match(e.address, /^(127\.0\.0\.1|::1)$/);
c.on('error', common.mustCall(function(error) {
const failedAttempt = error.errors[0];
assert.strictEqual(failedAttempt.code, 'ECONNREFUSED');
assert.strictEqual(failedAttempt.port, common.PORT);
assert.match(failedAttempt.address, /^(127\.0\.0\.1|::1)$/);
}));

0 comments on commit 18928f3

Please sign in to comment.