---
title: llarp/peerstats/peer_db.hpp

---

# llarp/peerstats/peer_db.hpp



## Namespaces

| Name           |
| -------------- |
| **[llarp](Namespaces/namespacellarp.md)** <br>[crypto.hpp] |

## Classes

|                | Name           |
| -------------- | -------------- |
| struct | **[llarp::PeerDb](Classes/structllarp_1_1PeerDb.md)** <br>Maintains a database of stats collected about the connections with our Service Node peers.  |




## Source code

```cpp
#pragma once

#include <filesystem>
#include <functional>
#include <unordered_map>

#include <llarp/util/fs.hpp>
#include <llarp/config/config.hpp>
#include <llarp/router_id.hpp>
#include <llarp/util/time.hpp>
#include <llarp/util/status.hpp>
#include "types.hpp"
#ifdef LOKINET_PEERSTATS_BACKEND
#include "orm.hpp"
#endif

namespace llarp
{
  struct PeerDb
  {
    PeerDb();

    void
    loadDatabase(std::optional<fs::path> file);

    void
    flushDatabase();

    void
    accumulatePeerStats(const RouterID& routerId, const PeerStats& delta);

    void
    modifyPeerStats(const RouterID& routerId, std::function<void(PeerStats&)> callback);

    std::optional<PeerStats>
    getCurrentPeerStats(const RouterID& routerId) const;

    std::vector<PeerStats>
    listAllPeerStats() const;

    std::vector<PeerStats>
    listPeerStats(const std::vector<RouterID>& ids) const;

    void
    handleGossipedRC(const RouterContact& rc, llarp_time_t now = time_now_ms());

    void
    configure(const RouterConfig& routerConfig);

    bool
    shouldFlush(llarp_time_t now);

    util::StatusObject
    ExtractStatus() const;

#ifdef LOKINET_PEERSTATS_BACKEND
   private:
    std::unordered_map<RouterID, PeerStats> m_peerStats;
    mutable std::mutex m_statsLock;

    std::unique_ptr<PeerDbStorage> m_storage;

    std::atomic<llarp_time_t> m_lastFlush;
#endif
  };

}  // namespace llarp
```


-------------------------------

Updated on 2023-01-23 at 22:05:56 +0000
