---
title: llarp::PeerDb
summary: Maintains a database of stats collected about the connections with our Service Node peers. 

---

# llarp::PeerDb



Maintains a database of stats collected about the connections with our Service Node peers.  [More...](#detailed-description)


`#include <peer_db.hpp>`

## Public Functions

|                | Name           |
| -------------- | -------------- |
| | **[PeerDb](Classes/structllarp_1_1PeerDb.md#function-peerdb)**()<br>Constructor.  |
| void | **[loadDatabase](Classes/structllarp_1_1PeerDb.md#function-loaddatabase)**(std::optional< fs::path > file)<br>Loads the database from disk using the provided filepath.  |
| void | **[flushDatabase](Classes/structllarp_1_1PeerDb.md#function-flushdatabase)**()<br>Flushes the database.  |
| void | **[accumulatePeerStats](Classes/structllarp_1_1PeerDb.md#function-accumulatepeerstats)**(const [RouterID](Classes/structllarp_1_1RouterID.md) & routerId, const [PeerStats](Classes/structllarp_1_1PeerStats.md) & delta)<br>Add the given stats to the cummulative stats for the given peer.  |
| void | **[modifyPeerStats](Classes/structllarp_1_1PeerDb.md#function-modifypeerstats)**(const [RouterID](Classes/structllarp_1_1RouterID.md) & routerId, std::function< void([PeerStats](Classes/structllarp_1_1PeerStats.md) &)> callback)<br>Allows write-access to the stats for a given peer while appropriate mutex lock is held.  |
| std::optional< [PeerStats](Classes/structllarp_1_1PeerStats.md) > | **[getCurrentPeerStats](Classes/structllarp_1_1PeerDb.md#function-getcurrentpeerstats)**(const [RouterID](Classes/structllarp_1_1RouterID.md) & routerId) const<br>Provides a snapshot of the most recent [PeerStats]() we have for the given peer.  |
| std::vector< [PeerStats](Classes/structllarp_1_1PeerStats.md) > | **[listAllPeerStats](Classes/structllarp_1_1PeerDb.md#function-listallpeerstats)**() const<br>Lists all peer stats.  |
| std::vector< [PeerStats](Classes/structllarp_1_1PeerStats.md) > | **[listPeerStats](Classes/structllarp_1_1PeerDb.md#function-listpeerstats)**(const std::vector< [RouterID](Classes/structllarp_1_1RouterID.md) > & ids) const<br>Lists specific peer stats.  |
| void | **[handleGossipedRC](Classes/structllarp_1_1PeerDb.md#function-handlegossipedrc)**(const [RouterContact](Classes/structllarp_1_1RouterContact.md) & rc, llarp_time_t now =[time_now_ms](Namespaces/namespacellarp.md#function-time-now-ms)())<br>Handles a new gossiped RC, updating stats as needed.  |
| void | **[configure](Classes/structllarp_1_1PeerDb.md#function-configure)**(const [RouterConfig](Classes/structllarp_1_1RouterConfig.md) & routerConfig)<br>Configures the [PeerDb](Classes/structllarp_1_1PeerDb.md) based on [RouterConfig](Classes/structllarp_1_1RouterConfig.md).  |
| bool | **[shouldFlush](Classes/structllarp_1_1PeerDb.md#function-shouldflush)**(llarp_time_t now)<br>Returns whether or not we should flush, as determined by the last time we flushed and the configured flush interval.  |
| util::StatusObject | **[ExtractStatus](Classes/structllarp_1_1PeerDb.md#function-extractstatus)**() const<br>Get JSON status for API.  |

## Detailed Description

```cpp
struct llarp::PeerDb;
```

Maintains a database of stats collected about the connections with our Service Node peers. 

This uses a sqlite3 database behind the scenes as persistance, but this database is periodically flushed to, meaning that it will become stale as [PeerDb](Classes/structllarp_1_1PeerDb.md) accumulates stats without a flush. 

## Public Functions Documentation

### function PeerDb

```cpp
PeerDb()
```

Constructor. 

### function loadDatabase

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

Loads the database from disk using the provided filepath. 

**Parameters**: 

  * **file** is an optional file which doesn't have to exist but must be writable, if a value is provided. If no value is provided, the database will be memory-backed. 


**Exceptions**: 

  * **if** sqlite_orm/sqlite3 is unable to open or create a database at the given file 


If the file is equal to `std::nullopt`, the database will be loaded into memory (useful for testing).

This must be called prior to calling [flushDatabase()](Classes/structllarp_1_1PeerDb.md#function-flushdatabase), and will truncate any existing data.

This is a blocking call, both in the sense that it blocks on disk/database I/O and that it will sit on a mutex while the database is loaded.


### function flushDatabase

```cpp
void flushDatabase()
```

Flushes the database. 

**Exceptions**: 

  * **if** the database could not be written to (esp. if [loadDatabase()](Classes/structllarp_1_1PeerDb.md#function-loaddatabase) has not been called) 


Must be called after [loadDatabase()](Classes/structllarp_1_1PeerDb.md#function-loaddatabase). This call will block during I/O and should be called in an appropriate threading context. However, it will make a temporary copy of the peer stats so as to avoid sitting on a mutex lock during disk I/O.


### function accumulatePeerStats

```cpp
void accumulatePeerStats(
    const RouterID & routerId,
    const PeerStats & delta
)
```

Add the given stats to the cummulative stats for the given peer. 

**Parameters**: 

  * **routerId** is the id of the router whose stats should be modified. 
  * **delta** is the stats to add to the existing stats 


For cummulative stats, the stats are added together; for watermark stats, the max is kept.

This is intended to be used in the following pattern:

1) Initialize an empty [PeerStats](Classes/structllarp_1_1PeerStats.md) 2) Collect relevant stats 3) Call [accumulatePeerStats()](Classes/structllarp_1_1PeerDb.md#function-accumulatepeerstats) with the stats 4) Reset the stats to 0 5) <Repeat 2-4 periodically>


### function modifyPeerStats

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

Allows write-access to the stats for a given peer while appropriate mutex lock is held. 

**Parameters**: 

  * **routerId** is the id of the router whose stats should be modified. 
  * **callback** is a function which will be called immediately with mutex 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.


### function getCurrentPeerStats

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

Provides a snapshot of the most recent [PeerStats]() we have for the given peer. 

**Parameters**: 

  * **routerId** is the [RouterID](Classes/structllarp_1_1RouterID.md) of the requested peer 


**Return**: a copy of the most recent peer stats or an empty one if no such peer is known 

If we don't have any stats for the peer, std::nullopt


### function listAllPeerStats

```cpp
std::vector< PeerStats > listAllPeerStats() const
```

Lists all peer stats. 

**Return**: a list of all [PeerStats](Classes/structllarp_1_1PeerStats.md) we have maintained 

This essentially dumps the database into a list of [PeerStats](Classes/structllarp_1_1PeerStats.md) objects.

Note that this avoids disk I/O by copying from our cached map of peers.


### function listPeerStats

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

Lists specific peer stats. 

**Parameters**: 

  * **peers** is list of RouterIDs which are desired 


**Return**: a list of the requested peers. Peers not found will be omitted. 

### function handleGossipedRC

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

Handles a new gossiped RC, updating stats as needed. 

**Parameters**: 

  * **rc** is the [RouterContact](Classes/structllarp_1_1RouterContact.md) to handle 
  * **now** is an optional time representing the current time 


The database tracks the last advertised update time, so it knows whether this is a new RC or not.

The given RC is assumed to be valid.


### function configure

```cpp
void configure(
    const RouterConfig & routerConfig
)
```

Configures the [PeerDb](Classes/structllarp_1_1PeerDb.md) based on [RouterConfig](Classes/structllarp_1_1RouterConfig.md). 

**Parameters**: 

  * **routerConfig** 


### function shouldFlush

```cpp
bool shouldFlush(
    llarp_time_t now
)
```

Returns whether or not we should flush, as determined by the last time we flushed and the configured flush interval. 

**Parameters**: 

  * **now** is the current[-ish] time 


### function ExtractStatus

```cpp
util::StatusObject ExtractStatus() const
```

Get JSON status for API. 

**Return**: JSON object representing our current status 

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

Updated on 2022-10-17 at 13:14:48 +0000