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 9, 2023
1 parent 5092346 commit 8fa810f
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 36 deletions.
8 changes: 4 additions & 4 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 @@ -1888,7 +1888,6 @@ Node.js options that are allowed are:
* `--disable-proto`
* `--dns-result-order`
* `--enable-fips`
* `--enable-network-family-autoselection`
* `--enable-source-maps`
* `--experimental-abortcontroller`
* `--experimental-import-meta-resolve`
Expand Down Expand Up @@ -1929,6 +1928,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
9 changes: 7 additions & 2 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,11 @@ behavior.
<!-- YAML
added: v0.1.90
changes:
- version: REPLACEME
pr-url: REPLACEME
description: The default value for autoSelectFamily option is now true.
The `--enable-network-family-autoselection` has been removed
in favor of `--network-family-autoselection`.
- 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,8 +941,8 @@ 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`.
**Default:** initially `true`, but it can be changed at runtime using [`net.setDefaultAutoSelectFamily(value)`][]
or via the command line option `--no-network-family-autoselection`.
* `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.
Expand Down
2 changes: 1 addition & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ let cluster;
let dns;
let BlockList;
let SocketAddress;
let autoSelectFamilyDefault = getOptionValue('--enable-network-family-autoselection');
let autoSelectFamilyDefault = getOptionValue('--network-family-autoselection');

const { clearTimeout, setTimeout } = require('timers');
const { kTimeout } = require('internal/timers');
Expand Down
9 changes: 5 additions & 4 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,11 @@ 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);
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
38 changes: 14 additions & 24 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 All @@ -10,13 +10,7 @@ const dgram = require('dgram');
const { Resolver } = require('dns');
const { createConnection, createServer } = require('net');

// Test that happy eyeballs algorithm can be enable from command line.

let autoSelectFamilyAttemptTimeout = common.platformTimeout(250);
if (common.isWindows) {
// Some of the windows machines in the CI need more time to establish connection
autoSelectFamilyAttemptTimeout = common.platformTimeout(1500);
}
// Test that happy eyeballs algorithm can be disabled from command line.

function _lookup(resolver, hostname, options, cb) {
resolver.resolve(hostname, 'ANY', (err, replies) => {
Expand Down Expand Up @@ -65,7 +59,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 @@ -80,29 +74,25 @@ function createDnsServer(ipv6Addr, ipv4Addr, cb) {

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

let response = '';
connection.setEncoding('utf-8');

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

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');
}));
}));
}

0 comments on commit 8fa810f

Please sign in to comment.