20 #include <ripple/basics/Log.h>
21 #include <ripple/peerfinder/impl/Bootcache.h>
22 #include <ripple/peerfinder/impl/Tuning.h>
23 #include <ripple/peerfinder/impl/iosformat.h>
26 namespace PeerFinder {
32 , m_whenUpdate(m_clock.now())
33 , m_needsUpdate(false)
48 Bootcache::map_type::size_type
94 this->m_map.insert(value_type(endpoint, valence)));
97 JLOG(this->m_journal.error())
98 << beast::leftw(18) <<
"Bootcache discard " << endpoint;
104 JLOG(m_journal.info()) <<
beast::leftw(18) <<
"Bootcache loaded " << n
105 << ((n > 1) ?
" addresses" :
" address");
113 auto const result(m_map.insert(
value_type(endpoint, 0)));
116 JLOG(m_journal.trace())
121 return result.second;
127 auto result(m_map.insert(
value_type(endpoint, staticValence)));
129 if (!result.second && (result.first->right.valence() < staticValence))
132 m_map.erase(result.first);
133 result = m_map.insert(
value_type(endpoint, staticValence));
138 JLOG(m_journal.trace())
143 return result.second;
149 auto result(m_map.insert(
value_type(endpoint, 1)));
156 Entry entry(result.first->right);
160 m_map.erase(result.first);
161 result = m_map.insert(
value_type(endpoint, entry));
162 assert(result.second);
164 Entry const& entry(result.first->right);
165 JLOG(m_journal.info()) <<
beast::leftw(18) <<
"Bootcache connect "
166 << endpoint <<
" with " << entry.
valence()
167 << ((entry.
valence() > 1) ?
" successes"
175 auto result(m_map.insert(
value_type(endpoint, -1)));
182 Entry entry(result.first->right);
186 m_map.erase(result.first);
187 result = m_map.insert(
value_type(endpoint, entry));
188 assert(result.second);
190 Entry const& entry(result.first->right);
191 auto const n(std::abs(entry.
valence()));
192 JLOG(m_journal.debug())
193 <<
beast::leftw(18) <<
"Bootcache failed " << endpoint <<
" with " << n
194 << ((n > 1) ?
" attempts" :
" attempt");
199 Bootcache::periodicActivity()
210 for (
auto iter = m_map.right.begin(); iter != m_map.right.end(); ++iter)
213 entry[
"endpoint"] = iter->get_left().to_string();
214 entry[
"valence"] =
std::int32_t(iter->get_right().valence());
222 if (size() <= Tuning::bootcacheSize)
226 auto count((size() * Tuning::bootcachePrunePercent) / 100);
227 decltype(count) pruned(0);
232 for (
auto iter(m_map.right.end());
233 count-- > 0 && iter != m_map.right.begin();
238 Entry const& entry(iter->get_right());
239 JLOG(m_journal.trace())
241 <<
" at valence " << entry.
valence();
242 iter = m_map.right.erase(iter);
245 JLOG(m_journal.debug()) <<
beast::leftw(18) <<
"Bootcache pruned " << pruned
257 for (
auto const& e : m_map)
261 se.
valence = e.get_right().valence();
266 m_needsUpdate =
false;
267 m_whenUpdate = m_clock.now() + Tuning::bootcacheCooldownTime;
272 Bootcache::checkUpdate()
274 if (m_needsUpdate && m_whenUpdate < m_clock.now())
280 Bootcache::flagForUpdate()
282 m_needsUpdate =
true;