mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-06-21 14:22:46 +02:00
wtclient: add clarifying comments to the AddressIterator
This commit is contained in:
parent
9e4c8dd509
commit
4f7e871b42
@ -162,11 +162,15 @@ func (a *addressIterator) next(lock bool) (net.Addr, error) {
|
|||||||
a.mu.Lock()
|
a.mu.Lock()
|
||||||
defer a.mu.Unlock()
|
defer a.mu.Unlock()
|
||||||
|
|
||||||
|
// In-case currentTopAddr is nil (meaning that Reset has not yet been
|
||||||
|
// called), return an error indicating this.
|
||||||
if a.currentTopAddr == nil {
|
if a.currentTopAddr == nil {
|
||||||
return nil, ErrAddressesExhausted
|
return nil, ErrAddressesExhausted
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the next candidate to the subsequent element.
|
// Set the next candidate to the subsequent element. If we are at the
|
||||||
|
// end of the address list, this could mean setting currentTopAddr to
|
||||||
|
// nil.
|
||||||
a.currentTopAddr = a.currentTopAddr.Next()
|
a.currentTopAddr = a.currentTopAddr.Next()
|
||||||
|
|
||||||
for a.currentTopAddr != nil {
|
for a.currentTopAddr != nil {
|
||||||
@ -176,9 +180,26 @@ func (a *addressIterator) next(lock bool) (net.Addr, error) {
|
|||||||
// Check whether this address is still considered a candidate.
|
// Check whether this address is still considered a candidate.
|
||||||
// If it's not, we'll proceed to the next.
|
// If it's not, we'll proceed to the next.
|
||||||
candidate, ok := a.candidates[addrID]
|
candidate, ok := a.candidates[addrID]
|
||||||
|
|
||||||
|
// If the address cannot be found in the candidate set, then
|
||||||
|
// this must mean that the Remove method was called for the
|
||||||
|
// address. The Remove method would have checked that the
|
||||||
|
// address is not the last one in the iterator and that it has
|
||||||
|
// no locks on it. It is therefor safe to remove.
|
||||||
if !ok {
|
if !ok {
|
||||||
|
// Grab the next address candidate. This might be nil
|
||||||
|
// if the iterator is on the last item in the list.
|
||||||
nextCandidate := a.currentTopAddr.Next()
|
nextCandidate := a.currentTopAddr.Next()
|
||||||
|
|
||||||
|
// Remove the address from the list that is no longer
|
||||||
|
// in the candidate set.
|
||||||
a.addrList.Remove(a.currentTopAddr)
|
a.addrList.Remove(a.currentTopAddr)
|
||||||
|
|
||||||
|
// Set the current top to the next candidate. This might
|
||||||
|
// mean setting it to nil if the iterator is on its last
|
||||||
|
// item in which case the loop will be exited and an
|
||||||
|
// ErrAddressesExhausted exhausted error will be
|
||||||
|
// returned.
|
||||||
a.currentTopAddr = nextCandidate
|
a.currentTopAddr = nextCandidate
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -234,9 +255,25 @@ func (a *addressIterator) peek(lock bool) net.Addr {
|
|||||||
|
|
||||||
addrID := a.currentTopAddr.Value.(string)
|
addrID := a.currentTopAddr.Value.(string)
|
||||||
candidate, ok := a.candidates[addrID]
|
candidate, ok := a.candidates[addrID]
|
||||||
|
|
||||||
|
// If the address cannot be found in the candidate set, then
|
||||||
|
// this must mean that the Remove method was called for the
|
||||||
|
// address. The Remove method would have checked that the
|
||||||
|
// address is not the last one in the iterator and that it has
|
||||||
|
// no locks on it. It is therefor safe to remove.
|
||||||
if !ok {
|
if !ok {
|
||||||
|
// Grab the next address candidate. This might be nil
|
||||||
|
// if the iterator is on the last item in the list.
|
||||||
nextCandidate := a.currentTopAddr.Next()
|
nextCandidate := a.currentTopAddr.Next()
|
||||||
|
|
||||||
|
// Remove the address from the list that is no longer
|
||||||
|
// in the candidate set.
|
||||||
a.addrList.Remove(a.currentTopAddr)
|
a.addrList.Remove(a.currentTopAddr)
|
||||||
|
|
||||||
|
// Set the current top to the next candidate. This might
|
||||||
|
// mean setting it to nil if the iterator is on its last
|
||||||
|
// item but this will be reset at the top of the for
|
||||||
|
// loop.
|
||||||
a.currentTopAddr = nextCandidate
|
a.currentTopAddr = nextCandidate
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user