From 833e342f15f87e9b24357909322d5e57991592c6 Mon Sep 17 00:00:00 2001 From: Paolo Insogna Date: Wed, 17 Apr 2024 11:06:12 +0200 Subject: [PATCH] net: add CLI option for autoSelectFamilyAttemptTimeout PR-URL: https://github.com/nodejs/node/pull/52474 Reviewed-By: Matteo Collina Reviewed-By: Marco Ippolito --- 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 | 10 ++++++++++ ...-autoselectfamily-attempt-timeout-default-value.js | 8 ++++++++ 8 files changed, 42 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..474ffe024cd549 --- /dev/null +++ b/test/parallel/test-net-autoselectfamily-attempt-timeout-cli-option.js @@ -0,0 +1,10 @@ +'use strict'; + +// Flags: --network-family-autoselection-attempt-timeout=123 + +const { platformTimeout } = require('../common'); + +const assert = require('assert'); +const { getDefaultAutoSelectFamilyAttemptTimeout } = require('net'); + +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 new file mode 100644 index 00000000000000..782276952708a0 --- /dev/null +++ b/test/parallel/test-net-autoselectfamily-attempt-timeout-default-value.js @@ -0,0 +1,8 @@ +'use strict'; + +const { platformTimeout } = require('../common'); + +const assert = require('assert'); +const { getDefaultAutoSelectFamilyAttemptTimeout } = require('net'); + +assert.strictEqual(getDefaultAutoSelectFamilyAttemptTimeout(), platformTimeout(2500));