From f5fa00a747c43e3eafe714985602cea8e0c6e541 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Mon, 30 Sep 2024 08:15:16 -0500 Subject: [PATCH] GH-835 Reuse connection --- plugins/net_plugin/net_plugin.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/plugins/net_plugin/net_plugin.cpp b/plugins/net_plugin/net_plugin.cpp index 624fb2ccad..d2becf3426 100644 --- a/plugins/net_plugin/net_plugin.cpp +++ b/plugins/net_plugin/net_plugin.cpp @@ -4719,7 +4719,31 @@ namespace eosio { auto& index = connections.get(); index.erase(c); } - resolve_and_connect(c->peer_address(), c->listen_address, c->consecutive_immediate_connection_close.load()); + + auto [host, port, type] = split_host_port_type(c->peer_address()); + c->strand.post([this, c, host, port]() { + auto resolver = std::make_shared( my_impl->thread_pool.get_executor() ); + resolver->async_resolve(host, port, boost::asio::bind_executor(c->strand, + [this, resolver, c, host, port] + ( const boost::system::error_code& err, const tcp::resolver::results_type& results ) { + c->set_heartbeat_timeout( heartbeat_timeout ); + { + std::lock_guard g( connections_mtx ); + connections.emplace( connection_detail{ + .host = c->peer_address(), + .c = c, + }); + } + if( !err ) { + c->connect( results ); + } else { + fc_wlog( logger, "Unable to resolve ${host}:${port} ${error}", + ("host", host)("port", port)( "error", err.message() ) ); + c->set_state(connection::connection_state::closed); + ++c->consecutive_immediate_connection_close; + } + }) ); + }); } // called by API