From 79ef324cd35552ed37ea221c63d35291fb0308c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Sat, 6 Dec 2014 14:22:54 -0500 Subject: [PATCH] avoid duplicating hard-coded constants --- include/opendht/dht.h | 12 ++++++++--- src/dht.cpp | 50 +++++++++++++++++++++---------------------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/include/opendht/dht.h b/include/opendht/dht.h index 68b185d1..b3643462 100644 --- a/include/opendht/dht.h +++ b/include/opendht/dht.h @@ -214,6 +214,9 @@ private: /* The maximum number of searches we keep data about. */ static const unsigned MAX_SEARCHES {1024}; + /* Time for a request to timeout */ + static const time_t MAX_RESPONSE_TIME {15}; + /* A search with no nodes will timeout after this time. */ static const time_t SEARCH_TIMEOUT {10}; @@ -224,6 +227,9 @@ private: /* The time after which we consider a search to be expirable. */ static const time_t SEARCH_EXPIRE_TIME {62 * 60}; + /* The time after which we consider a node to be expirable. */ + static const time_t NODE_EXPIRE_TIME {15 * 60}; + /* The maximum number of nodes that we snub. There is probably little reason to increase this value. */ static const unsigned BLACKLISTED_MAX {10}; @@ -311,7 +317,7 @@ private: * Can we use this node to listen/announce ? */ bool isSynced(time_t now) const { - return /*pinged < 3 && replied &&*/ reply_time > now - 15 * 60; + return /*pinged < 3 && replied &&*/ reply_time > now - NODE_EXPIRE_TIME; } time_t getAnnounceTime(AnnounceStatusMap::const_iterator ack, const ValueType& type) const { @@ -323,8 +329,8 @@ private: return getAnnounceTime(acked.find(vid), type); } time_t getListenTime() const { - time_t min_t = listenStatus.request_time + 15; - return listenStatus.reply_time ? std::max(listenStatus.reply_time + 15*60, min_t) : min_t; + time_t min_t = listenStatus.request_time + MAX_RESPONSE_TIME; + return listenStatus.reply_time ? std::max(listenStatus.reply_time + NODE_EXPIRE_TIME, min_t) : min_t; } InfoHash id {}; diff --git a/src/dht.cpp b/src/dht.cpp index ed448679..7ecbf548 100644 --- a/src/dht.cpp +++ b/src/dht.cpp @@ -281,7 +281,7 @@ Dht::Node::isGood(time_t now) const return pinged <= 2 && reply_time >= now - 7200 && - time >= now - 15 * 60; + time >= now - NODE_EXPIRE_TIME; } /* Every bucket caches the address of a likely node. Ping it. */ @@ -401,25 +401,25 @@ Dht::newNode(const InfoHash& id, const sockaddr *sa, socklen_t salen, int confir for (auto& n : b->nodes) { if (n.id != id) continue; - if (confirm || n.time < now.tv_sec - 15 * 60) { - /* Known node. Update stuff. */ + /* Known node. Update stuff. */ + + if (confirm || n.time < now.tv_sec - NODE_EXPIRE_TIME) { memcpy((sockaddr*)&n.ss, sa, salen); - if (confirm) - n.time = now.tv_sec; - if (confirm >= 2) { - n.reply_time = now.tv_sec; - n.pinged = 0; - n.pinged_time = 0; - } - if (confirm) { - /* If this node existed in searches but was expired, give it another chance. */ - for (auto& s : searches) { - if (s.af != sa->sa_family) continue; - if (s.insertNode(id, sa, salen, now.tv_sec, true)) { - time_t tm = s.getNextStepTime(types, now.tv_sec); - if (tm != 0 && (search_time == 0 || search_time > tm)) - search_time = tm; - } + } + if (confirm >= 2) { + n.reply_time = now.tv_sec; + n.pinged = 0; + n.pinged_time = 0; + } + if (confirm) { + n.time = now.tv_sec; + /* If this node existed in searches but was expired, give it another chance. */ + for (auto& s : searches) { + if (s.af != sa->sa_family) continue; + if (s.insertNode(id, sa, salen, now.tv_sec, true)) { + time_t tm = s.getNextStepTime(types, now.tv_sec); + if (tm != 0 && (search_time == 0 || search_time > tm)) + search_time = tm; } } } @@ -447,7 +447,7 @@ Dht::newNode(const InfoHash& id, const sockaddr *sa, socklen_t salen, int confir /* First, try to get rid of a known-bad node. */ for (auto& n : b->nodes) { - if (n.pinged < 3 || n.pinged_time >= now.tv_sec - 15) + if (n.pinged < 3 || n.pinged_time >= now.tv_sec - MAX_RESPONSE_TIME) continue; n.id = id; memcpy((sockaddr*)&n.ss, sa, salen); @@ -468,7 +468,7 @@ Dht::newNode(const InfoHash& id, const sockaddr *sa, socklen_t salen, int confir of bad nodes fast. */ if (!n.isGood(now.tv_sec)) { dubious = true; - if (n.pinged_time < now.tv_sec - 15) { + if (n.pinged_time < now.tv_sec - MAX_RESPONSE_TIME) { DHT_DEBUG("Sending ping to dubious node."); sendPing((sockaddr*)&n.ss, n.sslen, TransId {TransPrefix::PING}); n.pinged++; @@ -610,13 +610,13 @@ Dht::searchSendGetValues(Search& sr, SearchNode *n) time_t t = now.tv_sec; if (!n) { auto ni = std::find_if(sr.nodes.begin(), sr.nodes.end(), [t](const SearchNode& sn) { - return sn.pinged < 3 && !sn.isSynced(t) && sn.request_time < t - 15; + return sn.pinged < 3 && !sn.isSynced(t) && sn.request_time < t - MAX_RESPONSE_TIME; }); if (ni != sr.nodes.end()) n = &*ni; } - if (!n || n->pinged >= 3 || n->isSynced(t) || n->request_time >= t - 15) + if (!n || n->pinged >= 3 || n->isSynced(t) || n->request_time >= t - MAX_RESPONSE_TIME) return false; { @@ -647,7 +647,7 @@ Dht::searchStep(Search& sr) if (sr.step_time == 0) sr.step_time = now.tv_sec; if (now.tv_sec - sr.step_time >= SEARCH_TIMEOUT) { - DHT_WARN("Search timed out."); + DHT_WARN("Search IPv%c %s timed out.", sr.af == AF_INET ? '4' : '6', sr.id.toString().c_str()); sr.step_time = now.tv_sec; sr.callbacks.clear(); if (sr.done_callback) { @@ -1325,7 +1325,7 @@ Dht::expireStorage() std::partition(i->listeners.begin(), i->listeners.end(), [&](const Listener& l) { - bool expired = l.time + 15*60 < now.tv_sec; + bool expired = l.time + NODE_EXPIRE_TIME < now.tv_sec; if (expired) DHT_DEBUG("Discarding expired listener %s", l.id.toString().c_str()); // return false if the element should be removed