mirror of
https://github.com/oxen-io/lokinet
synced 2023-12-14 06:53:00 +01:00
Add PeerDb::modifyPeerStats()
This commit is contained in:
parent
4f4192e272
commit
7109ddc951
3 changed files with 51 additions and 1 deletions
|
@ -100,6 +100,16 @@ namespace llarp
|
|||
itr->second += delta;
|
||||
}
|
||||
|
||||
void
|
||||
PeerDb::modifyPeerStats(const RouterID& routerId, std::function<void(PeerStats&)> callback)
|
||||
{
|
||||
std::lock_guard gaurd(m_statsLock);
|
||||
|
||||
PeerStats& stats = m_peerStats[routerId];
|
||||
stats.routerId = routerId.ToString();
|
||||
callback(stats);
|
||||
}
|
||||
|
||||
std::optional<PeerStats>
|
||||
PeerDb::getCurrentPeerStats(const RouterID& routerId) const
|
||||
{
|
||||
|
@ -125,7 +135,7 @@ namespace llarp
|
|||
bool
|
||||
PeerDb::shouldFlush(llarp_time_t now)
|
||||
{
|
||||
static constexpr llarp_time_t TargetFlushInterval = 30s;
|
||||
constexpr llarp_time_t TargetFlushInterval = 30s;
|
||||
return (now - m_lastFlush.load() >= TargetFlushInterval);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <sqlite_orm/sqlite_orm.h>
|
||||
|
@ -54,9 +55,24 @@ namespace llarp
|
|||
/// 3) Call accumulatePeerStats() with the stats
|
||||
/// 4) Reset the stats to 0
|
||||
/// 5) <Repeat 2-4 periodically>
|
||||
///
|
||||
/// @param routerId is the id of the router whose stats should be modified.
|
||||
/// @param delta is the stats to add to the existing stats
|
||||
void
|
||||
accumulatePeerStats(const RouterID& routerId, const PeerStats& delta);
|
||||
|
||||
/// Allows write-access to the stats for a given peer while appropriate mutex lock is held. This
|
||||
/// is an alternative means of incrementing peer stats that is suitable for one-off
|
||||
/// modifications.
|
||||
///
|
||||
/// Note that this holds m_statsLock during the callback invocation, so the callback should
|
||||
/// return as quickly as possible.
|
||||
///
|
||||
/// @param routerId is the id of the router whose stats should be modified.
|
||||
/// @param callback is a function which will be called immediately with mutex held
|
||||
void
|
||||
modifyPeerStats(const RouterID& routerId, std::function<void(PeerStats&)> callback);
|
||||
|
||||
/// Provides a snapshot of the most recent PeerStats we have for the given peer. If we don't
|
||||
/// have any stats for the peer, std::nullopt
|
||||
///
|
||||
|
|
|
@ -87,3 +87,27 @@ TEST_CASE("Test PeerDb file-backed database reloads properly", "[PeerDb]")
|
|||
|
||||
fs::remove(filename);
|
||||
}
|
||||
|
||||
TEST_CASE("Test PeerDb modifyPeerStats", "[PeerDb]")
|
||||
{
|
||||
const llarp::RouterID id = llarp::test::makeBuf<llarp::RouterID>(0xF2);
|
||||
|
||||
int numTimesCalled = 0;
|
||||
|
||||
llarp::PeerDb db;
|
||||
db.loadDatabase(std::nullopt);
|
||||
|
||||
db.modifyPeerStats(id, [&](llarp::PeerStats& stats) {
|
||||
numTimesCalled++;
|
||||
|
||||
stats.numPathBuilds += 42;
|
||||
});
|
||||
|
||||
db.flushDatabase();
|
||||
|
||||
CHECK(numTimesCalled == 1);
|
||||
|
||||
auto stats = db.getCurrentPeerStats(id);
|
||||
CHECK(stats.has_value());
|
||||
CHECK(stats.value().numPathBuilds == 42);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue