Commit 0bd63633 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

dht/kbucket: race condition fix

parent 87458f6d
......@@ -19,6 +19,17 @@ func newBucket() *Bucket {
return b
}
func (b *Bucket) Peers() []peer.ID {
b.lk.RLock()
defer b.lk.RUnlock()
ps := make([]peer.ID, 0, b.list.Len())
for e := b.list.Front(); e != nil; e = e.Next() {
id := e.Value.(peer.ID)
ps = append(ps, id)
}
return ps
}
func (b *Bucket) find(id peer.ID) *list.Element {
b.lk.RLock()
defer b.lk.RUnlock()
......@@ -81,7 +92,3 @@ func (b *Bucket) Split(cpl int, target ID) *Bucket {
}
return newbuck
}
func (b *Bucket) getIter() *list.Element {
return b.list.Front()
}
......@@ -176,11 +176,11 @@ func (rt *RoutingTable) Size() int {
// NOTE: This is potentially unsafe... use at your own risk
func (rt *RoutingTable) ListPeers() []peer.ID {
var peers []peer.ID
rt.tabLock.RLock()
for _, buck := range rt.Buckets {
for e := buck.getIter(); e != nil; e = e.Next() {
peers = append(peers, e.Value.(peer.ID))
}
peers = append(peers, buck.Peers()...)
}
rt.tabLock.RUnlock()
return peers
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment