mirror of
https://github.com/savoirfairelinux/opendht.git
synced 2026-11-08 04:21:51 +00:00
node cache: expire nodes on destructions
Expiring nodes on cache destruction allow to cancel any remaining request before the dht instance is destructed, since Node instances might remain alive after the dht instance is destroyed.
This commit is contained in:
1 parent
6378840d33
commit
41ebbe21d4
2 files changed
+17
No files matched your search
@@ -37,12 +37,15 @@ struct NodeCache {
|
||||
*/
|
||||
void clearBadNodes(sa_family_t family = 0);
|
||||
|
||||
~NodeCache();
|
||||
|
||||
private:
|
||||
class NodeMap : public std::map<InfoHash, std::weak_ptr<Node>> {
|
||||
public:
|
||||
Sp<Node> getNode(const InfoHash& id);
|
||||
Sp<Node> getNode(const InfoHash& id, const SockAddr&, time_point now, bool confirmed, bool client);
|
||||
void clearBadNodes();
|
||||
void setExpired();
|
||||
};
|
||||
|
||||
const NodeMap& cache(sa_family_t af) const { return af == AF_INET ? cache_4 : cache_6; }
|
||||
|
||||
@@ -20,6 +20,12 @@
|
||||
|
||||
namespace dht {
|
||||
|
||||
NodeCache::~NodeCache()
|
||||
{
|
||||
cache_4.setExpired();
|
||||
cache_6.setExpired();
|
||||
}
|
||||
|
||||
Sp<Node>
|
||||
NodeCache::getNode(const InfoHash& id, sa_family_t family) {
|
||||
return cache(family).getNode(id);
|
||||
@@ -116,4 +122,12 @@ NodeCache::NodeMap::clearBadNodes() {
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
NodeCache::NodeMap::setExpired() {
|
||||
for (auto& wn : *this)
|
||||
if (auto n = wn.second.lock())
|
||||
n->setExpired();
|
||||
clear();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user