Skip to content

Commit bf100f8

Browse files
committed
[net] Cleanup InactivityChecks() and add commenting about time
Also clean up and better comment the function. InactivityChecks() uses a mixture of (non-mockable) system time and mockable time. Make sure that's well documented. Despite being marked as const in CConnman before this commit, the function did mutate the state of the passed in CNode, which is contained in vNodes, which is a member of CConnman. To make the function truly const in CConnman and all its data, instead make InactivityChecks() a pure function, return whether the peer should be disconnected, and let the calling function (SocketHandler()) update the CNode object. Also make the CNode& argument const.
1 parent 06fa85c commit bf100f8

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

src/net.cpp

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,37 +1216,47 @@ void CConnman::NotifyNumConnectionsChanged()
12161216
}
12171217
}
12181218

1219-
void CConnman::InactivityCheck(CNode& node) const
1219+
bool CConnman::InactivityCheck(const CNode& node) const
12201220
{
1221-
int64_t nTime = GetSystemTimeInSeconds();
1222-
if (nTime - node.nTimeConnected > m_peer_connect_timeout)
1223-
{
1224-
if (node.nLastRecv == 0 || node.nLastSend == 0)
1225-
{
1226-
LogPrint(BCLog::NET, "socket no message in first %i seconds, %d %d from %d\n", m_peer_connect_timeout, node.nLastRecv != 0, node.nLastSend != 0, node.GetId());
1227-
node.fDisconnect = true;
1228-
}
1229-
else if (nTime - node.nLastSend > TIMEOUT_INTERVAL)
1230-
{
1231-
LogPrintf("socket sending timeout: %is\n", nTime - node.nLastSend);
1232-
node.fDisconnect = true;
1233-
}
1234-
else if (nTime - node.nLastRecv > TIMEOUT_INTERVAL)
1235-
{
1236-
LogPrintf("socket receive timeout: %is\n", nTime - node.nLastRecv);
1237-
node.fDisconnect = true;
1238-
}
1239-
else if (node.nPingNonceSent && node.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL} < GetTime<std::chrono::microseconds>())
1240-
{
1241-
LogPrintf("ping timeout: %fs\n", 0.000001 * count_microseconds(GetTime<std::chrono::microseconds>() - node.m_ping_start.load()));
1242-
node.fDisconnect = true;
1243-
}
1244-
else if (!node.fSuccessfullyConnected)
1245-
{
1246-
LogPrint(BCLog::NET, "version handshake timeout from %d\n", node.GetId());
1247-
node.fDisconnect = true;
1248-
}
1221+
// Use non-mockable system time (otherwise these timers will pop when we
1222+
// use setmocktime in the tests).
1223+
int64_t now = GetSystemTimeInSeconds();
1224+
1225+
if (now <= node.nTimeConnected + m_peer_connect_timeout) {
1226+
// Only run inactivity checks if the peer has been connected longer
1227+
// than m_peer_connect_timeout.
1228+
return false;
1229+
}
1230+
1231+
if (node.nLastRecv == 0 || node.nLastSend == 0) {
1232+
LogPrint(BCLog::NET, "socket no message in first %i seconds, %d %d from %d\n", m_peer_connect_timeout, node.nLastRecv != 0, node.nLastSend != 0, node.GetId());
1233+
return true;
1234+
}
1235+
1236+
if (now > node.nLastSend + TIMEOUT_INTERVAL) {
1237+
LogPrintf("socket sending timeout: %is\n", now - node.nLastSend);
1238+
return true;
12491239
}
1240+
1241+
if (now > node.nLastRecv + TIMEOUT_INTERVAL) {
1242+
LogPrintf("socket receive timeout: %is\n", now - node.nLastRecv);
1243+
return true;
1244+
}
1245+
1246+
if (node.nPingNonceSent && node.m_ping_start.load() + std::chrono::seconds{TIMEOUT_INTERVAL} < GetTime<std::chrono::microseconds>()) {
1247+
// We use mockable time for ping timeouts. This means that setmocktime
1248+
// may cause pings to time out for peers that have been connected for
1249+
// longer than m_peer_connect_timeout.
1250+
LogPrintf("ping timeout: %fs\n", 0.000001 * count_microseconds(GetTime<std::chrono::microseconds>() - node.m_ping_start.load()));
1251+
return true;
1252+
}
1253+
1254+
if (!node.fSuccessfullyConnected) {
1255+
LogPrint(BCLog::NET, "version handshake timeout from %d\n", node.GetId());
1256+
return true;
1257+
}
1258+
1259+
return false;
12501260
}
12511261

12521262
bool CConnman::GenerateSelectSet(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set)
@@ -1522,7 +1532,7 @@ void CConnman::SocketHandler()
15221532
if (bytes_sent) RecordBytesSent(bytes_sent);
15231533
}
15241534

1525-
InactivityCheck(*pnode);
1535+
if (InactivityCheck(*pnode)) pnode->fDisconnect = true;
15261536
}
15271537
{
15281538
LOCK(cs_vNodes);

src/net.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,8 @@ class CConnman
10441044
void AcceptConnection(const ListenSocket& hListenSocket);
10451045
void DisconnectNodes();
10461046
void NotifyNumConnectionsChanged();
1047-
void InactivityCheck(CNode& node) const;
1047+
/** Return true if the peer is inactive and should be disconnected. */
1048+
bool InactivityCheck(const CNode& node) const;
10481049
bool GenerateSelectSet(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set);
10491050
void SocketEvents(std::set<SOCKET> &recv_set, std::set<SOCKET> &send_set, std::set<SOCKET> &error_set);
10501051
void SocketHandler();

0 commit comments

Comments
 (0)