optimize PEX to trigger earlier, and only start applying the load-balancing logic when it's needed

This commit is contained in:
Arvid Norberg 2013-08-04 05:09:20 +00:00
parent 11fd428c46
commit 27ea092b51
2 changed files with 15 additions and 4 deletions

View File

@ -86,7 +86,12 @@ namespace libtorrent { namespace
{
// randomize when we rebuild the pex message
// to evenly spread it out across all torrents
ut_pex_plugin(torrent& t): m_torrent(t), m_1_minute(random() % 60), m_peers_in_message(0) {}
// the more torrents we have, the longer we can
// delay the rebuilding
ut_pex_plugin(torrent& t)
: m_torrent(t)
, m_1_minute(random() % (std::max)(60 - int(m_torrent.session().get_torrents().size()), 1))
, m_peers_in_message(0) {}
virtual boost::shared_ptr<peer_plugin> new_connection(peer_connection* pc);
@ -422,7 +427,13 @@ namespace libtorrent { namespace
// every minute we send a pex message
virtual void tick()
{
if (!m_message_index) return; // no handshake yet
// no handshake yet
if (!m_message_index) return;
// if there aren't any peers other than this one,
// there no need to start the pex logic yet
if (m_first_time && m_torrent.num_peers() <= 1) return;
if (++m_1_minute <= 60) return;
if (m_first_time)

View File

@ -108,7 +108,7 @@ void test_pex()
torrent_status st1;
torrent_status st2;
torrent_status st3;
for (int i = 0; i < 15; ++i)
for (int i = 0; i < 50; ++i)
{
print_alerts(ses1, "ses1");
print_alerts(ses2, "ses2");
@ -129,7 +129,7 @@ void test_pex()
// through session 2
if (st3.state == torrent_status::seeding) break;
test_sleep(1000);
test_sleep(100);
}
TEST_CHECK(st1.num_peers == 2 && st2.num_peers == 2 && st3.num_peers == 2)