From 3d4253b9348b9294623f31291d6d95e0977a2d6b Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Tue, 24 Apr 2018 21:12:32 -0400 Subject: [PATCH] expose how many initial nodes are found --- dht/dht.go | 2 +- dht/dht_test.go | 4 ++-- dht/routing_table.go | 13 +++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/dht/dht.go b/dht/dht.go index 226fa46..5d3cc6f 100644 --- a/dht/dht.go +++ b/dht/dht.go @@ -270,7 +270,7 @@ func (dht *DHT) Start() error { go dht.runHandler() dht.join() - log.Debugf("[%s] DHT ready on %s", dht.node.id.HexShort(), dht.node.Addr().String()) + log.Debugf("[%s] DHT ready on %s (%d nodes found during join)", dht.node.id.HexShort(), dht.node.Addr().String(), dht.rt.Count()) return nil } diff --git a/dht/dht_test.go b/dht/dht_test.go index e302926..ea5cf53 100644 --- a/dht/dht_test.go +++ b/dht/dht_test.go @@ -48,10 +48,10 @@ func TestNodeFinder_FindNodes(t *testing.T) { } if !foundOne { - t.Errorf("did not find node %s", dhts[0].node.id.Hex()) + t.Errorf("did not find first node %s", dhts[0].node.id.Hex()) } if !foundTwo { - t.Errorf("did not find node %s", dhts[1].node.id.Hex()) + t.Errorf("did not find second node %s", dhts[1].node.id.Hex()) } } diff --git a/dht/routing_table.go b/dht/routing_table.go index da8fb3a..caae5c0 100644 --- a/dht/routing_table.go +++ b/dht/routing_table.go @@ -243,6 +243,19 @@ func (rt *routingTable) GetClosest(target Bitmap, limit int) []Node { return nodes } +// Count returns the number of nodes in the routing table +func (rt *routingTable) Count() int { + rt.lock.RLock() + defer rt.lock.RUnlock() + count := 0 + for _, bucket := range rt.buckets { + for curr := bucket.Front(); curr != nil; curr = curr.Next() { + count++ + } + } + return count +} + func findInList(bucket *list.List, value Bitmap) *list.Element { for curr := bucket.Front(); curr != nil; curr = curr.Next() { if curr.Value.(Node).id.Equals(value) {