Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addrmgr: Improve documentation. #1125

Merged
merged 1 commit into from
Mar 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 19 additions & 18 deletions addrmgr/addrmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ const PeersFilename = "peers.json"
// AddrManager provides a concurrency safe address manager for caching potential
// peers on the Decred network.
type AddrManager struct {
mtx sync.Mutex
peersFile string
lookupFunc func(string) ([]net.IP, error)
rand *rand.Rand
key [32]byte
addrIndex map[string]*KnownAddress // address key to ka for all addrs.
addrNew [newBucketCount]map[string]*KnownAddress
addrTried [triedBucketCount]*list.List
started int32
shutdown int32
wg sync.WaitGroup
quit chan struct{}
nTried int
nNew int
lamtx sync.Mutex
localAddresses map[string]*localAddress
mtx sync.Mutex // main mutex used to sync methods
peersFile string // path of file to store peers in
lookupFunc func(string) ([]net.IP, error) // for DNS lookups
rand *rand.Rand // internal PRNG
key [32]byte // cryptographically secure random bytes
addrIndex map[string]*KnownAddress // address key to ka for all addresses
addrNew [newBucketCount]map[string]*KnownAddress // storage for new addresses
addrTried [triedBucketCount]*list.List // storage for tried addresses
started int32 // is 1 if started
shutdown int32 // is 1 if shutdown is done or in progress
wg sync.WaitGroup // wait group used by main handler
quit chan struct{} // channel to notify main handler of shutdown
nTried int // number of tried addresses
nNew int // number of new addresses (i.e., not tried)
lamtx sync.Mutex // local address mutex
localAddresses map[string]*localAddress // address key to la for all local addresses
}

type serializedKnownAddress struct {
Expand Down Expand Up @@ -664,7 +664,7 @@ func (a *AddrManager) AddressCache() []*wire.NetAddress {
// `numAddresses' since we are throwing the rest.
for i := 0; i < numAddresses; i++ {
// pick a number between current index and the end
j := rand.Intn(addrIndexLen-i) + i
j := a.rand.Intn(addrIndexLen-i) + i
allAddr[i], allAddr[j] = allAddr[j], allAddr[i]
}

Expand Down Expand Up @@ -936,7 +936,7 @@ func (a *AddrManager) Good(addr *wire.NetAddress) {
rmka.refs++

// We don't touch a.nTried here since the number of tried stays the same
// but we decemented new above, raise it again since we're putting
// but we decremented a.nNew above, raise it again since we're putting
// something back.
a.nNew++

Expand Down Expand Up @@ -1091,6 +1091,7 @@ func (a *AddrManager) GetBestLocalAddress(remoteAddr *wire.NetAddress) *wire.Net

// New returns a new Decred address manager.
// Use Start to begin processing asynchronous address updates.
// The address manager uses lookupFunc for necessary DNS lookups.
func New(dataDir string, lookupFunc func(string) ([]net.IP, error)) *AddrManager {
am := AddrManager{
peersFile: filepath.Join(dataDir, PeersFilename),
Expand Down