make test_pex a bit more reliable

This commit is contained in:
Arvid Norberg 2013-10-18 08:14:49 +00:00
parent 17d276e060
commit 299cec6377
2 changed files with 46 additions and 14 deletions

View File

@ -90,7 +90,7 @@ namespace libtorrent { namespace
// delay the rebuilding // delay the rebuilding
ut_pex_plugin(torrent& t) ut_pex_plugin(torrent& t)
: m_torrent(t) : m_torrent(t)
, m_1_minute(random() % (std::max)(60 - int(m_torrent.session().get_torrents().size()), 1)) , m_last_msg(min_time())
, m_peers_in_message(0) {} , m_peers_in_message(0) {}
virtual boost::shared_ptr<peer_plugin> new_connection(peer_connection* pc); virtual boost::shared_ptr<peer_plugin> new_connection(peer_connection* pc);
@ -112,9 +112,12 @@ namespace libtorrent { namespace
// max_peer_entries limits the packet size // max_peer_entries limits the packet size
virtual void tick() virtual void tick()
{ {
if (++m_1_minute < 60) return; ptime now = time_now();
if (now - m_last_msg < seconds(60)) return;
m_last_msg = now;
m_1_minute = 0; int num_peers = m_torrent.num_peers();
if (num_peers == 0) return;
entry pex; entry pex;
std::string& pla = pex["added"].string(); std::string& pla = pex["added"].string();
@ -226,7 +229,7 @@ namespace libtorrent { namespace
torrent& m_torrent; torrent& m_torrent;
std::set<tcp::endpoint> m_old_peers; std::set<tcp::endpoint> m_old_peers;
int m_1_minute; ptime m_last_msg;
std::vector<char> m_ut_pex_msg; std::vector<char> m_ut_pex_msg;
int m_peers_in_message; int m_peers_in_message;
}; };
@ -238,7 +241,7 @@ namespace libtorrent { namespace
: m_torrent(t) : m_torrent(t)
, m_pc(pc) , m_pc(pc)
, m_tp(tp) , m_tp(tp)
, m_1_minute(60) , m_last_msg(min_time())
, m_message_index(0) , m_message_index(0)
, m_first_time(true) , m_first_time(true)
{ {
@ -430,11 +433,41 @@ namespace libtorrent { namespace
// no handshake yet // no handshake yet
if (!m_message_index) return; if (!m_message_index) return;
// if there aren't any peers other than this one, ptime now = time_now();
// there no need to start the pex logic yet if (now - m_last_msg < seconds(60))
if (m_first_time && m_torrent.num_peers() <= 1) return; {
#ifdef TORRENT_VERBOSE_LOGGING
m_pc.peer_log("*** PEX [ waiting: %d seconds to next msg ]"
, total_seconds(seconds(60) - (now - m_last_msg)));
#endif
return;
}
static ptime global_last = min_time();
if (++m_1_minute <= 60) return; int num_peers = m_torrent.num_peers();
if (num_peers == 0) return;
// don't send pex messages more often than 1 every 100 ms, and
// allow pex messages to be sent 5 seconds apart if there isn't
// contention
int delay = (std::min)((std::max)(60000 / num_peers, 100), 3000);
if (now - global_last < milliseconds(delay))
{
#ifdef TORRENT_VERBOSE_LOGGING
m_pc.peer_log("*** PEX [ global-wait: %d ]", total_seconds(milliseconds(delay) - (now - global_last)));
#endif
return;
}
// this will allow us to catch up, even if our timer
// has lower resolution than delay
if (global_last == min_time())
global_last = now;
else
global_last += milliseconds(delay);
m_last_msg = now;
if (m_first_time) if (m_first_time)
{ {
@ -445,7 +478,6 @@ namespace libtorrent { namespace
{ {
send_ut_peer_diff(); send_ut_peer_diff();
} }
m_1_minute = 0;
} }
void send_ut_peer_diff() void send_ut_peer_diff()
@ -600,12 +632,12 @@ namespace libtorrent { namespace
// we look at 6 pex messages back. // we look at 6 pex messages back.
ptime m_last_pex[6]; ptime m_last_pex[6];
int m_1_minute; ptime m_last_msg;
int m_message_index; int m_message_index;
// this is initialized to true, and set to // this is initialized to true, and set to
// false after the first pex message has been sent. // false after the first pex message has been sent.
// it is used to know if a diff message or a full // it is used to know if a diff message or a) ful
// message should be sent. // message should be sent.
bool m_first_time; bool m_first_time;
}; };

View File

@ -115,7 +115,7 @@ void test_pex()
torrent_status st1; torrent_status st1;
torrent_status st2; torrent_status st2;
torrent_status st3; torrent_status st3;
for (int i = 0; i < 50; ++i) for (int i = 0; i < 80; ++i)
{ {
print_alerts(ses1, "ses1"); print_alerts(ses1, "ses1");
print_alerts(ses2, "ses2"); print_alerts(ses2, "ses2");
@ -125,7 +125,7 @@ void test_pex()
st2 = tor2.status(); st2 = tor2.status();
st3 = tor3.status(); st3 = tor3.status();
print_ses_rate(i, &st1, &st2, &st3); print_ses_rate(i / 10.f, &st1, &st2, &st3);
// this is the success condition // this is the success condition
if (st1.num_peers == 2 && st2.num_peers == 2 && st3.num_peers == 2) if (st1.num_peers == 2 && st2.num_peers == 2 && st3.num_peers == 2)