From 180eff2d7bc1f81c3b83166cfe3d37c32332279c Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Tue, 30 Apr 2024 09:41:51 -0500 Subject: [PATCH] server: Use atomic types for some svr peer fields. This updates a couple of cases of server peer data fields protected by an individual mutex to make use of the newer atomic types introduced in Go 1.19 instead. This is desirable since atomic types are both simpler to use and more efficient when the data being protected does not require additional logic in the critical section as is the case for the updated fields. --- rpcadaptors.go | 2 +- server.go | 41 ++++++++--------------------------------- 2 files changed, 9 insertions(+), 34 deletions(-) diff --git a/rpcadaptors.go b/rpcadaptors.go index 643f9f802e..ea22f78328 100644 --- a/rpcadaptors.go +++ b/rpcadaptors.go @@ -94,7 +94,7 @@ func (p *rpcPeer) LastPingNonce() uint64 { // This function is safe for concurrent access and is part of the rpcserver.Peer // interface implementation. func (p *rpcPeer) IsTxRelayDisabled() bool { - return (*serverPeer)(p).relayTxDisabled() + return (*serverPeer)(p).disableRelayTx.Load() } // BanScore returns the current integer value that represents how close the peer diff --git a/server.go b/server.go index 1c0ee6d2d6..a6692915f3 100644 --- a/server.go +++ b/server.go @@ -551,8 +551,7 @@ type serverPeer struct { server *server persistent bool continueHash atomic.Pointer[chainhash.Hash] - relayMtx sync.Mutex - disableRelayTx bool + disableRelayTx atomic.Bool isWhitelisted bool knownAddresses *apbf.Filter banScore connmgr.DynamicBanScore @@ -575,8 +574,7 @@ type serverPeer struct { blockProcessed chan struct{} // peerNa is network address of the peer connected to. - peerNa *wire.NetAddress - peerNaMtx sync.Mutex + peerNa atomic.Pointer[wire.NetAddress] // announcedBlock tracks the most recent block announced to this peer and is // used to filter duplicates. @@ -824,25 +822,6 @@ func (sp *serverPeer) addressKnown(na *addrmgr.NetAddress) bool { return sp.knownAddresses.Contains([]byte(na.Key())) } -// setDisableRelayTx toggles relaying of transactions for the given peer. -// It is safe for concurrent access. -func (sp *serverPeer) setDisableRelayTx(disable bool) { - sp.relayMtx.Lock() - sp.disableRelayTx = disable - sp.relayMtx.Unlock() -} - -// relayTxDisabled returns whether or not relaying of transactions for the given -// peer is disabled. -// It is safe for concurrent access. -func (sp *serverPeer) relayTxDisabled() bool { - sp.relayMtx.Lock() - isDisabled := sp.disableRelayTx - sp.relayMtx.Unlock() - - return isDisabled -} - // wireToAddrmgrNetAddress converts a wire NetAddress to an address manager // NetAddress. func wireToAddrmgrNetAddress(netAddr *wire.NetAddress) *addrmgr.NetAddress { @@ -1022,12 +1001,10 @@ func (sp *serverPeer) OnVersion(_ *peer.Peer, msg *wire.MsgVersion) { } } - sp.peerNaMtx.Lock() - sp.peerNa = &msg.AddrYou - sp.peerNaMtx.Unlock() + sp.peerNa.Store(&msg.AddrYou) // Choose whether or not to relay transactions. - sp.setDisableRelayTx(msg.DisableRelayTx) + sp.disableRelayTx.Store(msg.DisableRelayTx) // Add the remote peer time as a sample for creating an offset against // the local clock to keep the network time in sync. @@ -1861,9 +1838,7 @@ func (s *server) handleAddPeerMsg(state *peerState, sp *serverPeer) bool { return false } - sp.peerNaMtx.Lock() - na := sp.peerNa - sp.peerNaMtx.Unlock() + na := sp.peerNa.Load() // Add the new peer and start it. srvrLog.Debugf("New peer %s", sp) @@ -2062,9 +2037,9 @@ func (s *server) handleRelayInvMsg(state *peerState, msg relayMsg) { } if iv.Type == wire.InvTypeTx { - // Don't relay the transaction to the peer when it has - // transaction relaying disabled. - if sp.relayTxDisabled() { + // Don't relay the transaction to the peer when it has transaction + // relaying disabled. + if sp.disableRelayTx.Load() { return } }