mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-08 12:28:42 +00:00
searches: prefer to remove expired nodes
This commit is contained in:
1 parent
8f9089abba
commit
39b8ca3fce
2 files changed
+30
-6
No files matched your search
@@ -497,6 +497,8 @@ private:
|
||||
time_point getListenTime(time_point now) const;
|
||||
|
||||
time_point getNextStepTime(const std::map<ValueType::Id, ValueType>& types, time_point now) const;
|
||||
|
||||
bool removeExpiredNode(time_point now);
|
||||
};
|
||||
|
||||
struct ValueStorage {
|
||||
|
||||
+28
-6
@@ -600,6 +600,20 @@ Dht::findSearch(unsigned short tid, sa_family_t af)
|
||||
return sr == searches.end() ? nullptr : &(*sr);
|
||||
}
|
||||
|
||||
bool
|
||||
Dht::Search::removeExpiredNode(time_point now)
|
||||
{
|
||||
auto e = nodes.end();
|
||||
while (e != nodes.begin()) {
|
||||
e = std::prev(e);
|
||||
if (e->node->isExpired(now)) {
|
||||
nodes.erase(e);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* A search contains a list of nodes, sorted by decreasing distance to the
|
||||
target. We just got a new candidate, insert it at the right spot or
|
||||
discard it. */
|
||||
@@ -614,7 +628,7 @@ Dht::Search::insertNode(std::shared_ptr<Node> node, time_point now, const Blob&
|
||||
const auto& nid = node->id;
|
||||
|
||||
// Fast track for the case where the node is not relevant for this search
|
||||
if (nodes.size() == SEARCH_NODES && id.xorCmp(nid, nodes.back().node->id) > 0)
|
||||
if (nodes.size() == SEARCH_NODES && id.xorCmp(nid, nodes.back().node->id) > 0 && node->isExpired(now))
|
||||
return false;
|
||||
|
||||
// Reset search timer if it was empty
|
||||
@@ -624,7 +638,7 @@ Dht::Search::insertNode(std::shared_ptr<Node> node, time_point now, const Blob&
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
auto n = std::find_if(nodes.begin(), nodes.end(), [=,&found](const SearchNode& sn) {
|
||||
auto n = std::find_if(nodes.begin(), nodes.end(), [&](const SearchNode& sn) {
|
||||
if (sn.node == node) {
|
||||
found = true;
|
||||
return true;
|
||||
@@ -632,11 +646,19 @@ Dht::Search::insertNode(std::shared_ptr<Node> node, time_point now, const Blob&
|
||||
return id.xorCmp(nid, sn.node->id) < 0;
|
||||
});
|
||||
if (!found) {
|
||||
if (n == nodes.end() && nodes.size() == SEARCH_NODES)
|
||||
return false;
|
||||
if (nodes.size() == SEARCH_NODES) {
|
||||
if (node->isExpired(now))
|
||||
return false;
|
||||
if (n == nodes.end()) {
|
||||
// search is full, try to remove an expired node
|
||||
if (not removeExpiredNode(now))
|
||||
return false;
|
||||
n = nodes.end();
|
||||
}
|
||||
}
|
||||
n = nodes.insert(n, SearchNode(node));
|
||||
if (nodes.size() > SEARCH_NODES)
|
||||
nodes.pop_back();
|
||||
if (nodes.size() > SEARCH_NODES and not removeExpiredNode(now))
|
||||
nodes.pop_back();
|
||||
expired = false;
|
||||
}
|
||||
if (not token.empty()) {
|
||||
|
||||
Reference in new issue
Block a user