|
@ -86,14 +86,17 @@ FOR_LOOP: |
|
|
|
|
|
|
|
|
// Ensures that sufficient peers are connected.
|
|
|
// Ensures that sufficient peers are connected.
|
|
|
func (pm *PeerManager) ensurePeers() { |
|
|
func (pm *PeerManager) ensurePeers() { |
|
|
numPeers := pm.sw.NumOutboundPeers() |
|
|
|
|
|
numDialing := pm.sw.dialing.Size() |
|
|
|
|
|
numToDial := minNumPeers - (numPeers + numDialing) |
|
|
|
|
|
|
|
|
numOutPeers, _, numDialing := pm.sw.NumPeers() |
|
|
|
|
|
numToDial := minNumPeers - (numOutPeers + numDialing) |
|
|
if numToDial <= 0 { |
|
|
if numToDial <= 0 { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
toDial := NewCMap() |
|
|
|
|
|
|
|
|
|
|
|
// Try to pick numToDial addresses to dial.
|
|
|
|
|
|
// TODO: improve logic.
|
|
|
for i := 0; i < numToDial; i++ { |
|
|
for i := 0; i < numToDial; i++ { |
|
|
newBias := MinInt(numPeers, 8)*10 + 10 |
|
|
|
|
|
|
|
|
newBias := MinInt(numOutPeers, 8)*10 + 10 |
|
|
var picked *NetAddress |
|
|
var picked *NetAddress |
|
|
// Try to fetch a new peer 3 times.
|
|
|
// Try to fetch a new peer 3 times.
|
|
|
// This caps the maximum number of tries to 3 * numToDial.
|
|
|
// This caps the maximum number of tries to 3 * numToDial.
|
|
@ -103,7 +106,9 @@ func (pm *PeerManager) ensurePeers() { |
|
|
log.Debug("Empty addrbook.") |
|
|
log.Debug("Empty addrbook.") |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
if pm.sw.Peers().Has(picked) { |
|
|
|
|
|
|
|
|
if toDial.Has(picked.String()) || |
|
|
|
|
|
pm.sw.IsDialing(picked) || |
|
|
|
|
|
pm.sw.Peers().Has(picked) { |
|
|
continue |
|
|
continue |
|
|
} else { |
|
|
} else { |
|
|
break |
|
|
break |
|
@ -112,7 +117,12 @@ func (pm *PeerManager) ensurePeers() { |
|
|
if picked == nil { |
|
|
if picked == nil { |
|
|
continue |
|
|
continue |
|
|
} |
|
|
} |
|
|
// Dial picked address
|
|
|
|
|
|
|
|
|
toDial.Set(picked.String(), picked) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Dial picked addresses
|
|
|
|
|
|
for _, item := range toDial.Values() { |
|
|
|
|
|
picked := item.(*NetAddress) |
|
|
go func() { |
|
|
go func() { |
|
|
peer, err := pm.sw.DialPeerWithAddress(picked) |
|
|
peer, err := pm.sw.DialPeerWithAddress(picked) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|