mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
swarm/network: add Kademlia.Saturation method with lock
This commit is contained in:
parent
517476cbeb
commit
bc652aebd9
2 changed files with 12 additions and 4 deletions
|
|
@ -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
|
// 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()
|
||||||
pivotDepth := hive.saturation()
|
|
||||||
hive.Kademlia.lock.RUnlock()
|
|
||||||
// the test exchange is as follows:
|
// the test exchange is as follows:
|
||||||
// 1. pivot sends to the control peer a `subPeersMsg` advertising its depth (ignored)
|
// 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)
|
// 2. peer sends to pivot a `subPeersMsg` advertising its own depth (arbitrarily chosen)
|
||||||
|
|
|
||||||
|
|
@ -735,8 +735,18 @@ func NewPeerPotMap(neighbourhoodSize int, addrs [][]byte) map[string]*PeerPot {
|
||||||
return ppmap
|
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
|
// 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 {
|
func (k *Kademlia) saturation() int {
|
||||||
prev := -1
|
prev := -1
|
||||||
radius := neighbourhoodRadiusForPot(k.conns, k.NeighbourhoodSize, k.base)
|
radius := neighbourhoodRadiusForPot(k.conns, k.NeighbourhoodSize, k.base)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue