@cryptotaxi247 / kubo / commits / 260e7a009

sort peers outside of locks

License: MIT Signed-off-by: Jeromy <why@ipfs.io>

Jeromy committed Jun 11, 2016 at 23:07 UTC 260e7a009865116b170177cc304cb413902d682e
1 file changed +3 -2
routing/kbucket/table.go
+3 -2
@@ -144,10 +144,10 @@ func (rt *RoutingTable) NearestPeer(id ID) peer.ID {
144
145 // NearestPeers returns a list of the 'count' closest peers to the given ID
146 func (rt *RoutingTable) NearestPeers(id ID, count int) []peer.ID {
147 - rt.tabLock.RLock()
148 - defer rt.tabLock.RUnlock()
147 cpl := commonPrefixLen(id, rt.local)
148
149 + rt.tabLock.RLock()
150 +
151 // Get bucket at cpl index or last bucket
152 var bucket *Bucket
153 if cpl >= len(rt.Buckets) {
@@ -170,6 +170,7 @@ func (rt *RoutingTable) NearestPeers(id ID, count int) []peer.ID {
170 peerArr = copyPeersFromList(id, peerArr, plist)
171 }
172 }
173 + rt.tabLock.RUnlock()
174
175 // Sort by distance to local peer
176 sort.Sort(peerArr)