diff --git a/nano/core_test/entry.cpp b/nano/core_test/entry.cpp index 423f3731b8..9d08284dd0 100644 --- a/nano/core_test/entry.cpp +++ b/nano/core_test/entry.cpp @@ -6,8 +6,6 @@ #include -constexpr std::size_t OPEN_FILE_DESCRIPTORS_LIMIT = 16384; - namespace nano { namespace test @@ -19,8 +17,8 @@ void force_nano_dev_network (); GTEST_API_ int main (int argc, char ** argv) { + nano::initialize_file_descriptor_limit (); nano::logger::initialize_for_tests (nano::log_config::tests_default ()); - nano::set_file_descriptor_limit (OPEN_FILE_DESCRIPTORS_LIMIT); nano::force_nano_dev_network (); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; testing::InitGoogleTest (&argc, argv); diff --git a/nano/lib/utility.cpp b/nano/lib/utility.cpp index f1a0ca908e..64d51efaca 100644 --- a/nano/lib/utility.cpp +++ b/nano/lib/utility.cpp @@ -53,6 +53,16 @@ void nano::set_file_descriptor_limit (std::size_t limit) #endif } +void nano::initialize_file_descriptor_limit () +{ + nano::set_file_descriptor_limit (DEFAULT_FILE_DESCRIPTOR_LIMIT); + auto limit = nano::get_file_descriptor_limit (); + if (limit < DEFAULT_FILE_DESCRIPTOR_LIMIT) + { + std::cerr << "WARNING: Current file descriptor limit of " << limit << " is lower than the " << DEFAULT_FILE_DESCRIPTOR_LIMIT << " recommended. Node was unable to change it." << std::endl; + } +} + nano::container_info_composite::container_info_composite (std::string const & name) : name (name) { diff --git a/nano/lib/utility.hpp b/nano/lib/utility.hpp index 35d19892dd..ecbc35ee13 100644 --- a/nano/lib/utility.hpp +++ b/nano/lib/utility.hpp @@ -134,6 +134,11 @@ void create_load_memory_address_files (); */ std::size_t get_file_descriptor_limit (); void set_file_descriptor_limit (std::size_t limit); +/** + * This should be called from entry points. It sets the file descriptor limit to the maximum allowed and logs any errors. + */ +constexpr std::size_t DEFAULT_FILE_DESCRIPTOR_LIMIT = 16384; +void initialize_file_descriptor_limit (); void remove_all_files_in_dir (std::filesystem::path const & dir); void move_all_files_to_dir (std::filesystem::path const & from, std::filesystem::path const & to); diff --git a/nano/nano_node/entry.cpp b/nano/nano_node/entry.cpp index 994e893eba..c86228a04b 100644 --- a/nano/nano_node/entry.cpp +++ b/nano/nano_node/entry.cpp @@ -56,22 +56,14 @@ class address_library_pair bool operator== (const address_library_pair & other) const; }; -constexpr std::size_t OPEN_FILE_DESCRIPTORS_LIMIT = 16384; } int main (int argc, char * const * argv) { nano::set_umask (); // Make sure the process umask is set before any files are created + nano::initialize_file_descriptor_limit (); nano::logger::initialize (nano::log_config::cli_default ()); - // Increase file descriptor limit - nano::set_file_descriptor_limit (OPEN_FILE_DESCRIPTORS_LIMIT); - auto const file_descriptor_limit = nano::get_file_descriptor_limit (); - if (file_descriptor_limit < OPEN_FILE_DESCRIPTORS_LIMIT) - { - std::cerr << "WARNING: Current file descriptor limit of " << file_descriptor_limit << " is lower than the " << OPEN_FILE_DESCRIPTORS_LIMIT << " recommended. Node was unable to change it." << std::endl; - } - nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; boost::program_options::options_description description ("Command line options"); diff --git a/nano/nano_rpc/entry.cpp b/nano/nano_rpc/entry.cpp index 11cea70432..c54b335869 100644 --- a/nano/nano_rpc/entry.cpp +++ b/nano/nano_rpc/entry.cpp @@ -84,6 +84,7 @@ void run (std::filesystem::path const & data_path, std::vector cons int main (int argc, char * const * argv) { nano::set_umask (); // Make sure the process umask is set before any files are created + nano::initialize_file_descriptor_limit (); nano::logger::initialize (nano::log_config::cli_default ()); boost::program_options::options_description description ("Command line options"); diff --git a/nano/nano_wallet/entry.cpp b/nano/nano_wallet/entry.cpp index 65be63a226..25b29947fb 100644 --- a/nano/nano_wallet/entry.cpp +++ b/nano/nano_wallet/entry.cpp @@ -237,6 +237,7 @@ int run_wallet (QApplication & application, int argc, char * const * argv, std:: int main (int argc, char * const * argv) { nano::set_umask (); // Make sure the process umask is set before any files are created + nano::initialize_file_descriptor_limit (); nano::logger::initialize (nano::log_config::cli_default ()); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; diff --git a/nano/qt_test/entry.cpp b/nano/qt_test/entry.cpp index e8976178c4..b3ce57fd8f 100644 --- a/nano/qt_test/entry.cpp +++ b/nano/qt_test/entry.cpp @@ -17,6 +17,7 @@ void force_nano_dev_network (); int main (int argc, char ** argv) { + nano::initialize_file_descriptor_limit (); nano::logger::initialize_for_tests (nano::log_config::tests_default ()); nano::force_nano_dev_network (); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard; diff --git a/nano/rpc_test/entry.cpp b/nano/rpc_test/entry.cpp index 2a94b7d9e7..806f07645b 100644 --- a/nano/rpc_test/entry.cpp +++ b/nano/rpc_test/entry.cpp @@ -15,6 +15,7 @@ void force_nano_dev_network (); int main (int argc, char ** argv) { + nano::initialize_file_descriptor_limit (); nano::logger::initialize_for_tests (nano::log_config::tests_default ()); nano::force_nano_dev_network (); nano::set_use_memory_pools (false); diff --git a/nano/slow_test/entry.cpp b/nano/slow_test/entry.cpp index 0b9a0ca758..096c7dc6a9 100644 --- a/nano/slow_test/entry.cpp +++ b/nano/slow_test/entry.cpp @@ -14,6 +14,7 @@ void force_nano_dev_network (); int main (int argc, char ** argv) { + nano::initialize_file_descriptor_limit (); nano::logger::initialize_for_tests (nano::log_config::tests_default ()); nano::force_nano_dev_network (); nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard;