From f3ec8c5b37d718b1dee249cfa8e528e256bac1e2 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sat, 20 Oct 2018 17:58:54 -0500 Subject: [PATCH] fix admin dht function, more cleanup, and slowly throttle back dht traffic when idle --- src/yggdrasil/admin.go | 38 +++++++++++++++++--------------------- src/yggdrasil/dht.go | 7 +++++++ src/yggdrasil/search.go | 3 --- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/yggdrasil/admin.go b/src/yggdrasil/admin.go index 2446be5..cfbef94 100644 --- a/src/yggdrasil/admin.go +++ b/src/yggdrasil/admin.go @@ -555,27 +555,23 @@ func (a *admin) getData_getSwitchQueues() admin_nodeInfo { func (a *admin) getData_getDHT() []admin_nodeInfo { var infos []admin_nodeInfo getDHT := func() { - /* TODO fix this - now := time.Now() - for i := 0; i < a.core.dht.nBuckets(); i++ { - b := a.core.dht.getBucket(i) - getInfo := func(vs []*dhtInfo, isPeer bool) { - for _, v := range vs { - addr := *address_addrForNodeID(v.getNodeID()) - info := admin_nodeInfo{ - {"ip", net.IP(addr[:]).String()}, - {"coords", fmt.Sprint(v.coords)}, - {"bucket", i}, - {"peer_only", isPeer}, - {"last_seen", int(now.Sub(v.recv).Seconds())}, - } - infos = append(infos, info) - } - } - getInfo(b.other, false) - getInfo(b.peers, true) - } - */ + now := time.Now() + var dhtInfos []*dhtInfo + for _, v := range a.core.dht.table { + dhtInfos = append(dhtInfos, v) + } + sort.SliceStable(dhtInfos, func(i, j int) bool { + return dht_ordered(&a.core.dht.nodeID, dhtInfos[i].getNodeID(), dhtInfos[j].getNodeID()) + }) + for _, v := range dhtInfos { + addr := *address_addrForNodeID(v.getNodeID()) + info := admin_nodeInfo{ + {"ip", net.IP(addr[:]).String()}, + {"coords", fmt.Sprint(v.coords)}, + {"last_seen", int(now.Sub(v.recv).Seconds())}, + } + infos = append(infos, info) + } } a.core.router.doAdmin(getDHT) return infos diff --git a/src/yggdrasil/dht.go b/src/yggdrasil/dht.go index 5b43d06..0516aa5 100644 --- a/src/yggdrasil/dht.go +++ b/src/yggdrasil/dht.go @@ -15,6 +15,7 @@ type dhtInfo struct { coords []byte send time.Time // When we last sent a message recv time.Time // When we last received a message + throttle time.Duration } // Returns the *NodeID associated with dhtInfo.key, calculating it on the fly the first time or from a cache all subsequent times. @@ -101,9 +102,14 @@ func (t *dht) insert(info *dhtInfo) { info.recv = time.Now() if oldInfo, isIn := t.table[*info.getNodeID()]; isIn { info.send = oldInfo.send + info.throttle = oldInfo.throttle } else { info.send = info.recv } + info.throttle += time.Second + if info.throttle > 30*time.Second { + info.throttle = 30 * time.Second + } t.table[*info.getNodeID()] = info } @@ -293,6 +299,7 @@ func (t *dht) doMaintenance() { } } if successor != nil && + now.Sub(successor.recv) > successor.throttle && now.Sub(successor.send) > 6*time.Second { t.ping(successor, nil) } diff --git a/src/yggdrasil/search.go b/src/yggdrasil/search.go index 3f7ff38..dd6b09c 100644 --- a/src/yggdrasil/search.go +++ b/src/yggdrasil/search.go @@ -126,10 +126,7 @@ func (s *searches) doSearchStep(sinfo *searchInfo) { // Send to the next search target var next *dhtInfo next, sinfo.toVisit = sinfo.toVisit[0], sinfo.toVisit[1:] - //var oldPings int - //oldPings, next.pings = next.pings, 0 s.core.dht.ping(next, &sinfo.dest) - //next.pings = oldPings // Don't evict a node for searching with it too much sinfo.visited[*next.getNodeID()] = true } }