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 committed 2025-12-07 00:36:24 +10:00
1 parent e68517208b
commit cea443e246
2 files changed
+9 -8

No files matched your search

+7 -6
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; 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 // Tests that see disconnects after using mocktime can start nodes with a
// large timeout. For example, -peertimeout=999999999. // 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_send{node.m_last_send.load()};
const auto last_recv{node.m_last_recv.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) { if (now > last_send + TIMEOUT_INTERVAL) {
LogDebug(BCLog::NET, 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) node.DisconnectMsg(fLogIPs)
); );
return true; return true;
@@ -2041,7 +2040,7 @@ bool CConnman::InactivityCheck(const CNode& node) const
if (now > last_recv + TIMEOUT_INTERVAL) { if (now > last_recv + TIMEOUT_INTERVAL) {
LogDebug(BCLog::NET, 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) node.DisconnectMsg(fLogIPs)
); );
return true; return true;
@@ -2123,6 +2122,8 @@ void CConnman::SocketHandlerConnected(const std::vector<CNode*>& nodes,
{ {
AssertLockNotHeld(m_total_bytes_sent_mutex); AssertLockNotHeld(m_total_bytes_sent_mutex);
auto now = GetTime<std::chrono::microseconds>();
for (CNode* pnode : nodes) { for (CNode* pnode : nodes) {
if (m_interrupt_net->interrupted()) { if (m_interrupt_net->interrupted()) {
return; 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;
} }
} }
+2 -2
View File
@@ -1314,7 +1314,7 @@ public:
void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc); void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);
/** Return true if we should disconnect the peer for failing an inactivity check. */ /** Return true if we should disconnect the peer for failing an inactivity check. */
bool ShouldRunInactivityChecks(const CNode& node, std::chrono::seconds now) const; bool ShouldRunInactivityChecks(const CNode& node, std::chrono::microseconds now) const;
bool MultipleManualOrFullOutboundConns(Network net) const EXCLUSIVE_LOCKS_REQUIRED(m_nodes_mutex); bool MultipleManualOrFullOutboundConns(Network net) const EXCLUSIVE_LOCKS_REQUIRED(m_nodes_mutex);
@@ -1364,7 +1364,7 @@ private:
void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex, !m_nodes_mutex); void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex, !m_nodes_mutex);
void NotifyNumConnectionsChanged(); void NotifyNumConnectionsChanged();
/** Return true if the peer is inactive and should be disconnected. */ /** Return true if the peer is inactive and should be disconnected. */
bool InactivityCheck(const CNode& node) const; bool InactivityCheck(const CNode& node, std::chrono::microseconds now) const;
/** /**
* Generate a collection of sockets to check for IO readiness. * Generate a collection of sockets to check for IO readiness.