mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-31 10:41:08 +00:00
Merge bitcoin/bitcoin#34025: net: Waste less time in socket handling
5f5c1ea01955d277581f6c2acbeb982949088960 net: Cache -capturemessages setting (Anthony Towns)
cea443e246185c0aa89a8b5dd9f78f6ab09af523 net: Pass time to InactivityChecks fuctions (Anthony Towns)
Pull request description:
Cuts out some wasted time in net socket handling. First, only calculates the current time once every 50ms, rather than once for each peer, which given we only care about second-level precision seems more than adequate. Second, caches the value of the `-capturemessages` setting in `CConnman` rather than re-evaluating it every time we invoke `PushMessaage`.
ACKs for top commit:
maflcko:
review ACK 5f5c1ea01955d277581f6c2acbeb982949088960 🏣
vasild:
ACK 5f5c1ea01955d277581f6c2acbeb982949088960
sedited:
ACK 5f5c1ea01955d277581f6c2acbeb982949088960
mzumsande:
ACK 5f5c1ea01955d277581f6c2acbeb982949088960
Tree-SHA512: 0194143a3a4481c6355ac9eab27ce6ae4bed5db1d483ba5d06288dd92f195ccb9f0f055a9eb9d7e16e9bbf72f145eca1ff17c6700ee9aa42730103a8f047b32c
This commit is contained in:
commit
597b8be223
@ -2043,6 +2043,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||
connOptions.m_peer_connect_timeout = peer_connect_timeout;
|
||||
connOptions.whitelist_forcerelay = args.GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY);
|
||||
connOptions.whitelist_relay = args.GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY);
|
||||
connOptions.m_capture_messages = args.GetBoolArg("-capturemessages", false);
|
||||
|
||||
// Port to bind to if `-bind=addr` is provided without a `:port` suffix.
|
||||
const uint16_t default_bind_port =
|
||||
|
||||
15
src/net.cpp
15
src/net.cpp
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -3900,7 +3901,7 @@ void CConnman::PushMessage(CNode* pnode, CSerializedNetMsg&& msg)
|
||||
AssertLockNotHeld(m_total_bytes_sent_mutex);
|
||||
size_t nMessageSize = msg.data.size();
|
||||
LogDebug(BCLog::NET, "sending %s (%d bytes) peer=%d\n", msg.m_type, nMessageSize, pnode->GetId());
|
||||
if (gArgs.GetBoolArg("-capturemessages", false)) {
|
||||
if (m_capture_messages) {
|
||||
CaptureMessage(pnode->addr, msg.m_type, msg.data, /*is_incoming=*/false);
|
||||
}
|
||||
|
||||
|
||||
14
src/net.h
14
src/net.h
@ -1086,6 +1086,7 @@ public:
|
||||
bool m_i2p_accept_incoming;
|
||||
bool whitelist_forcerelay = DEFAULT_WHITELISTFORCERELAY;
|
||||
bool whitelist_relay = DEFAULT_WHITELISTRELAY;
|
||||
bool m_capture_messages = false;
|
||||
};
|
||||
|
||||
void Init(const Options& connOptions) EXCLUSIVE_LOCKS_REQUIRED(!m_added_nodes_mutex, !m_total_bytes_sent_mutex)
|
||||
@ -1123,8 +1124,12 @@ public:
|
||||
m_onion_binds = connOptions.onion_binds;
|
||||
whitelist_forcerelay = connOptions.whitelist_forcerelay;
|
||||
whitelist_relay = connOptions.whitelist_relay;
|
||||
m_capture_messages = connOptions.m_capture_messages;
|
||||
}
|
||||
|
||||
// test only
|
||||
void SetCaptureMessages(bool cap) { m_capture_messages = cap; }
|
||||
|
||||
CConnman(uint64_t seed0,
|
||||
uint64_t seed1,
|
||||
AddrMan& addrman,
|
||||
@ -1314,7 +1319,7 @@ public:
|
||||
void WakeMessageHandler() EXCLUSIVE_LOCKS_REQUIRED(!mutexMsgProc);
|
||||
|
||||
/** 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);
|
||||
|
||||
@ -1364,7 +1369,7 @@ private:
|
||||
void DisconnectNodes() EXCLUSIVE_LOCKS_REQUIRED(!m_reconnections_mutex, !m_nodes_mutex);
|
||||
void NotifyNumConnectionsChanged();
|
||||
/** 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.
|
||||
@ -1666,6 +1671,11 @@ private:
|
||||
*/
|
||||
bool whitelist_relay;
|
||||
|
||||
/**
|
||||
* flag for whether messages are captured
|
||||
*/
|
||||
bool m_capture_messages{false};
|
||||
|
||||
/**
|
||||
* Mutex protecting m_i2p_sam_sessions.
|
||||
*/
|
||||
|
||||
@ -814,7 +814,7 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
|
||||
// Pretend that we bound to this port.
|
||||
const uint16_t bind_port = 20001;
|
||||
m_node.args->ForceSetArg("-bind", strprintf("3.4.5.6:%u", bind_port));
|
||||
m_node.args->ForceSetArg("-capturemessages", "1");
|
||||
m_node.connman->SetCaptureMessages(true);
|
||||
|
||||
// Our address:port as seen from the peer - 2.3.4.5:20002 (different from the above).
|
||||
in_addr peer_us_addr;
|
||||
@ -892,7 +892,7 @@ BOOST_AUTO_TEST_CASE(initial_advertise_from_version_message)
|
||||
|
||||
CaptureMessage = CaptureMessageOrig;
|
||||
chainman.ResetIbd();
|
||||
m_node.args->ForceSetArg("-capturemessages", "0");
|
||||
m_node.connman->SetCaptureMessages(false);
|
||||
m_node.args->ForceSetArg("-bind", "");
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user