From 1bf562beda1fc3fb7444e501112e694b4db53818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 19 Mar 2024 16:57:58 +0100 Subject: [PATCH] RPC SHARED IO_CTX --- nano/nano_node/daemon.cpp | 2 +- nano/nano_rpc/entry.cpp | 2 +- nano/nano_wallet/entry.cpp | 2 +- nano/rpc/rpc.cpp | 9 +++++---- nano/rpc/rpc.hpp | 14 +++++++++----- nano/rpc_test/rpc.cpp | 2 +- nano/rpc_test/rpc_context.cpp | 2 +- nano/slow_test/bootstrap.cpp | 2 +- 8 files changed, 20 insertions(+), 15 deletions(-) diff --git a/nano/nano_node/daemon.cpp b/nano/nano_node/daemon.cpp index 460470f1e8..879bc5e2d1 100644 --- a/nano/nano_node/daemon.cpp +++ b/nano/nano_node/daemon.cpp @@ -176,7 +176,7 @@ void nano::daemon::run (std::filesystem::path const & data_path, nano::node_flag } }); }); - rpc = nano::get_rpc (*io_ctx, rpc_config, *rpc_handler); + rpc = nano::get_rpc (io_ctx, rpc_config, *rpc_handler); rpc->start (); } else diff --git a/nano/nano_rpc/entry.cpp b/nano/nano_rpc/entry.cpp index 625f24c63b..b198d20820 100644 --- a/nano/nano_rpc/entry.cpp +++ b/nano/nano_rpc/entry.cpp @@ -55,7 +55,7 @@ void run (std::filesystem::path const & data_path, std::vector cons try { nano::ipc_rpc_processor ipc_rpc_processor (*io_ctx, rpc_config); - auto rpc = nano::get_rpc (*io_ctx, rpc_config, ipc_rpc_processor); + auto rpc = nano::get_rpc (io_ctx, rpc_config, ipc_rpc_processor); rpc->start (); debug_assert (!nano::signal_handler_impl); diff --git a/nano/nano_wallet/entry.cpp b/nano/nano_wallet/entry.cpp index de5699630d..5c3a0a29af 100644 --- a/nano/nano_wallet/entry.cpp +++ b/nano/nano_wallet/entry.cpp @@ -192,7 +192,7 @@ int run_wallet (QApplication & application, int argc, char * const * argv, std:: } rpc_config.tls_config = tls_config; rpc_handler = std::make_unique (*node, ipc, config.rpc); - rpc = nano::get_rpc (*io_ctx, rpc_config, *rpc_handler); + rpc = nano::get_rpc (io_ctx, rpc_config, *rpc_handler); rpc->start (); } else diff --git a/nano/rpc/rpc.cpp b/nano/rpc/rpc.cpp index 687c9b316e..196feee1ad 100644 --- a/nano/rpc/rpc.cpp +++ b/nano/rpc/rpc.cpp @@ -12,10 +12,11 @@ #include #endif -nano::rpc::rpc (boost::asio::io_context & io_ctx_a, nano::rpc_config config_a, nano::rpc_handler_interface & rpc_handler_interface_a) : +nano::rpc::rpc (std::shared_ptr io_ctx_a, nano::rpc_config config_a, nano::rpc_handler_interface & rpc_handler_interface_a) : config (std::move (config_a)), - acceptor (io_ctx_a), - io_ctx (io_ctx_a), + io_ctx_shared (io_ctx_a), + io_ctx (*io_ctx_shared), + acceptor (io_ctx), rpc_handler_interface (rpc_handler_interface_a) { rpc_handler_interface.rpc_instance (*this); @@ -78,7 +79,7 @@ void nano::rpc::stop () acceptor.close (); } -std::unique_ptr nano::get_rpc (boost::asio::io_context & io_ctx_a, nano::rpc_config const & config_a, nano::rpc_handler_interface & rpc_handler_interface_a) +std::unique_ptr nano::get_rpc (std::shared_ptr io_ctx_a, nano::rpc_config const & config_a, nano::rpc_handler_interface & rpc_handler_interface_a) { std::unique_ptr impl; diff --git a/nano/rpc/rpc.hpp b/nano/rpc/rpc.hpp index 99f66b260a..353d263138 100644 --- a/nano/rpc/rpc.hpp +++ b/nano/rpc/rpc.hpp @@ -20,25 +20,29 @@ class rpc_handler_interface; class rpc { public: - rpc (boost::asio::io_context & io_ctx_a, nano::rpc_config config_a, nano::rpc_handler_interface & rpc_handler_interface_a); + rpc (std::shared_ptr, nano::rpc_config config_a, nano::rpc_handler_interface & rpc_handler_interface_a); virtual ~rpc (); + void start (); - virtual void accept (); void stop (); - std::uint16_t listening_port () + virtual void accept (); + + std::uint16_t listening_port () const { return acceptor.local_endpoint ().port (); } +public: nano::logger logger{ "rpc" }; nano::rpc_config config; - boost::asio::ip::tcp::acceptor acceptor; + std::shared_ptr io_ctx_shared; boost::asio::io_context & io_ctx; + boost::asio::ip::tcp::acceptor acceptor; nano::rpc_handler_interface & rpc_handler_interface; bool stopped{ false }; }; /** Returns the correct RPC implementation based on TLS configuration */ -std::unique_ptr get_rpc (boost::asio::io_context & io_ctx_a, nano::rpc_config const & config_a, nano::rpc_handler_interface & rpc_handler_interface_a); +std::unique_ptr get_rpc (std::shared_ptr, nano::rpc_config const & config_a, nano::rpc_handler_interface & rpc_handler_interface_a); } diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index ebdbcc6d46..7d4e0705f7 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -6047,7 +6047,7 @@ TEST (rpc, simultaneous_calls) ASSERT_TRUE (ipc_tcp_port.has_value ()); rpc_config.rpc_process.num_ipc_connections = 8; nano::ipc_rpc_processor ipc_rpc_processor (*system.io_ctx, rpc_config, ipc_tcp_port.value ()); - nano::rpc rpc (*system.io_ctx, rpc_config, ipc_rpc_processor); + nano::rpc rpc (system.io_ctx, rpc_config, ipc_rpc_processor); rpc.start (); boost::property_tree::ptree request; request.put ("action", "account_block_count"); diff --git a/nano/rpc_test/rpc_context.cpp b/nano/rpc_test/rpc_context.cpp index 96d35b651e..31cadfdc4f 100644 --- a/nano/rpc_test/rpc_context.cpp +++ b/nano/rpc_test/rpc_context.cpp @@ -50,7 +50,7 @@ nano::test::rpc_context nano::test::add_rpc (nano::test::system & system, std::s const auto ipc_tcp_port = ipc_server->listening_tcp_port (); debug_assert (ipc_tcp_port.has_value ()); auto ipc_rpc_processor (std::make_unique (*system.io_ctx, rpc_config, ipc_tcp_port.value ())); - auto rpc (std::make_shared (*system.io_ctx, rpc_config, *ipc_rpc_processor)); + auto rpc (std::make_shared (system.io_ctx, rpc_config, *ipc_rpc_processor)); rpc->start (); return rpc_context{ rpc, ipc_server, ipc_rpc_processor, node_rpc_config }; diff --git a/nano/slow_test/bootstrap.cpp b/nano/slow_test/bootstrap.cpp index 9834c0ea2c..95662612ad 100644 --- a/nano/slow_test/bootstrap.cpp +++ b/nano/slow_test/bootstrap.cpp @@ -35,7 +35,7 @@ class rpc_wrapper rpc_config{ node.network_params.network, port, true }, ipc{ node, node_rpc_config }, ipc_rpc_processor{ *system.io_ctx, rpc_config }, - rpc{ *system.io_ctx, rpc_config, ipc_rpc_processor } + rpc{ system.io_ctx, rpc_config, ipc_rpc_processor } { }