Skip to content

Commit

Permalink
avoid empty dht routing table buckets
Browse files Browse the repository at this point in the history
  • Loading branch information
arvidn committed Jul 27, 2019
1 parent 929f727 commit ca27892
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/libtorrent/kademlia/routing_table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ class TORRENT_EXTRA_EXPORT routing_table
// replacement list
void fill_from_replacements(table_t::iterator bucket);

void prune_empty_bucket();

dht_settings const& m_settings;

// (k-bucket, replacement cache) pairs
Expand Down
23 changes: 23 additions & 0 deletions src/kademlia/routing_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,15 @@ void routing_table::fill_from_replacements(table_t::iterator bucket)
}
}

void routing_table::prune_empty_bucket()
{
if (m_buckets.back().live_nodes.empty()
&& m_buckets.back().replacements.empty())
{
m_buckets.erase(m_buckets.end() - 1);
}
}

void routing_table::remove_node_internal(node_entry* n, bucket_t& b)
{
if (!b.empty()
Expand Down Expand Up @@ -437,6 +446,17 @@ bool routing_table::add_node(node_entry const& e)
continue;

s = add_node_impl(e);

// we just split the last bucket and tried to insert a new node. If none
// of the nodes in the split bucket, nor the new node ended up in the new
// bucket, erase it
if (m_buckets.back().live_nodes.empty())
{
m_buckets.erase(m_buckets.end() - 1);
// we just split, trying to add the node again should not request
// another split
TORRENT_ASSERT(s != need_bucket_split);
}
if (s == failed_to_add) return false;
if (s == node_added) return true;
}
Expand Down Expand Up @@ -501,6 +521,7 @@ routing_table::add_node_status_t routing_table::add_node_impl(node_entry e)
// if this was a replacement node it may be elligible for
// promotion to the live bucket
fill_from_replacements(existing_bucket);
prune_empty_bucket();
return node_added;
}
else if (existing->id.is_all_zeros())
Expand Down Expand Up @@ -543,6 +564,7 @@ routing_table::add_node_status_t routing_table::add_node_impl(node_entry e)
node.last_queried = min_time();
}

prune_empty_bucket();
return failed_to_add;
}
}
Expand Down Expand Up @@ -1013,6 +1035,7 @@ void routing_table::node_failed(node_id const& nid, udp::endpoint const& ep)
b.erase(j);

fill_from_replacements(i);
prune_empty_bucket();
}

void routing_table::add_router_node(udp::endpoint const& router)
Expand Down

0 comments on commit ca27892

Please sign in to comment.