swarm/network: add Kademlia.Saturation method with lock

This commit is contained in:
Janos Guljas 2019-04-11 15:12:55 +02:00
parent 517476cbeb
commit bc652aebd9
2 changed files with 12 additions and 4 deletions

View file

@ -174,9 +174,7 @@ func testInitialPeersMsg(t *testing.T, peerPO, peerDepth int) {
}
// pivotDepth is the advertised depth of the pivot node we expect in the outgoing subPeersMsg
hive.Kademlia.lock.RLock() // protect Kademlia.conns that are read in Kademlia.saturation()
pivotDepth := hive.saturation()
hive.Kademlia.lock.RUnlock()
pivotDepth := hive.Saturation()
// the test exchange is as follows:
// 1. pivot sends to the control peer a `subPeersMsg` advertising its depth (ignored)
// 2. peer sends to pivot a `subPeersMsg` advertising its own depth (arbitrarily chosen)

View file

@ -735,8 +735,18 @@ func NewPeerPotMap(neighbourhoodSize int, addrs [][]byte) map[string]*PeerPot {
return ppmap
}
// saturation returns the smallest po value in which the node has less than MinBinSize peers
// Saturation returns the smallest po value in which the node has less than MinBinSize peers
// if the iterator reaches neighbourhood radius, then the last bin + 1 is returned
func (k *Kademlia) Saturation() int {
k.lock.RLock()
defer k.lock.RUnlock()
return k.saturation()
}
// saturation returns the smallest po value in which the node has less than MinBinSize peers
// if the iterator reaches neighbourhood radius, then the last bin + 1 is returned.
// This function is safe to use in Kademlia methods that use the lock.
func (k *Kademlia) saturation() int {
prev := -1
radius := neighbourhoodRadiusForPot(k.conns, k.NeighbourhoodSize, k.base)