network: structured output for kademlia table (#1586)

This commit is contained in:
Anton Evangelatov
2019-07-19 13:35:11 +02:00
committed by GitHub
parent 090197227f
commit 9720da34db
2 changed files with 70 additions and 25 deletions

View File

@@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"math/rand"
"sort"
"strings"
"sync"
"time"
@@ -93,6 +94,14 @@ type Kademlia struct {
nDepthSig []chan struct{} // signals when neighbourhood depth nDepth is changed
}
type KademliaInfo struct {
Depth int `json:"depth"`
TotalConnections int `json:"total_connections"`
TotalKnown int `json:"total_known"`
Connections [][]string `json:"connections"`
Known [][]string `json:"known"`
}
// NewKademlia creates a Kademlia table for base address addr
// with parameters as in params
// if params is nil, it uses default values
@@ -422,18 +431,6 @@ func (k *Kademlia) Off(p *Peer) {
}
}
func (k *Kademlia) ListKnown() []*BzzAddr {
res := []*BzzAddr{}
k.addrs.Each(func(val pot.Val) bool {
e := val.(*entry)
res = append(res, e.BzzAddr)
return true
})
return res
}
// EachConn is an iterator with args (base, po, f) applies f to each live peer
// that has proximity order po or less as measured from the base
// if base is nil, kademlia base address is used
@@ -576,6 +573,56 @@ func (k *Kademlia) BaseAddr() []byte {
return k.base
}
func (k *Kademlia) KademliaInfo() KademliaInfo {
k.lock.RLock()
defer k.lock.RUnlock()
return k.kademliaInfo()
}
func (k *Kademlia) kademliaInfo() (ki KademliaInfo) {
ki.Depth = depthForPot(k.conns, k.NeighbourhoodSize, k.base)
ki.TotalConnections = k.conns.Size()
ki.TotalKnown = k.addrs.Size()
ki.Connections = make([][]string, k.MaxProxDisplay)
ki.Known = make([][]string, k.MaxProxDisplay)
k.conns.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val) bool) bool) bool {
if po >= k.MaxProxDisplay {
po = k.MaxProxDisplay - 1
}
row := []string{}
f(func(val pot.Val) bool {
e := val.(*Peer)
row = append(row, fmt.Sprintf("%x", e.Address()))
return true
})
sort.Strings(row)
ki.Connections[po] = row
return true
})
k.addrs.EachBin(k.base, Pof, 0, func(po, size int, f func(func(val pot.Val) bool) bool) bool {
if po >= k.MaxProxDisplay {
po = k.MaxProxDisplay - 1
}
row := []string{}
f(func(val pot.Val) bool {
e := val.(*entry)
row = append(row, fmt.Sprintf("%x", e.Address()))
return true
})
sort.Strings(row)
ki.Known[po] = row
return true
})
return
}
// String returns kademlia table + kaddb table displayed with ascii
func (k *Kademlia) String() string {
k.lock.RLock()