From f336eafb19fb52086a73f21b7c09f207af2bf504 Mon Sep 17 00:00:00 2001 From: Kevin Heifner Date: Tue, 1 Oct 2024 09:04:24 -0500 Subject: [PATCH] GH-525 Minimum code changes to resolve on reconnect --- plugins/net_plugin/net_plugin.cpp | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/plugins/net_plugin/net_plugin.cpp b/plugins/net_plugin/net_plugin.cpp index f7a90a7a34..ab3295eed7 100644 --- a/plugins/net_plugin/net_plugin.cpp +++ b/plugins/net_plugin/net_plugin.cpp @@ -419,8 +419,9 @@ namespace eosio { void add(connection_ptr c); string connect(const string& host, const string& p2p_address); - string resolve_and_connect(const string& host, const string& p2p_address); + string resolve_and_connect(const string& host, const string& p2p_address, uint16_t consecutive_immediate_connection_close = 0); void connect(const connection_ptr& c); + void remove(const connection_ptr& c); string disconnect(const string& host); void close_all(); @@ -835,7 +836,7 @@ namespace eosio { public: enum class connection_state { connecting, connected, closing, closed }; - explicit connection( const string& endpoint, const string& listen_address ); + explicit connection( const string& endpoint, const string& listen_address, uint16_t consecutive_immediate_connection_close ); /// @brief ctor /// @param socket created by boost::asio in fc::listener /// @param address identifier of listen socket which accepted this new connection @@ -1264,13 +1265,14 @@ namespace eosio { //--------------------------------------------------------------------------- - connection::connection( const string& endpoint, const string& listen_address ) + connection::connection( const string& endpoint, const string& listen_address, uint16_t consecutive_immediate_connection_close ) : peer_addr( endpoint ), strand( my_impl->thread_pool.get_executor() ), socket( new tcp::socket( my_impl->thread_pool.get_executor() ) ), listen_address( listen_address ), log_p2p_address( endpoint ), connection_id( ++my_impl->current_connection_id ), + consecutive_immediate_connection_close( consecutive_immediate_connection_close ), sync_response_expected_timer( my_impl->thread_pool.get_executor() ), last_handshake_recv(), last_handshake_sent(), @@ -2826,10 +2828,10 @@ namespace eosio { return true; // true so doesn't remove from valid connections } } + connection_ptr c = shared_from_this(); - strand.post([c]() { - my_impl->connections.connect(c); - }); + my_impl->connections.remove(c); + my_impl->connections.resolve_and_connect(c->peer_address(), c->listen_address, c->consecutive_immediate_connection_close.load()); return true; } @@ -4657,7 +4659,7 @@ namespace eosio { return resolve_and_connect( host, p2p_address ); } - string connections_manager::resolve_and_connect( const string& peer_address, const string& listen_address ) { + string connections_manager::resolve_and_connect( const string& peer_address, const string& listen_address, uint16_t consecutive_immediate_connection_close ) { string::size_type colon = peer_address.find(':'); if (colon == std::string::npos || colon == 0) { fc_elog( logger, "Invalid peer address. must be \"host:port[:|]\": ${p}", ("p", peer_address) ); @@ -4673,8 +4675,9 @@ namespace eosio { auto resolver = std::make_shared( my_impl->thread_pool.get_executor() ); resolver->async_resolve(host, port, - [resolver, host = host, port = port, peer_address = peer_address, listen_address = listen_address, this]( const boost::system::error_code& err, const tcp::resolver::results_type& results ) { - connection_ptr c = std::make_shared( peer_address, listen_address ); + [resolver, host, port, peer_address, listen_address, consecutive_immediate_connection_close, this] + ( const boost::system::error_code& err, const tcp::resolver::results_type& results ) { + connection_ptr c = std::make_shared( peer_address, listen_address, consecutive_immediate_connection_close ); c->set_heartbeat_timeout( heartbeat_timeout ); std::lock_guard g( connections_mtx ); auto [it, inserted] = connections.emplace( connection_detail{ @@ -4688,7 +4691,7 @@ namespace eosio { fc_wlog( logger, "Unable to resolve ${host}:${port} ${error}", ("host", host)("port", port)( "error", err.message() ) ); it->c->set_state(connection::connection_state::closed); - ++(it->c->consecutive_immediate_connection_close); + ++it->c->consecutive_immediate_connection_close; } } ); @@ -4704,7 +4707,13 @@ namespace eosio { } } - // called by API + void connections_manager::remove(const connection_ptr& c) { + std::lock_guard g( connections_mtx ); + auto& index = connections.get(); + index.erase(c); + } + +// called by API string connections_manager::disconnect( const string& host ) { std::lock_guard g( connections_mtx ); auto& index = connections.get();