protocol: change standby mode to not wait sleeping

This commit is contained in:
moneromooo-monero 2018-12-07 12:32:50 +00:00
parent 85807dfb25
commit 5464725a29
No known key found for this signature in database
GPG key ID: 686F07454D6CEFC3
2 changed files with 24 additions and 12 deletions

View file

@ -131,6 +131,7 @@ namespace cryptonote
bool should_download_next_span(cryptonote_connection_context& context) const; bool should_download_next_span(cryptonote_connection_context& context) const;
void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans); void drop_connection(cryptonote_connection_context &context, bool add_fail, bool flush_all_spans);
bool kick_idle_peers(); bool kick_idle_peers();
bool check_standby_peers();
int try_add_next_blocks(cryptonote_connection_context &context); int try_add_next_blocks(cryptonote_connection_context &context);
t_core& m_core; t_core& m_core;
@ -143,6 +144,7 @@ namespace cryptonote
boost::mutex m_sync_lock; boost::mutex m_sync_lock;
block_queue m_block_queue; block_queue m_block_queue;
epee::math_helper::once_a_time_seconds<30> m_idle_peer_kicker; epee::math_helper::once_a_time_seconds<30> m_idle_peer_kicker;
epee::math_helper::once_a_time_milliseconds<100> m_standby_checker;
boost::mutex m_buffer_mutex; boost::mutex m_buffer_mutex;
double get_avg_block_size(); double get_avg_block_size();

View file

@ -1210,6 +1210,7 @@ skip:
bool t_cryptonote_protocol_handler<t_core>::on_idle() bool t_cryptonote_protocol_handler<t_core>::on_idle()
{ {
m_idle_peer_kicker.do_call(boost::bind(&t_cryptonote_protocol_handler<t_core>::kick_idle_peers, this)); m_idle_peer_kicker.do_call(boost::bind(&t_cryptonote_protocol_handler<t_core>::kick_idle_peers, this));
m_standby_checker.do_call(boost::bind(&t_cryptonote_protocol_handler<t_core>::check_standby_peers, this));
return m_core.on_idle(); return m_core.on_idle();
} }
//------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------
@ -1245,6 +1246,22 @@ skip:
} }
//------------------------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------------------------
template<class t_core> template<class t_core>
bool t_cryptonote_protocol_handler<t_core>::check_standby_peers()
{
m_p2p->for_each_connection([&](cryptonote_connection_context& context, nodetool::peerid_type peer_id, uint32_t support_flags)->bool
{
if (context.m_state == cryptonote_connection_context::state_standby)
{
LOG_PRINT_CCONTEXT_L2("requesting callback");
++context.m_callback_request_count;
m_p2p->request_callback(context);
}
return true;
});
return true;
}
//------------------------------------------------------------------------------------------------------------------------
template<class t_core>
int t_cryptonote_protocol_handler<t_core>::handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, cryptonote_connection_context& context) int t_cryptonote_protocol_handler<t_core>::handle_request_chain(int command, NOTIFY_REQUEST_CHAIN::request& arg, cryptonote_connection_context& context)
{ {
MLOG_P2P_MESSAGE("Received NOTIFY_REQUEST_CHAIN (" << arg.block_ids.size() << " blocks"); MLOG_P2P_MESSAGE("Received NOTIFY_REQUEST_CHAIN (" << arg.block_ids.size() << " blocks");
@ -1338,14 +1355,13 @@ skip:
bool start_from_current_chain = false; bool start_from_current_chain = false;
if (!force_next_span) if (!force_next_span)
{ {
bool first = true; do
while (1)
{ {
size_t nblocks = m_block_queue.get_num_filled_spans(); size_t nblocks = m_block_queue.get_num_filled_spans();
size_t size = m_block_queue.get_data_size(); size_t size = m_block_queue.get_data_size();
if (nblocks < BLOCK_QUEUE_NBLOCKS_THRESHOLD || size < BLOCK_QUEUE_SIZE_THRESHOLD) if (nblocks < BLOCK_QUEUE_NBLOCKS_THRESHOLD || size < BLOCK_QUEUE_SIZE_THRESHOLD)
{ {
if (!first) if (context.m_state != cryptonote_connection_context::state_standby)
{ {
LOG_DEBUG_CC(context, "Block queue is " << nblocks << " and " << size << ", resuming"); LOG_DEBUG_CC(context, "Block queue is " << nblocks << " and " << size << ", resuming");
} }
@ -1368,10 +1384,9 @@ skip:
break; break;
} }
if (first) if (context.m_state != cryptonote_connection_context::state_standby)
{ {
LOG_DEBUG_CC(context, "Block queue is " << nblocks << " and " << size << ", pausing"); LOG_DEBUG_CC(context, "Block queue is " << nblocks << " and " << size << ", pausing");
first = false;
context.m_state = cryptonote_connection_context::state_standby; context.m_state = cryptonote_connection_context::state_standby;
} }
@ -1385,13 +1400,8 @@ skip:
return true; return true;
} }
for (size_t n = 0; n < 50; ++n) return true;
{ } while(0);
if (m_stopping)
return true;
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
}
}
context.m_state = cryptonote_connection_context::state_synchronizing; context.m_state = cryptonote_connection_context::state_synchronizing;
} }