From e5ab3574c87a69308d1f3241a5869688980793db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Sat, 4 Jan 2025 14:26:51 +0100 Subject: [PATCH] Multithreaded interval utility --- nano/lib/interval.hpp | 23 ++++++++++++++++++++++- nano/lib/uniquer.hpp | 2 +- nano/node/block_processor.cpp | 2 +- nano/node/bootstrap/bootstrap_service.cpp | 2 +- nano/node/local_block_broadcaster.cpp | 2 +- nano/node/rpc_callbacks.cpp | 4 ++-- nano/node/transport/tcp_listener.cpp | 2 +- nano/node/vote_cache.cpp | 2 +- 8 files changed, 30 insertions(+), 9 deletions(-) diff --git a/nano/lib/interval.hpp b/nano/lib/interval.hpp index a4b500fa64..776c6ce963 100644 --- a/nano/lib/interval.hpp +++ b/nano/lib/interval.hpp @@ -1,13 +1,14 @@ #pragma once #include +#include namespace nano { class interval { public: - bool elapsed (auto target) + bool elapse (auto target) { auto const now = std::chrono::steady_clock::now (); if (now - last >= target) @@ -21,4 +22,24 @@ class interval private: std::chrono::steady_clock::time_point last{ std::chrono::steady_clock::now () }; }; + +class interval_mt +{ +public: + bool elapse (auto target) + { + std::lock_guard guard{ mutex }; + auto const now = std::chrono::steady_clock::now (); + if (now - last >= target) + { + last = now; + return true; + } + return false; + } + +private: + std::mutex mutex; + std::chrono::steady_clock::time_point last{ std::chrono::steady_clock::now () }; +}; } \ No newline at end of file diff --git a/nano/lib/uniquer.hpp b/nano/lib/uniquer.hpp index 0a3a160e4c..6f3dc32e94 100644 --- a/nano/lib/uniquer.hpp +++ b/nano/lib/uniquer.hpp @@ -27,7 +27,7 @@ class uniquer final nano::lock_guard guard{ mutex }; - if (cleanup_interval.elapsed (cleanup_cutoff)) + if (cleanup_interval.elapse (cleanup_cutoff)) { cleanup (); } diff --git a/nano/node/block_processor.cpp b/nano/node/block_processor.cpp index dd72521c8c..be75b4be34 100644 --- a/nano/node/block_processor.cpp +++ b/nano/node/block_processor.cpp @@ -223,7 +223,7 @@ void nano::block_processor::run () } } - if (log_interval.elapsed (15s)) + if (log_interval.elapse (15s)) { logger.info (nano::log::type::block_processor, "{} blocks (+ {} forced) in processing queue", queue.size (), diff --git a/nano/node/bootstrap/bootstrap_service.cpp b/nano/node/bootstrap/bootstrap_service.cpp index 8f60f9924f..250afead5d 100644 --- a/nano/node/bootstrap/bootstrap_service.cpp +++ b/nano/node/bootstrap/bootstrap_service.cpp @@ -727,7 +727,7 @@ void nano::bootstrap_service::cleanup_and_sync () tags_by_order.pop_front (); } - if (sync_dependencies_interval.elapsed (60s)) + if (sync_dependencies_interval.elapse (60s)) { stats.inc (nano::stat::type::bootstrap, nano::stat::detail::sync_dependencies); accounts.sync_dependencies (); diff --git a/nano/node/local_block_broadcaster.cpp b/nano/node/local_block_broadcaster.cpp index 6248de0042..607fb9d257 100644 --- a/nano/node/local_block_broadcaster.cpp +++ b/nano/node/local_block_broadcaster.cpp @@ -127,7 +127,7 @@ void nano::local_block_broadcaster::run () { stats.inc (nano::stat::type::local_block_broadcaster, nano::stat::detail::loop); - if (cleanup_interval.elapsed (config.cleanup_interval)) + if (cleanup_interval.elapse (config.cleanup_interval)) { cleanup (lock); debug_assert (lock.owns_lock ()); diff --git a/nano/node/rpc_callbacks.cpp b/nano/node/rpc_callbacks.cpp index 9fa05802c5..f9f4d0715e 100644 --- a/nano/node/rpc_callbacks.cpp +++ b/nano/node/rpc_callbacks.cpp @@ -53,9 +53,9 @@ void nano::http_callbacks::setup_callbacks () stats.inc (nano::stat::type::http_callbacks_notified, nano::stat::detail::block_confirmed); constexpr size_t warning_threshold = 10000; - static nano::interval warning_interval; + static nano::interval_mt warning_interval; - if (workers.queued_tasks () > warning_threshold && warning_interval.elapsed (15s)) + if (workers.queued_tasks () > warning_threshold && warning_interval.elapse (15s)) { stats.inc (nano::stat::type::http_callbacks, nano::stat::detail::large_backlog); logger.warn (nano::log::type::http_callbacks, "Backlog of {} http callback notifications to process", workers.queued_tasks ()); diff --git a/nano/node/transport/tcp_listener.cpp b/nano/node/transport/tcp_listener.cpp index 372d9bbbc6..e666b2dd6d 100644 --- a/nano/node/transport/tcp_listener.cpp +++ b/nano/node/transport/tcp_listener.cpp @@ -362,7 +362,7 @@ asio::awaitable nano::transport::tcp_listener::wait_available_slots () con nano::interval log_interval; while (connection_count () >= config.max_inbound_connections && !stopped) { - if (log_interval.elapsed (node.network_params.network.is_dev_network () ? 1s : 15s)) + if (log_interval.elapse (node.network_params.network.is_dev_network () ? 1s : 15s)) { logger.warn (nano::log::type::tcp_listener, "Waiting for available slots to accept new connections (current: {} / max: {})", connection_count (), config.max_inbound_connections); diff --git a/nano/node/vote_cache.cpp b/nano/node/vote_cache.cpp index 89ddd8e650..a2f264f7d7 100644 --- a/nano/node/vote_cache.cpp +++ b/nano/node/vote_cache.cpp @@ -238,7 +238,7 @@ std::deque nano::vote_cache::top (const nano::uint1 { nano::lock_guard lock{ mutex }; - if (cleanup_interval.elapsed (config.age_cutoff / 2)) + if (cleanup_interval.elapse (config.age_cutoff / 2)) { cleanup (); }