Skip to content

Commit

Permalink
Multithreaded interval utility
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 4, 2025
1 parent 1785fcf commit c833b02
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 9 deletions.
23 changes: 22 additions & 1 deletion nano/lib/interval.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#pragma once

#include <chrono>
#include <mutex>

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)
Expand All @@ -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 () };
};
}
2 changes: 1 addition & 1 deletion nano/lib/uniquer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class uniquer final

nano::lock_guard<nano::mutex> guard{ mutex };

if (cleanup_interval.elapsed (cleanup_cutoff))
if (cleanup_interval.elapse (cleanup_cutoff))
{
cleanup ();
}
Expand Down
2 changes: 1 addition & 1 deletion nano/node/block_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 (),
Expand Down
2 changes: 1 addition & 1 deletion nano/node/bootstrap/bootstrap_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
2 changes: 1 addition & 1 deletion nano/node/local_block_broadcaster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
Expand Down
3 changes: 1 addition & 2 deletions nano/node/rpc_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ 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;

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 ());
Expand Down
1 change: 1 addition & 0 deletions nano/node/rpc_callbacks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ class http_callbacks
void do_rpc_callback (boost::asio::ip::tcp::resolver::iterator i_a, std::string const &, uint16_t, std::shared_ptr<std::string> const &, std::shared_ptr<std::string> const &, std::shared_ptr<boost::asio::ip::tcp::resolver> const &);

nano::thread_pool workers;
nano::interval_mt warning_interval;
};
}
2 changes: 1 addition & 1 deletion nano/node/transport/tcp_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ asio::awaitable<void> 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);
Expand Down
2 changes: 1 addition & 1 deletion nano/node/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ std::deque<nano::vote_cache::top_entry> nano::vote_cache::top (const nano::uint1
{
nano::lock_guard<nano::mutex> lock{ mutex };

if (cleanup_interval.elapsed (config.age_cutoff / 2))
if (cleanup_interval.elapse (config.age_cutoff / 2))
{
cleanup ();
}
Expand Down

0 comments on commit c833b02

Please sign in to comment.