net: Pass time to InactivityChecks fuctions

We run InactivityChecks() for each node everytime poll()/select() every
50ms or so. Rather than calculating the current time once for each node,
just calculate it once and reuse it.
This commit is contained in:
Anthony Towns
2025-12-06 19:36:46 +10:00
parent e68517208b
commit cea443e246
2 changed files with 9 additions and 8 deletions

View File

@@ -2000,16 +2000,15 @@ void CConnman::NotifyNumConnectionsChanged()
}
}
bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const
bool CConnman::ShouldRunInactivityChecks(const CNode& node, std::chrono::microseconds now) const
{
return node.m_connected + m_peer_connect_timeout < now;
}
bool CConnman::InactivityCheck(const CNode& node) const
bool CConnman::InactivityCheck(const CNode& node, std::chrono::microseconds now) const
{
// Tests that see disconnects after using mocktime can start nodes with a
// large timeout. For example, -peertimeout=999999999.
const auto now{GetTime<std::chrono::seconds>()};
const auto last_send{node.m_last_send.load()};
const auto last_recv{node.m_last_recv.load()};
@@ -2033,7 +2032,7 @@ bool CConnman::InactivityCheck(const CNode& node) const
if (now > last_send + TIMEOUT_INTERVAL) {
LogDebug(BCLog::NET,
"socket sending timeout: %is, %s\n", count_seconds(now - last_send),
"socket sending timeout: %is, %s\n", Ticks<std::chrono::seconds>(now - last_send),
node.DisconnectMsg(fLogIPs)
);
return true;
@@ -2041,7 +2040,7 @@ bool CConnman::InactivityCheck(const CNode& node) const
if (now > last_recv + TIMEOUT_INTERVAL) {
LogDebug(BCLog::NET,
"socket receive timeout: %is, %s\n", count_seconds(now - last_recv),
"socket receive timeout: %is, %s\n", Ticks<std::chrono::seconds>(now - last_recv),
node.DisconnectMsg(fLogIPs)
);
return true;
@@ -2123,6 +2122,8 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
{
AssertLockNotHeld(m_total_bytes_sent_mutex);
auto now = GetTime<std::chrono::microseconds>();
for (CNode* pnode : nodes) {
if (m_interrupt_net->interrupted()) {
return;
@@ -2214,7 +2215,7 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
}
}
if (InactivityCheck(*pnode)) pnode->fDisconnect = true;
if (InactivityCheck(*pnode, now)) pnode->fDisconnect = true;
}
}