addrmgr: Use RLock/RUnlock when possible

This commit is contained in:
Gustavo Chain 2021-03-09 18:22:04 +01:00 committed by John C. Vernaleo
parent 01c6a6fe9b
commit f86ae60936

View file

@ -30,7 +30,7 @@ import (
// AddrManager provides a concurrency safe address manager for caching potential // AddrManager provides a concurrency safe address manager for caching potential
// peers on the bitcoin network. // peers on the bitcoin network.
type AddrManager struct { type AddrManager struct {
mtx sync.Mutex mtx sync.RWMutex
peersFile string peersFile string
lookupFunc func(string) ([]net.IP, error) lookupFunc func(string) ([]net.IP, error)
rand *rand.Rand rand *rand.Rand
@ -645,8 +645,8 @@ func (a *AddrManager) numAddresses() int {
// NumAddresses returns the number of addresses known to the address manager. // NumAddresses returns the number of addresses known to the address manager.
func (a *AddrManager) NumAddresses() int { func (a *AddrManager) NumAddresses() int {
a.mtx.Lock() a.mtx.RLock()
defer a.mtx.Unlock() defer a.mtx.RUnlock()
return a.numAddresses() return a.numAddresses()
} }
@ -654,8 +654,8 @@ func (a *AddrManager) NumAddresses() int {
// NeedMoreAddresses returns whether or not the address manager needs more // NeedMoreAddresses returns whether or not the address manager needs more
// addresses. // addresses.
func (a *AddrManager) NeedMoreAddresses() bool { func (a *AddrManager) NeedMoreAddresses() bool {
a.mtx.Lock() a.mtx.RLock()
defer a.mtx.Unlock() defer a.mtx.RUnlock()
return a.numAddresses() < needAddressThreshold return a.numAddresses() < needAddressThreshold
} }
@ -685,8 +685,8 @@ func (a *AddrManager) AddressCache() []*wire.NetAddress {
// getAddresses returns all of the addresses currently found within the // getAddresses returns all of the addresses currently found within the
// manager's address cache. // manager's address cache.
func (a *AddrManager) getAddresses() []*wire.NetAddress { func (a *AddrManager) getAddresses() []*wire.NetAddress {
a.mtx.Lock() a.mtx.RLock()
defer a.mtx.Unlock() defer a.mtx.RUnlock()
addrIndexLen := len(a.addrIndex) addrIndexLen := len(a.addrIndex)
if addrIndexLen == 0 { if addrIndexLen == 0 {