Skip to content

Commit

Permalink
Use dedicated thread pool
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Jan 4, 2025
1 parent 5e47b80 commit 1b98927
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions nano/lib/thread_roles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ std::string nano::thread_role::get_string (nano::thread_role::name role)
case nano::thread_role::name::monitor:
thread_role_name_string = "Monitor";
break;
case nano::thread_role::name::http_callbacks:
thread_role_name_string = "HTTP callbacks";
break;
default:
debug_assert (false && "nano::thread_role::get_string unhandled thread role");
}
Expand Down
1 change: 1 addition & 0 deletions nano/lib/thread_roles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ enum class name
vote_router,
online_reps,
monitor,
http_callbacks,
};

std::string_view to_string (name);
Expand Down
3 changes: 3 additions & 0 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ void nano::node::start ()
vote_router.start ();
online_reps.start ();
monitor.start ();
http_callbacks.start ();

add_initial_peers ();
}
Expand Down Expand Up @@ -605,6 +606,7 @@ void nano::node::stop ()
message_processor.stop ();
network.stop ();
monitor.stop ();
http_callbacks.stop ();

bootstrap_workers.stop ();
wallet_workers.stop ();
Expand Down Expand Up @@ -1075,6 +1077,7 @@ nano::container_info nano::node::container_info () const
info.add ("bandwidth", outbound_limiter.container_info ());
info.add ("backlog_scan", backlog_scan.container_info ());
info.add ("bounded_backlog", backlog.container_info ());
info.add ("http_callbacks", http_callbacks.container_info ());
return info;
}

Expand Down
22 changes: 19 additions & 3 deletions nano/node/rpc_callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ nano::http_callbacks::http_callbacks (nano::node & node_a) :
observers{ node_a.observers },
ledger{ node_a.ledger },
logger{ node_a.logger },
stats{ node_a.stats }
stats{ node_a.stats },
workers{ 1, nano::thread_role::name::http_callbacks }
{
// Only set up callbacks if a callback address is configured
if (!config.callback_address.empty ())
Expand All @@ -19,6 +20,21 @@ nano::http_callbacks::http_callbacks (nano::node & node_a) :
}
}

void nano::http_callbacks::start ()
{
workers.start ();
}

void nano::http_callbacks::stop ()
{
workers.stop ();
}

nano::container_info nano::http_callbacks::container_info () const
{
return workers.container_info ();
}

void nano::http_callbacks::setup_callbacks ()
{
// Add observer for block confirmations
Expand All @@ -36,8 +52,8 @@ void nano::http_callbacks::setup_callbacks ()
stats.inc (nano::stat::type::http_callbacks_notified, nano::stat::detail::block_confirmed);

// Post callback processing to worker thread
// Safe to capture 'this' by reference as workers are stopped before node destruction
node.workers.post ([this, block_a, account_a, amount_a, is_state_send_a, is_state_epoch_a] () {
// Safe to capture 'this' by reference as workers are stopped before this component destruction
workers.post ([this, block_a, account_a, amount_a, is_state_send_a, is_state_epoch_a] () {
// Construct the callback payload as a property tree
boost::property_tree::ptree event;
event.add ("account", account_a.to_account ());
Expand Down
8 changes: 8 additions & 0 deletions nano/node/rpc_callbacks.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <nano/lib/thread_pool.hpp>
#include <nano/node/fwd.hpp>

namespace nano
Expand All @@ -9,6 +10,11 @@ class http_callbacks
public:
explicit http_callbacks (nano::node &);

void start ();
void stop ();

nano::container_info container_info () const;

private: // Dependencies
nano::node_config const & config;
nano::node & node;
Expand All @@ -20,5 +26,7 @@ class http_callbacks
private:
void setup_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;
};
}

0 comments on commit 1b98927

Please sign in to comment.