From 41ebbe21d4123cb44efd14cc6e588073dcc0e7f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Thu, 26 Oct 2017 19:24:28 +0200 Subject: [PATCH] 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. --- include/opendht/node_cache.h | 3 +++ src/node_cache.cpp | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/opendht/node_cache.h b/include/opendht/node_cache.h index 97e62cd1..9b1e2a06 100644 --- a/include/opendht/node_cache.h +++ b/include/opendht/node_cache.h @@ -37,12 +37,15 @@ struct NodeCache { */ void clearBadNodes(sa_family_t family = 0); + ~NodeCache(); + private: class NodeMap : public std::map> { public: Sp getNode(const InfoHash& id); Sp 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; } diff --git a/src/node_cache.cpp b/src/node_cache.cpp index 68b6441b..485b208b 100644 --- a/src/node_cache.cpp +++ b/src/node_cache.cpp @@ -20,6 +20,12 @@ namespace dht { +NodeCache::~NodeCache() +{ + cache_4.setExpired(); + cache_6.setExpired(); +} + Sp 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(); +} + }