Skip to content
This repository was archived by the owner on Oct 28, 2021. It is now read-only.

Commit a270d31

Browse files
committed
Log active peer count and peer list
1 parent 5eb6315 commit a270d31

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

libp2p/Common.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,16 @@ inline std::ostream& operator<<(std::ostream& _strm, NodeID const& _id)
267267
return _strm;
268268
}
269269

270+
inline boost::log::formatting_ostream& operator<<(
271+
boost::log::formatting_ostream& _strm, PeerSessionInfo const& _peerSessionInfo)
272+
{
273+
_strm << _peerSessionInfo.id << "|" << _peerSessionInfo.clientVersion << "|"
274+
<< _peerSessionInfo.host << "|" << _peerSessionInfo.port << "|";
275+
for (auto cap : _peerSessionInfo.caps)
276+
_strm << "(" << cap.first << "," << cap.second << ")";
277+
return _strm;
278+
}
279+
270280
/// Simple stream output for a NodeIPEndpoint.
271281
std::ostream& operator<<(std::ostream& _out, NodeIPEndpoint const& _ep);
272282

libp2p/Host.cpp

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ constexpr chrono::seconds c_keepAliveTimeOut{1};
3434

3535
/// Interval which m_runTimer is run when network is connected.
3636
constexpr chrono::milliseconds c_runTimerInterval{100};
37+
38+
/// Interval at which active peer info is logged
39+
constexpr chrono::seconds c_logActivePeersInterval{30};
3740
} // namespace
3841

3942
HostNodeTableHandler::HostNodeTableHandler(Host& _host): m_host(_host) {}
@@ -95,6 +98,7 @@ Host::Host(
9598
m_alias{_secretAndENR.first},
9699
m_enr{_secretAndENR.second},
97100
m_lastPing(chrono::steady_clock::time_point::min()),
101+
m_lastPeerLogMessage(chrono::steady_clock::time_point::min()),
98102
m_capabilityHost(createCapabilityHost(*this))
99103
{
100104
cnetnote << "Id: " << id();
@@ -840,15 +844,32 @@ void Host::keepAlivePeers()
840844
if (!m_run || chrono::steady_clock::now() - c_keepAliveInterval < m_lastPing)
841845
return;
842846

847+
bool logActivePeers =
848+
chrono::steady_clock::now() - c_logActivePeersInterval > m_lastPeerLogMessage;
849+
std::vector<PeerSessionInfo> peerSessionInfos;
843850
RecursiveGuard l(x_sessions);
844-
for (auto it = m_sessions.begin(); it != m_sessions.end();)
845-
if (auto p = it->second.lock())
851+
{
852+
for (auto it = m_sessions.begin(); it != m_sessions.end();)
853+
if (auto p = it->second.lock())
854+
{
855+
p->ping();
856+
++it;
857+
if (logActivePeers)
858+
peerSessionInfos.push_back(p->info());
859+
}
860+
else
861+
it = m_sessions.erase(it);
862+
}
863+
864+
if (logActivePeers)
865+
{
866+
LOG(m_logger) << "Active peers: " << peerSessionInfos.size();
867+
for (auto const& peerInfo : peerSessionInfos)
846868
{
847-
p->ping();
848-
++it;
869+
LOG(m_detailsLogger) << "Peer: " << peerInfo;
849870
}
850-
else
851-
it = m_sessions.erase(it);
871+
m_lastPeerLogMessage = chrono::steady_clock::now();
872+
}
852873

853874
m_lastPing = chrono::steady_clock::now();
854875
}

libp2p/Host.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,12 @@ class Host: public Worker
371371

372372
std::shared_ptr<CapabilityHostFace> m_capabilityHost;
373373

374+
/// When the last "active peers" message was logged - used to throttle
375+
/// logging to once every c_logActivePeersInterval seconds
376+
std::chrono::steady_clock::time_point m_lastPeerLogMessage;
377+
374378
Logger m_logger{createLogger(VerbosityDebug, "net")};
379+
Logger m_detailsLogger{createLogger(VerbosityTrace, "net")};
375380
};
376381

377382
}

0 commit comments

Comments
 (0)