From 6c4c656be46960349cd807b27aa7615cffaa3f1e Mon Sep 17 00:00:00 2001 From: Denis Ryabov Date: Sat, 5 Jul 2014 16:13:41 +0400 Subject: [PATCH] faster DHT refresh --- libtorrent/include/libtorrent/kademlia/node.hpp | 1 + libtorrent/src/kademlia/node.cpp | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/libtorrent/include/libtorrent/kademlia/node.hpp b/libtorrent/include/libtorrent/kademlia/node.hpp index d8de0180..5d174084 100644 --- a/libtorrent/include/libtorrent/kademlia/node.hpp +++ b/libtorrent/include/libtorrent/kademlia/node.hpp @@ -300,6 +300,7 @@ private: ptime m_last_tracker_tick; ptime m_next_storage_refresh; + int m_refresh_per_tick; std::pair m_last_refreshed_item; // secret random numbers used to create write tokens diff --git a/libtorrent/src/kademlia/node.cpp b/libtorrent/src/kademlia/node.cpp index 42a6aa72..8712afb7 100644 --- a/libtorrent/src/kademlia/node.cpp +++ b/libtorrent/src/kademlia/node.cpp @@ -579,8 +579,8 @@ bool node_impl::refresh_storage() { (multi && item.local_add_time && item.local_add_time + 60*60*24*2 > time(NULL)) ) { num_refreshable++; - if( refresh_next_item ) { - refresh_next_item = false; + if( refresh_next_item && m_refresh_per_tick ) { + --m_refresh_per_tick; m_last_refreshed_item = std::make_pair(i->first,jIdx); #ifdef TORRENT_DHT_VERBOSE_LOGGING printf("node dht: refreshing storage: [%s,%s,%s]\n", @@ -611,13 +611,16 @@ bool node_impl::refresh_storage() { m_last_refreshed_item = std::make_pair(m_storage_table.begin()->first,0); } - time_duration sleepToRefresh; - if( num_refreshable ) { - sleepToRefresh = minutes(60) / num_refreshable; + const time_duration tickInterval = seconds(5); + const time_duration fullRefreshInterval = minutes(30); + const time_duration sleepInterval = minutes(10); + const time_duration sleepToRefresh = std::min( sleepInterval, fullRefreshInterval / (num_refreshable ? num_refreshable : 1) ); + m_next_storage_refresh = time_now() + sleepToRefresh; + if( sleepToRefresh > tickInterval ) { + m_refresh_per_tick = 1; } else { - sleepToRefresh = minutes(10); + m_refresh_per_tick = tickInterval.diff/sleepToRefresh.diff; } - m_next_storage_refresh = time_now() + sleepToRefresh; /* printf("node dht: next storage refresh in %s\n",