From 4e156bd4f72806142bc8712290099f715be8c8b3 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 25 Nov 2018 12:25:38 -0600 Subject: [PATCH] better cleanup of maps --- misc/sim/run-sim | 2 +- misc/sim/treesim.go | 16 ++++------------ src/yggdrasil/dht.go | 12 ++++++++---- src/yggdrasil/peer.go | 1 - src/yggdrasil/session.go | 35 +++++++++++++++++++++++++++++++++++ src/yggdrasil/signature.go | 5 +++++ 6 files changed, 53 insertions(+), 18 deletions(-) diff --git a/misc/sim/run-sim b/misc/sim/run-sim index 985fe2f..abe108c 100755 --- a/misc/sim/run-sim +++ b/misc/sim/run-sim @@ -1,4 +1,4 @@ #!/bin/bash export GOPATH=$PWD go get -d yggdrasil -go run -tags debug misc/sim/treesim.go +go run -tags debug misc/sim/treesim.go "$@" diff --git a/misc/sim/treesim.go b/misc/sim/treesim.go index 3a0959e..8a4bb2a 100644 --- a/misc/sim/treesim.go +++ b/misc/sim/treesim.go @@ -8,6 +8,7 @@ import "strconv" import "time" import "log" +import "runtime" import "runtime/pprof" import "flag" @@ -280,17 +281,7 @@ func pingNodes(store map[[32]byte]*Node) { } destAddr := dest.core.DEBUG_getAddr()[:] ticker := time.NewTicker(150 * time.Millisecond) - ch := make(chan bool, 1) - ch <- true - doTicker := func() { - for range ticker.C { - select { - case ch <- true: - default: - } - } - } - go doTicker() + sendTo(payload, destAddr) for loop := true; loop; { select { case packet := <-dest.recv: @@ -299,7 +290,7 @@ func pingNodes(store map[[32]byte]*Node) { loop = false } } - case <-ch: + case <-ticker.C: sendTo(payload, destAddr) //dumpDHTSize(store) // note that this uses racey functions to read things... } @@ -458,4 +449,5 @@ func main() { var block chan struct{} <-block } + runtime.GC() } diff --git a/src/yggdrasil/dht.go b/src/yggdrasil/dht.go index 84d8e40..9c2afc2 100644 --- a/src/yggdrasil/dht.go +++ b/src/yggdrasil/dht.go @@ -273,19 +273,23 @@ func (t *dht) ping(info *dhtInfo, target *NodeID) { // Periodic maintenance work to keep important DHT nodes alive. func (t *dht) doMaintenance() { now := time.Now() + newReqs := make(map[boxPubKey]map[NodeID]time.Time, len(t.reqs)) for key, dests := range t.reqs { + newDests := make(map[NodeID]time.Time, len(dests)) for nodeID, start := range dests { if now.Sub(start) > 6*time.Second { if info, isIn := t.table[*getNodeID(&key)]; isIn { info.pings++ } - delete(dests, nodeID) - } - if len(dests) == 0 { - delete(t.reqs, key) + continue } + newDests[nodeID] = start + } + if len(newDests) > 0 { + newReqs[key] = newDests } } + t.reqs = newReqs for infoID, info := range t.table { if now.Sub(info.recv) > time.Minute || info.pings > 3 { delete(t.table, infoID) diff --git a/src/yggdrasil/peer.go b/src/yggdrasil/peer.go index cf82792..e4d0998 100644 --- a/src/yggdrasil/peer.go +++ b/src/yggdrasil/peer.go @@ -175,7 +175,6 @@ func (p *peer) doSendSwitchMsgs() { // This must be launched in a separate goroutine by whatever sets up the peer struct. // It handles link protocol traffic. func (p *peer) linkLoop() { - go p.doSendSwitchMsgs() tick := time.NewTicker(time.Second) defer tick.Stop() for { diff --git a/src/yggdrasil/session.go b/src/yggdrasil/session.go index b0022d7..92ae262 100644 --- a/src/yggdrasil/session.go +++ b/src/yggdrasil/session.go @@ -311,6 +311,11 @@ func (ss *sessions) createSession(theirPermKey *boxPubKey) *sessionInfo { func (ss *sessions) cleanup() { // Time thresholds almost certainly could use some adjusting + for k := range ss.permShared { + // Delete a key, to make sure this eventually shrinks to 0 + delete(ss.permShared, k) + break + } if time.Since(ss.lastCleanup) < time.Minute { return } @@ -319,6 +324,36 @@ func (ss *sessions) cleanup() { s.close() } } + permShared := make(map[boxPubKey]*boxSharedKey, len(ss.permShared)) + for k, v := range ss.permShared { + permShared[k] = v + } + ss.permShared = permShared + sinfos := make(map[handle]*sessionInfo, len(ss.sinfos)) + for k, v := range ss.sinfos { + sinfos[k] = v + } + ss.sinfos = sinfos + byMySes := make(map[boxPubKey]*handle, len(ss.byMySes)) + for k, v := range ss.byMySes { + byMySes[k] = v + } + ss.byMySes = byMySes + byTheirPerm := make(map[boxPubKey]*handle, len(ss.byTheirPerm)) + for k, v := range ss.byTheirPerm { + byTheirPerm[k] = v + } + ss.byTheirPerm = byTheirPerm + addrToPerm := make(map[address]*boxPubKey, len(ss.addrToPerm)) + for k, v := range ss.addrToPerm { + addrToPerm[k] = v + } + ss.addrToPerm = addrToPerm + subnetToPerm := make(map[subnet]*boxPubKey, len(ss.subnetToPerm)) + for k, v := range ss.subnetToPerm { + subnetToPerm[k] = v + } + ss.subnetToPerm = subnetToPerm ss.lastCleanup = time.Now() } diff --git a/src/yggdrasil/signature.go b/src/yggdrasil/signature.go index 203c9ad..12a3d37 100644 --- a/src/yggdrasil/signature.go +++ b/src/yggdrasil/signature.go @@ -86,5 +86,10 @@ func (m *sigManager) cleanup() { delete(m.checked, s) } } + newChecked := make(map[sigBytes]knownSig, len(m.checked)) + for s, k := range m.checked { + newChecked[s] = k + } + m.checked = newChecked m.lastCleaned = time.Now() }