always drop the announcing peer when at capacity

This commit is contained in:
Steven Siloti 2016-09-19 21:07:39 -07:00
parent 6e24bbe77a
commit e2fefb074a
2 changed files with 10 additions and 27 deletions

View File

@ -267,26 +267,12 @@ namespace
torrent_entry* v; torrent_entry* v;
if (ti == m_map.end()) if (ti == m_map.end())
{ {
// we don't have this torrent, add it if (int(m_map.size()) >= m_settings.max_torrents)
// do we need to remove another one first?
if (!m_map.empty() && int(m_map.size()) >= m_settings.max_torrents)
{ {
// we need to remove some. Remove the ones with the // we're at capacity, drop the announce
// fewest peers return;
int num_peers = int(m_map.begin()->second.peers.size());
auto candidate = m_map.begin();
for (auto i = m_map.begin()
, end(m_map.end()); i != end; ++i)
{
if (int(i->second.peers.size()) > num_peers) continue;
if (i->first == info_hash) continue;
num_peers = int(i->second.peers.size());
candidate = i;
}
m_map.erase(candidate);
m_counters.peers -= num_peers;
m_counters.torrents -= 1;
} }
m_counters.torrents += 1; m_counters.torrents += 1;
v = &m_map[info_hash]; v = &m_map[info_hash];
} }
@ -314,13 +300,8 @@ namespace
} }
else if (v->peers.size() >= m_settings.max_peers) else if (v->peers.size() >= m_settings.max_peers)
{ {
// when we're at capacity, there's a 50/50 chance of dropping the // we're at capacity, drop the announce
// announcing peer or an existing peer return;
if (random(1)) return;
i = v->peers.lower_bound(peer);
if (i == v->peers.end()) --i;
v->peers.erase(i++);
m_counters.peers -= 1;
} }
v->peers.insert(i, peer); v->peers.insert(i, peer);
m_counters.peers += 1; m_counters.peers += 1;

View File

@ -111,6 +111,7 @@ TORRENT_TEST(announce_peer)
tcp::endpoint const p4 = ep("124.31.75.24", 1); tcp::endpoint const p4 = ep("124.31.75.24", 1);
s->announce_peer(n1, p1, "torrent_name", false); s->announce_peer(n1, p1, "torrent_name", false);
peers = entry();
s->get_peers(n1, udp::v4(), false, false, peers); s->get_peers(n1, udp::v4(), false, false, peers);
TEST_EQUAL(peers["n"].string(), "torrent_name") TEST_EQUAL(peers["n"].string(), "torrent_name")
TEST_EQUAL(peers["values"].list().size(), 1) TEST_EQUAL(peers["values"].list().size(), 1)
@ -118,8 +119,9 @@ TORRENT_TEST(announce_peer)
s->announce_peer(n2, p2, "torrent_name1", false); s->announce_peer(n2, p2, "torrent_name1", false);
s->announce_peer(n2, p3, "torrent_name1", false); s->announce_peer(n2, p3, "torrent_name1", false);
s->announce_peer(n3, p4, "torrent_name2", false); s->announce_peer(n3, p4, "torrent_name2", false);
bool r = s->get_peers(n1, udp::v4(), false, false, peers); peers = entry();
TEST_CHECK(!r); s->get_peers(n3, udp::v4(), false, false, peers);
TEST_CHECK(!peers.find_key("values"));
} }
TORRENT_TEST(dual_stack) TORRENT_TEST(dual_stack)