From dbbaf167389fa79b46ec38a9775a4dc74d033117 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Thu, 11 Apr 2024 16:31:03 +0200 Subject: [PATCH 1/2] net: add CLI option for autoSelectFamilyAttemptTimeout --- doc/api/cli.md | 11 +++++++++++ doc/api/net.md | 6 ++++-- lib/net.js | 2 +- src/node_options.cc | 5 +++++ src/node_options.h | 1 + test/common/index.js | 8 ++------ ...net-autoselectfamily-attempt-timeout-cli-option.js | 11 +++++++++++ ...-autoselectfamily-attempt-timeout-default-value.js | 9 +++++++++ 8 files changed, 44 insertions(+), 9 deletions(-) create mode 100644 test/parallel/test-net-autoselectfamily-attempt-timeout-cli-option.js create mode 100644 test/parallel/test-net-autoselectfamily-attempt-timeout-default-value.js diff --git a/doc/api/cli.md b/doc/api/cli.md index f08afb737d41b4..97070594f195fd 100644 --- a/doc/api/cli.md +++ b/doc/api/cli.md @@ -1329,6 +1329,15 @@ added: v7.10.0 This option is a no-op. It is kept for compatibility. +### `--network-family-autoselection-attempt-timeout` + + + +Sets the default value for the network family autoselection attempt timeout. +For more information, see [`net.getDefaultAutoSelectFamilyAttemptTimeout()`][]. + ### `--no-addons` Gets the current default value of the `autoSelectFamilyAttemptTimeout` option of [`socket.connect(options)`][]. -The initial default value is `250`. +The initial default value is `250` or the value specified via the command line +option `--network-family-autoselection-attempt-timeout`. * Returns: {number} The current default value of the `autoSelectFamilyAttemptTimeout` option. @@ -1763,7 +1764,8 @@ added: Sets the default value of the `autoSelectFamilyAttemptTimeout` option of [`socket.connect(options)`][]. * `value` {number} The new default value, which must be a positive number. If the number is less than `10`, - the value `10` is used instead. The initial default value is `250`. + the value `10` is used instead. The initial default value is `250` or the value specified via the command line + option `--network-family-autoselection-attempt-timeout`. ## `net.isIP(input)` diff --git a/lib/net.js b/lib/net.js index 0e8d278bdf9c7b..d5ef6827a42bdc 100644 --- a/lib/net.js +++ b/lib/net.js @@ -134,7 +134,7 @@ let dns; let BlockList; let SocketAddress; let autoSelectFamilyDefault = getOptionValue('--network-family-autoselection'); -let autoSelectFamilyAttemptTimeoutDefault = 250; +let autoSelectFamilyAttemptTimeoutDefault = getOptionValue('--network-family-autoselection-attempt-timeout'); const { clearTimeout, setTimeout } = require('timers'); const { kTimeout } = require('internal/timers'); diff --git a/src/node_options.cc b/src/node_options.cc index 398d4f428d018c..807ba1fa19488a 100644 --- a/src/node_options.cc +++ b/src/node_options.cc @@ -383,6 +383,11 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() { &EnvironmentOptions::network_family_autoselection, kAllowedInEnvvar, true); + AddOption("--network-family-autoselection-attempt-timeout", + "Sets the default value for the network family autoselection " + "attempt timeout.", + &EnvironmentOptions::network_family_autoselection_attempt_timeout, + kAllowedInEnvvar); AddAlias("--enable-network-family-autoselection", "--network-family-autoselection"); AddOption("--enable-source-maps", diff --git a/src/node_options.h b/src/node_options.h index ba171b4d14ac4c..94b99806c1778f 100644 --- a/src/node_options.h +++ b/src/node_options.h @@ -136,6 +136,7 @@ class EnvironmentOptions : public Options { int64_t heap_snapshot_near_heap_limit = 0; std::string heap_snapshot_signal; bool network_family_autoselection = true; + uint64_t network_family_autoselection_attempt_timeout = 250; uint64_t max_http_header_size = 16 * 1024; bool deprecation = true; bool force_async_hooks_checks = true; diff --git a/test/common/index.js b/test/common/index.js index 0ed7733c2ff09c..d2c68c0fafb01b 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -146,12 +146,8 @@ const isPi = (() => { const isDumbTerminal = process.env.TERM === 'dumb'; // When using high concurrency or in the CI we need much more time for each connection attempt -const defaultAutoSelectFamilyAttemptTimeout = platformTimeout(2500); -// Since this is also used by tools outside of the test suite, -// make sure setDefaultAutoSelectFamilyAttemptTimeout -if (typeof net.setDefaultAutoSelectFamilyAttemptTimeout === 'function') { - net.setDefaultAutoSelectFamilyAttemptTimeout(platformTimeout(defaultAutoSelectFamilyAttemptTimeout)); -} +net.setDefaultAutoSelectFamilyAttemptTimeout(platformTimeout(net.getDefaultAutoSelectFamilyAttemptTimeout() * 10)); +const defaultAutoSelectFamilyAttemptTimeout = net.getDefaultAutoSelectFamilyAttemptTimeout(); const buildType = process.config.target_defaults ? process.config.target_defaults.default_configuration : diff --git a/test/parallel/test-net-autoselectfamily-attempt-timeout-cli-option.js b/test/parallel/test-net-autoselectfamily-attempt-timeout-cli-option.js new file mode 100644 index 00000000000000..d242ebcf8a877b --- /dev/null +++ b/test/parallel/test-net-autoselectfamily-attempt-timeout-cli-option.js @@ -0,0 +1,11 @@ +'use strict'; + +// Flags: --network-family-autoselection-attempt-timeout=123 + +require('../common'); + +const assert = require('assert'); +const { getDefaultAutoSelectFamilyAttemptTimeout } = require('net'); + +// Note that in test/common/index the default value is multiplied by 10. +assert.strictEqual(getDefaultAutoSelectFamilyAttemptTimeout(), 1230); diff --git a/test/parallel/test-net-autoselectfamily-attempt-timeout-default-value.js b/test/parallel/test-net-autoselectfamily-attempt-timeout-default-value.js new file mode 100644 index 00000000000000..c7ddb86f181de1 --- /dev/null +++ b/test/parallel/test-net-autoselectfamily-attempt-timeout-default-value.js @@ -0,0 +1,9 @@ +'use strict'; + +require('../common'); + +const assert = require('assert'); +const { getDefaultAutoSelectFamilyAttemptTimeout } = require('net'); + +// Note that in test/common/index the default value is multiplied by 10. +assert.strictEqual(getDefaultAutoSelectFamilyAttemptTimeout(), 2500); From ecd78bbe718a207c5c76f457e827a5d3c181b01c Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Tue, 16 Apr 2024 11:16:43 +0200 Subject: [PATCH 2/2] net: fixed tests --- .../test-net-autoselectfamily-attempt-timeout-cli-option.js | 5 ++--- ...est-net-autoselectfamily-attempt-timeout-default-value.js | 5 ++--- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/test/parallel/test-net-autoselectfamily-attempt-timeout-cli-option.js b/test/parallel/test-net-autoselectfamily-attempt-timeout-cli-option.js index d242ebcf8a877b..474ffe024cd549 100644 --- a/test/parallel/test-net-autoselectfamily-attempt-timeout-cli-option.js +++ b/test/parallel/test-net-autoselectfamily-attempt-timeout-cli-option.js @@ -2,10 +2,9 @@ // Flags: --network-family-autoselection-attempt-timeout=123 -require('../common'); +const { platformTimeout } = require('../common'); const assert = require('assert'); const { getDefaultAutoSelectFamilyAttemptTimeout } = require('net'); -// Note that in test/common/index the default value is multiplied by 10. -assert.strictEqual(getDefaultAutoSelectFamilyAttemptTimeout(), 1230); +assert.strictEqual(getDefaultAutoSelectFamilyAttemptTimeout(), platformTimeout(123 * 10)); diff --git a/test/parallel/test-net-autoselectfamily-attempt-timeout-default-value.js b/test/parallel/test-net-autoselectfamily-attempt-timeout-default-value.js index c7ddb86f181de1..782276952708a0 100644 --- a/test/parallel/test-net-autoselectfamily-attempt-timeout-default-value.js +++ b/test/parallel/test-net-autoselectfamily-attempt-timeout-default-value.js @@ -1,9 +1,8 @@ 'use strict'; -require('../common'); +const { platformTimeout } = require('../common'); const assert = require('assert'); const { getDefaultAutoSelectFamilyAttemptTimeout } = require('net'); -// Note that in test/common/index the default value is multiplied by 10. -assert.strictEqual(getDefaultAutoSelectFamilyAttemptTimeout(), 2500); +assert.strictEqual(getDefaultAutoSelectFamilyAttemptTimeout(), platformTimeout(2500));