avoid flooding the alert queue in client_test

This commit is contained in:
arvidn 2017-03-26 19:07:15 -04:00 committed by Arvid Norberg
parent fb60c23807
commit b25178e7c8
2 changed files with 43 additions and 25 deletions

View File

@ -924,6 +924,23 @@ bool handle_alert(torrent_view& view, session_view& ses_view
} }
void pop_alerts(torrent_view& view, session_view& ses_view
, libtorrent::session& ses, std::deque<std::string> events)
{
std::vector<lt::alert*> alerts;
ses.pop_alerts(&alerts);
for (auto a : alerts)
{
if (::handle_alert(view, ses_view, ses, a)) continue;
// if we didn't handle the alert, print it to the log
std::string event_string;
print_alert(a, event_string);
events.push_back(event_string);
if (events.size() >= 20) events.pop_front();
}
}
void print_piece(libtorrent::partial_piece_info const* pp void print_piece(libtorrent::partial_piece_info const* pp
, libtorrent::cached_piece_info const* cs , libtorrent::cached_piece_info const* cs
, std::vector<libtorrent::peer_info> const& peers , std::vector<libtorrent::peer_info> const& peers
@ -1256,6 +1273,7 @@ MAGNETURL is a magnet link
} }
else else
{ {
int idx = 0;
for (auto const& e : ents) for (auto const& e : ents)
{ {
std::string const file = path_append(resume_dir, e); std::string const file = path_append(resume_dir, e);
@ -1281,6 +1299,14 @@ MAGNETURL is a magnet link
p.flags &= ~add_torrent_params::flag_need_save_resume; p.flags &= ~add_torrent_params::flag_need_save_resume;
ses.async_add_torrent(std::move(p)); ses.async_add_torrent(std::move(p));
++idx;
if ((idx % 32) == 0)
{
// regularly, pop and handle alerts, to avoid the alert queue from
// filling up with add_torrent_alerts
pop_alerts(view, ses_view, ses, events);
}
} }
} }
@ -1580,20 +1606,7 @@ MAGNETURL is a magnet link
if (c == 'q') break; if (c == 'q') break;
} }
// loop through the alert queue to see if anything has happened. pop_alerts(view, ses_view, ses, events);
std::vector<alert*> alerts;
ses.pop_alerts(&alerts);
for (auto i : alerts)
{
if (::handle_alert(view, ses_view, ses, i)) continue;
// if we didn't handle the alert, print it to the log
std::string event_string;
print_alert(i, event_string);
events.push_back(event_string);
if (events.size() >= 20) events.pop_front();
}
alerts.clear();
std::string out; std::string out;
@ -1913,12 +1926,18 @@ MAGNETURL is a magnet link
return true; return true;
}, 0); }, 0);
int idx = 0;
for (auto const& st : temp) for (auto const& st : temp)
{ {
// save_resume_data will generate an alert when it's done // save_resume_data will generate an alert when it's done
st.handle.save_resume_data(torrent_handle::save_info_dict); st.handle.save_resume_data(torrent_handle::save_info_dict);
++num_outstanding_resume_data; ++num_outstanding_resume_data;
++idx;
if ((idx % 32) == 0)
{
std::printf("\r%d ", num_outstanding_resume_data); std::printf("\r%d ", num_outstanding_resume_data);
pop_alerts(view, ses_view, ses, events);
}
} }
std::printf("\nwaiting for resume data [%d]\n", num_outstanding_resume_data); std::printf("\nwaiting for resume data [%d]\n", num_outstanding_resume_data);
@ -1926,16 +1945,7 @@ MAGNETURL is a magnet link
{ {
alert const* a = ses.wait_for_alert(seconds(10)); alert const* a = ses.wait_for_alert(seconds(10));
if (a == nullptr) continue; if (a == nullptr) continue;
pop_alerts(view, ses_view, ses, events);
std::vector<alert*> alerts;
ses.pop_alerts(&alerts);
for (auto i : alerts)
{
if (::handle_alert(view, ses_view, ses, i)) continue;
// if we didn't handle the alert, print it to the log
std::string event_string;
print_alert(i, event_string);
}
} }
if (g_log_file) std::fclose(g_log_file); if (g_log_file) std::fclose(g_log_file);

View File

@ -68,7 +68,15 @@ namespace libtorrent {
// for high priority alerts, double the upper limit // for high priority alerts, double the upper limit
if (m_alerts[m_generation].size() >= m_queue_size_limit if (m_alerts[m_generation].size() >= m_queue_size_limit
* (1 + T::priority)) * (1 + T::priority))
{
// if (T::priority > 0)
// {
// TODO: there should be a way for the client to detect that an
// alert was dropped. Maybe add a flag to each m_alerts
// generation
// }
return; return;
}
T& alert = m_alerts[m_generation].emplace_back<T>( T& alert = m_alerts[m_generation].emplace_back<T>(
m_allocations[m_generation], std::forward<Args>(args)...); m_allocations[m_generation], std::forward<Args>(args)...);