Skip to content

Commit

Permalink
addrmgr: Use RLock/RUnlock when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Chain authored and jcvernaleo committed Mar 16, 2021
1 parent 01c6a6f commit f86ae60
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions addrmgr/addrmanager.go
Expand Up @@ -30,7 +30,7 @@ import (
// AddrManager provides a concurrency safe address manager for caching potential
// peers on the bitcoin network.
type AddrManager struct {
mtx sync.Mutex
mtx sync.RWMutex
peersFile string
lookupFunc func(string) ([]net.IP, error)
rand *rand.Rand
Expand Down Expand Up @@ -645,17 +645,17 @@ func (a *AddrManager) numAddresses() int {

// NumAddresses returns the number of addresses known to the address manager.
func (a *AddrManager) NumAddresses() int {
a.mtx.Lock()
defer a.mtx.Unlock()
a.mtx.RLock()
defer a.mtx.RUnlock()

return a.numAddresses()
}

// NeedMoreAddresses returns whether or not the address manager needs more
// addresses.
func (a *AddrManager) NeedMoreAddresses() bool {
a.mtx.Lock()
defer a.mtx.Unlock()
a.mtx.RLock()
defer a.mtx.RUnlock()

return a.numAddresses() < needAddressThreshold
}
Expand Down Expand Up @@ -685,8 +685,8 @@ func (a *AddrManager) AddressCache() []*wire.NetAddress {
// getAddresses returns all of the addresses currently found within the
// manager's address cache.
func (a *AddrManager) getAddresses() []*wire.NetAddress {
a.mtx.Lock()
defer a.mtx.Unlock()
a.mtx.RLock()
defer a.mtx.RUnlock()

addrIndexLen := len(a.addrIndex)
if addrIndexLen == 0 {
Expand Down

0 comments on commit f86ae60

Please sign in to comment.