diff --git a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp index a8ab651b..63529343 100644 --- a/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp +++ b/libraries/plugins/peerplays_sidechain/common/rpc_client.cpp @@ -311,6 +311,9 @@ void rpc_client::schedule_connection_selection() void rpc_client::select_connection() { //ilog("n_active_rpc_client=${n}", ("n", n_active_rpc_client)); + static const int t_limit = 10*1000000, // 10 sec + quality_diff_threshold = 10000; // 10 ms + FC_ASSERT(connections.size()); int best_n = -1; @@ -318,6 +321,9 @@ void rpc_client::select_connection() std::vector head_block_numbers; head_block_numbers.resize(connections.size()); + + std::vector qualities; + qualities.resize(connections.size()); for (size_t n=0; n < connections.size(); n++) { rpc_connection *conn = connections[n]; @@ -333,7 +339,6 @@ void rpc_client::select_connection() t += rand() % 10; FC_ASSERT(t != -1); head_block_numbers[n] = head_block_number; - static const int t_limit = 10*1000000; // 10 sec if (t < t_limit) quality = t_limit - t; // the less time, the higher quality @@ -343,15 +348,17 @@ void rpc_client::select_connection() best_quality = quality; } } + qualities[n] = quality; + } - FC_ASSERT(best_n != -1 && best_quality != -1); - if (best_n != n_active_conn) { // if the best client is not the current one, ... - uint64_t active_head_block_number = head_block_numbers[n_active_conn]; - if (active_head_block_number == std::numeric_limits::max() // ... and the current one has no known head block... - || head_block_numbers[best_n] >= active_head_block_number) { // ...or the best client's head is more recent than the current, ... - n_active_conn = best_n; // ...then select new one - ilog("!!! rpc connection reselected, now ${n}", ("n", n_active_conn)); - } + FC_ASSERT(best_n != -1 && best_quality != -1); + if (best_n != n_active_conn) { // if the best client is not the current one, ... + uint64_t active_head_block_number = head_block_numbers[n_active_conn]; + if ((active_head_block_number == std::numeric_limits::max() // ...and the current one has no known head block... + || head_block_numbers[best_n] >= active_head_block_number) // ...or the best client's head is more recent than the current, ... + && best_quality > qualities[n_active_conn] + quality_diff_threshold) { // ...and the new client's quality exceeds current more than by threshold + n_active_conn = best_n; // ...then select new one + ilog("!!! rpc connection reselected, now ${n}", ("n", n_active_conn)); } }