Skip to content

Commit d6c493d

Browse files
authored
Merge pull request #2718 from bitshares/fix-network-mapper
Fix network_mapper
2 parents 1014956 + eb0e5ce commit d6c493d

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

programs/network_mapper/network_mapper.cpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class peer_probe : public graphene::net::peer_connection_delegate
2222
bool _done = false;
2323
graphene::net::peer_connection_ptr _connection = graphene::net::peer_connection::make_shared(this);
2424
fc::promise<void>::ptr _probe_complete_promise = fc::promise<void>::create("probe_complete");
25+
fc::future<void> _timeout_handler;
2526

2627
fc::ip::endpoint _remote;
2728
graphene::net::node_id_t _node_id;
@@ -50,6 +51,15 @@ class peer_probe : public graphene::net::peer_connection_delegate
5051
chain_id,
5152
fc::variant_object());
5253

54+
constexpr uint16_t timeout = 180; // seconds
55+
_timeout_handler = fc::schedule( [this]() {
56+
wlog( "Communication with peer ${remote} took too long, closing connection", ("remote", _remote) );
57+
wdump( (_peers)(_peers.size()) );
58+
_timeout_handler = fc::future<void>();
59+
_we_closed_connection = true;
60+
_connection->close_connection();
61+
}, fc::time_point::now() + fc::seconds(timeout), "timeout_handler" );
62+
5363
_connection->send_message(hello);
5464
} catch( const fc::exception& e ) {
5565
ilog( "Got exception when connecting to peer ${endpoint} ${e}",
@@ -156,8 +166,9 @@ class peer_probe : public graphene::net::peer_connection_delegate
156166
{
157167
// Note: In rare cases, the peer may neither send us an address_message nor close the connection,
158168
// causing us to wait forever.
159-
// We tolerate it, because this program (network_mapper) is not critical.
169+
// In this case the timeout handler will close the connection.
160170
_done = true;
171+
_timeout_handler.cancel();
161172
_probe_complete_promise->set_value();
162173
}
163174

@@ -230,7 +241,7 @@ int main(int argc, char** argv)
230241
const auto& update_info_by_address_info = [ &address_info_by_node_id, &my_node_id, &nodes_already_visited,
231242
&nodes_to_visit_set, &nodes_to_visit ] ( const graphene::net::address_info& info )
232243
{
233-
if (info.node_id == my_node_id) // We should not be in the list, just be defensive here
244+
if( info.node_id == graphene::net::node_id_t(my_node_id) ) // We should not be in the list, be defensive
234245
return;
235246
if (nodes_already_visited.find(info.remote_endpoint) == nodes_already_visited.end() &&
236247
nodes_to_visit_set.find(info.remote_endpoint) == nodes_to_visit_set.end())
@@ -361,7 +372,8 @@ int main(int argc, char** argv)
361372
constexpr uint16_t pair_depth = 2;
362373
for (auto& node_and_connections : connections_by_node_id)
363374
for (const graphene::net::address_info& this_connection : node_and_connections.second)
364-
if( this_connection.node_id != my_node_id ) // We should not be in the list, just be defensive here
375+
if( this_connection.node_id != graphene::net::node_id_t(my_node_id) ) // We should not be in the list,
376+
// just be defensive here
365377
dot_stream << " \"" << fc::variant( node_and_connections.first, pair_depth ).as_string()
366378
<< "\" -- \"" << fc::variant( this_connection.node_id, 1 ).as_string() << "\";\n";
367379

0 commit comments

Comments
 (0)