mirror of
https://github.com/btcsuite/btcd.git
synced 2025-01-19 05:33:36 +01:00
server: add addressesMtx to fix race condition
This commit is contained in:
parent
a41498d578
commit
c2ca0a408e
@ -273,6 +273,7 @@ type serverPeer struct {
|
||||
sentAddrs bool
|
||||
isWhitelisted bool
|
||||
filter *bloom.Filter
|
||||
addressesMtx sync.RWMutex
|
||||
knownAddresses map[string]struct{}
|
||||
banScore connmgr.DynamicBanScore
|
||||
quit chan struct{}
|
||||
@ -305,14 +306,18 @@ func (sp *serverPeer) newestBlock() (*chainhash.Hash, int32, error) {
|
||||
// addKnownAddresses adds the given addresses to the set of known addresses to
|
||||
// the peer to prevent sending duplicate addresses.
|
||||
func (sp *serverPeer) addKnownAddresses(addresses []*wire.NetAddress) {
|
||||
sp.addressesMtx.Lock()
|
||||
for _, na := range addresses {
|
||||
sp.knownAddresses[addrmgr.NetAddressKey(na)] = struct{}{}
|
||||
}
|
||||
sp.addressesMtx.Unlock()
|
||||
}
|
||||
|
||||
// addressKnown true if the given address is already known to the peer.
|
||||
func (sp *serverPeer) addressKnown(na *wire.NetAddress) bool {
|
||||
sp.addressesMtx.RLock()
|
||||
_, exists := sp.knownAddresses[addrmgr.NetAddressKey(na)]
|
||||
sp.addressesMtx.RUnlock()
|
||||
return exists
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user